Skip to content

Commit

Permalink
Change from array to Set
Browse files Browse the repository at this point in the history
  • Loading branch information
jakzaizzat committed May 1, 2024
1 parent f0b944c commit 2bf3e38
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { graphql, useFragment } from 'react-relay';
import { GalleryRefreshControl } from '~/components/GalleryRefreshControl';
import { useSyncTokensActions } from '~/contexts/SyncTokensContext';
import { NftSelectorContractPickerGridFragment$key } from '~/generated/NftSelectorContractPickerGridFragment.graphql';
import { SelectedItemMultiMode } from '~/screens/GalleryScreen/GalleryEditorNftSelector';
import { NftSelectorLoadingSkeleton } from '~/screens/NftSelectorScreen/NftSelectorLoadingSkeleton';
import { NftSelectorPickerSingularAsset } from '~/screens/NftSelectorScreen/NftSelectorPickerSingularAsset';

Expand All @@ -17,7 +16,7 @@ type Props = {
style?: ViewProps['style'];

isMultiselectMode?: boolean;
selectedTokens?: SelectedItemMultiMode[];
selectedTokens?: Set<string>;
};

export function NftSelectorContractPickerGrid({
Expand Down Expand Up @@ -55,8 +54,7 @@ export function NftSelectorContractPickerGrid({
}, [syncCreatedTokensForExistingContract, contractId]);

const isSelected = useMemo(() => {
const selectedTokenIds = selectedTokens?.map((token) => token.id) ?? [];
return (tokenId: string) => selectedTokenIds.includes(tokenId);
return (tokenId: string) => selectedTokens?.has(tokenId) ?? false;
}, [selectedTokens]);

const renderItem = useCallback<ListRenderItem<typeof tokens>>(
Expand Down
65 changes: 20 additions & 45 deletions apps/mobile/src/screens/GalleryScreen/GalleryEditorNftSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native';
import { Suspense, useCallback, useMemo, useState } from 'react';
import { View } from 'react-native';
import { Text, View } from 'react-native';

import { Button } from '~/components/Button';
import { NftSelectorHeader } from '~/components/NftSelector/NftSelectorHeader';
Expand All @@ -12,11 +12,6 @@ import { RootStackNavigatorParamList, RootStackNavigatorProp } from '~/navigatio
import { NftSelectorLoadingSkeleton } from '~/screens/NftSelectorScreen/NftSelectorLoadingSkeleton';
import { NftSelectorPickerGrid } from '~/screens/NftSelectorScreen/NftSelectorPickerGrid';

export type SelectedItemMultiMode = {
id: string;
contractAddress?: string;
};

export function GalleryEditorNftSelector() {
const {
searchQuery,
Expand All @@ -37,30 +32,20 @@ export function GalleryEditorNftSelector() {

const [isMultiselectMode, setIsMultiselectMode] = useState(false);

const [selectedTokens, setSelectedTokens] = useState<SelectedItemMultiMode[]>([]);
const [selectedTokens, setSelectedTokens] = useState<Set<string>>(new Set());

const handleSelectNft = useCallback(
(tokenId: string) => {
if (isMultiselectMode) {
setSelectedTokens((prevTokens) => {
if (
prevTokens.some((token) => {
return token.id === tokenId;
})
) {
return [
...prevTokens.filter((token) => {
return token.id !== tokenId;
}),
];
const newTokens = new Set(prevTokens);

if (newTokens.has(tokenId)) {
newTokens.delete(tokenId);
} else {
return [
...prevTokens,
{
id: tokenId,
},
];
newTokens.add(tokenId);
}
return newTokens;
});
} else {
navigation.navigate({
Expand All @@ -79,27 +64,17 @@ export function GalleryEditorNftSelector() {
const handleSelectNftGroup = useCallback(
(contractAddress: string, tokens: NftSelectorPickerGridTokenGridFragment$data[number][]) => {
if (isMultiselectMode) {
const formattedTokens = tokens.map((token) => {
return {
id: token.dbid,
contractAddress,
};
});

setSelectedTokens((prevTokens) => {
if (
prevTokens.some((token) => {
return token.contractAddress === contractAddress;
})
) {
return [
...prevTokens.filter((token) => {
return token.contractAddress !== contractAddress;
}),
];
} else {
return [...prevTokens, ...formattedTokens];
}
const newTokens = new Set(prevTokens);

tokens.forEach((token) => {
if (newTokens.has(token.dbid)) {
newTokens.delete(token.dbid);
} else {
newTokens.add(token.dbid);
}
});
return newTokens;
});
} else {
navigation.navigate({
Expand All @@ -115,7 +90,7 @@ export function GalleryEditorNftSelector() {
);

const handleAddSelectedTokens = useCallback(() => {
const formattedTokens = selectedTokens.map((token) => token.id);
const formattedTokens = Array.from(selectedTokens);

navigation.navigate({
name: 'GalleryEditor',
Expand Down Expand Up @@ -143,7 +118,7 @@ export function GalleryEditorNftSelector() {
<NftSelectorHeader
title="Select item to add"
rightButton={
isMultiselectMode && selectedTokens.length > 0 ? (
isMultiselectMode && selectedTokens.size > 0 ? (
<Button
onPress={handleAddSelectedTokens}
eventElementId={null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { NftSelectorContractWrapper } from '~/components/NftSelector/NftSelector
import { GalleryEditorNftSelectorContractScreenQuery } from '~/generated/GalleryEditorNftSelectorContractScreenQuery.graphql';
import { RootStackNavigatorParamList, RootStackNavigatorProp } from '~/navigation/types';

import { SelectedItemMultiMode } from './GalleryEditorNftSelector';

export function GalleryEditorNftSelectorContractScreen() {
const query = useLazyLoadQuery<GalleryEditorNftSelectorContractScreenQuery>(
graphql`
Expand Down Expand Up @@ -46,7 +44,7 @@ export function GalleryEditorNftSelectorContractScreen() {
useRoute<RouteProp<RootStackNavigatorParamList, 'NftSelectorContractGalleryEditor'>>();

const [isMultiselectMode, setIsMultiselectMode] = useState(false);
const [selectedTokens, setSelectedTokens] = useState<SelectedItemMultiMode[]>([]);
const [selectedTokens, setSelectedTokens] = useState<Set<string>>(new Set());

const contractAddress = route.params.contractAddress;
const isCreator = route.params.ownerFilter === 'Created';
Expand All @@ -70,24 +68,14 @@ export function GalleryEditorNftSelectorContractScreen() {
(tokenId: string) => {
if (isMultiselectMode) {
setSelectedTokens((prevTokens) => {
if (
prevTokens.some((token) => {
return token.id === tokenId;
})
) {
return [
...prevTokens.filter((token) => {
return token.id !== tokenId;
}),
];
const newTokens = new Set(prevTokens);

if (newTokens.has(tokenId)) {
newTokens.delete(tokenId);
} else {
return [
...prevTokens,
{
id: tokenId,
},
];
newTokens.add(tokenId);
}
return newTokens;
});
} else {
navigation.navigate({
Expand All @@ -104,7 +92,7 @@ export function GalleryEditorNftSelectorContractScreen() {
);

const handleAddSelectedTokens = useCallback(() => {
const formattedTokens = selectedTokens.map((token) => token.id);
const formattedTokens = Array.from(selectedTokens);

navigation.navigate({
name: 'GalleryEditor',
Expand All @@ -118,16 +106,14 @@ export function GalleryEditorNftSelectorContractScreen() {

const handleSelectedAllPress = useCallback(() => {
setSelectedTokens((prevTokens) => {
if (prevTokens.length > 0) {
const newTokens = new Set(prevTokens);

if (newTokens.size > 0) {
setIsMultiselectMode(false);
return [];
return new Set();
} else {
setIsMultiselectMode(true);
return nonNullableTokens.map((token) => {
return {
id: token.dbid,
};
});
return new Set(nonNullableTokens.map((token) => token.dbid));
}
});
}, [nonNullableTokens]);
Expand All @@ -139,7 +125,7 @@ export function GalleryEditorNftSelectorContractScreen() {
isCreator={isCreator}
contractId={contractId}
rightButton={
selectedTokens.length > 0 ? (
selectedTokens.size > 0 ? (
<Button
onPress={handleAddSelectedTokens}
eventElementId={null}
Expand All @@ -159,7 +145,7 @@ export function GalleryEditorNftSelectorContractScreen() {
isMultiselectMode={isMultiselectMode}
setIsMultiselectMode={setIsMultiselectMode}
onSelectedAllPress={handleSelectedAllPress}
hasSelectedItems={selectedTokens.length > 0}
hasSelectedItems={selectedTokens.size > 0}
ownershipTypeFilter={route.params.ownerFilter || 'Collected'}
/>
<NftSelectorContractPickerGrid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { contexts } from '~/shared/analytics/constants';
import { removeNullValues } from '~/shared/relay/removeNullValues';
import { doesUserOwnWalletFromChainFamily } from '~/shared/utils/doesUserOwnWalletFromChainFamily';

import { SelectedItemMultiMode } from '../GalleryScreen/GalleryEditorNftSelector';
import { NftSelectorLoadingSkeleton } from './NftSelectorLoadingSkeleton';

type NftSelectorPickerGridProps = {
Expand All @@ -52,7 +51,7 @@ type NftSelectorPickerGridProps = {
};

isMultiselectMode?: boolean;
selectedTokens?: SelectedItemMultiMode[];
selectedTokens?: Set<string>;

onRefresh: () => void;

Expand All @@ -67,7 +66,7 @@ export function NftSelectorPickerGrid({
searchCriteria,
style,
isMultiselectMode,
selectedTokens = [],
selectedTokens = new Set(),
onRefresh,
onSelect,
onSelectNftGroup,
Expand Down Expand Up @@ -533,7 +532,7 @@ type TokenGroupProps = {
tokens: NftSelectorPickerGridTokenGridFragment$data[number][]
) => void;
isMultiselectMode?: boolean;
selectedTokens?: SelectedItemMultiMode[];
selectedTokens?: Set<string>;
};

function TokenGroup({
Expand All @@ -544,7 +543,7 @@ function TokenGroup({
onSelectNft,
onSelectGroup,
isMultiselectMode,
selectedTokens = [],
selectedTokens = new Set(),
}: TokenGroupProps) {
const tokens = useFragment(
graphql`
Expand All @@ -564,9 +563,7 @@ function TokenGroup({
}, [tokens]);

const isSelected = useMemo(() => {
return selectedTokens.some((token) => {
return token.id === firstToken?.dbid;
});
return selectedTokens.has(firstToken?.dbid ?? '');
}, [firstToken?.dbid, selectedTokens]);

if (!firstToken) {
Expand Down

0 comments on commit 2bf3e38

Please sign in to comment.