Skip to content

Commit

Permalink
fix: remove setData
Browse files Browse the repository at this point in the history
  • Loading branch information
derHowie committed Jan 24, 2025
1 parent b5881aa commit 48d39a2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/components/ens-profile/ProfileSheetHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function ProfileSheetHeader({
});
const isImagesFetched = isAvatarFetched && isCoverFetched;

const uniqueTokens = useUserNftsStore()(state => state?.nfts);
const uniqueTokens = useUserNftsStore(state => state.getData()?.nfts);

const avatarUrl = avatar?.imageUrl;
const { onPress: onPressAvatar } = useOpenENSNFTHandler({
Expand Down
5 changes: 2 additions & 3 deletions src/components/expanded-state/ens/InfoRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,9 @@ function ImageValue({ ensName, url, value }: { ensName?: string; url?: string; v

const { data: address } = useENSAddress({ name: ensName || '' });

const userNftsStore = useUserNftsStore();
const uniqueTokensAccount = userNftsStore(state => state.nfts);
const uniqueTokensAccount = useUserNftsStore(state => state.getData()?.nfts);
const externalNftStore = useNftsStore(address ?? '');
const uniqueTokensProfile = externalNftStore(state => state.nfts);
const uniqueTokensProfile = externalNftStore(state => state.getData()?.nfts);

const isSelf = address === accountAddress;
const uniqueTokens = isSelf ? uniqueTokensAccount : uniqueTokensProfile;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useENSModifiedRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function useENSModifiedRegistration({
} = {}) {
const dispatch = useDispatch();
const { records, initialRecords, name, mode } = useENSRegistration();
const uniqueTokens = useUserNftsStore()(state => state.nfts);
const uniqueTokens = useUserNftsStore(state => state.getData()?.nfts || []);

const fetchEnabled = mode === REGISTRATION_MODES.EDIT || mode === REGISTRATION_MODES.RENEW || mode === REGISTRATION_MODES.SET_NAME;
const { data: avatar, isSuccess: isAvatarSuccess } = useENSAvatar(name, {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useENSPendingRegistrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useENSPendingRegistrations() {
return { accountRegistrations, pendingRegistrations };
});

const uniqueTokens = useUserNftsStore()(state => state.nfts);
const uniqueTokens = useUserNftsStore(state => state.getData()?.nfts || []);
const registrationImages = useMemo(() => {
const registrationImagesArray = pendingRegistrations?.map(({ name, records }) => {
const avatarUrl = getENSNFTAvatarUrl(uniqueTokens, records?.avatar);
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useSendableUniqueTokens.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { groupBy } from 'lodash';
import { useUserNftsStore } from '@/state/nfts';
import { UniqueAsset } from '@/entities';

export default function useSendableUniqueTokens() {
const uniqueTokens = useUserNftsStore()(state => (!Array.isArray(state.nfts) ? [] : state.nfts)) || [];
const sendableUniqueTokens = uniqueTokens?.filter((uniqueToken: any) => uniqueToken.isSendable);
const uniqueTokens = useUserNftsStore(state => state.getData()?.nfts || []);
const sendableUniqueTokens = uniqueTokens?.filter((uniqueToken: UniqueAsset) => uniqueToken.isSendable);
const grouped = groupBy(sendableUniqueTokens, token => token.familyName);
const families = Object.keys(grouped).sort();
const sendableTokens = [];
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useWalletSectionsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ export default function useWalletSectionsData({

const { sendableUniqueTokens } = useSendableUniqueTokens();

const userNftsStore = useUserNftsStore();
const isFetchingNfts = userNftsStore(state => state.status);
const allUniqueTokens = userNftsStore(state => (Array.isArray(state.nfts) ? state.nfts : []));
const isFetchingNfts = useUserNftsStore(state => state.status);
const allUniqueTokens = useUserNftsStore(state => (Array.isArray(state.getData()?.nfts) ? state.getData()?.nfts : []));

const { data: positions } = usePositions({ address: accountAddress, currency: nativeCurrency });
const { data: claimables } = useClaimables({ address: accountAddress, currency: nativeCurrency });
Expand Down
2 changes: 1 addition & 1 deletion src/screens/NFTSingleOfferSheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function NFTSingleOfferSheet() {
const [isAccepting, setIsAccepting] = useState(false);
const txsRef = useRef<string[]>([]);

const nft = useUserNftsStore()(state => state.getNft(offer.nft.uniqueId));
const nft = useUserNftsStore(state => state.getNft(offer.nft.uniqueId));

const insufficientEth = isSufficientGas === false && isValidGas;

Expand Down
4 changes: 2 additions & 2 deletions src/screens/mints/PoapSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const PoapSheet = () => {
const { isReadOnlyWallet } = useWallets();
const params = useRoute();

const nfts = useUserNftsStore()(state => state.nfts);
const nfts = useUserNftsStore(state => state.getData()?.nfts);

const [claimStatus, setClaimStatus] = useState<PoapClaimStatus>('none');
const [errorCode, setErrorCode] = useState<PoapMintError | undefined>(undefined);
Expand Down Expand Up @@ -189,7 +189,7 @@ const PoapSheet = () => {
}, [claimPoapByQrHash, claimPoapBySecret, claimStatus, goBack, navigate, nft, poapMintType]);

useEffect(() => {
const nft = nfts.find(item => item.image_original_url === poapEvent.imageUrl);
const nft = nfts?.find(item => item.image_original_url === poapEvent.imageUrl);
if (nft) {
setClaimStatus('claimed');
setNft(nft);
Expand Down
28 changes: 13 additions & 15 deletions src/state/nfts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fetchUserNfts } from '@/resources/nfts';
import { time } from '@/utils';
import { NftCollectionSortCriterion, SortDirection } from '@/graphql/__generated__/arc';
import { useAccountSettings } from '@/hooks';
import reduxStore from '@/redux/store';

type UserNftsStoreType = ReturnType<typeof createUserNftsStore>;

Expand Down Expand Up @@ -43,14 +44,6 @@ export const createUserNftsStore = (config: NftFactoryConfig) =>
sortBy: $ => $(useNftSortStore, s => s.getNftSort(config.address).sortBy),
sortDirection: $ => $(useNftSortStore, s => s.getNftSort(config.address).sortDirection),
},
setData: ({ data, set }) => {
set(() => {
return {
nfts: data.nfts,
nftsMap: data.nftsMap,
};
});
},
staleTime: time.minutes(10),
},
(_, get) => ({
Expand All @@ -66,9 +59,16 @@ export const createUserNftsStore = (config: NftFactoryConfig) =>

function getOrCreateStore(address: string, internal?: boolean): UserNftsStoreType {
const { cachedAddress, cachedStore } = userNftsStoreManager.getState();
if (cachedAddress && cachedAddress === address && cachedStore) {
return cachedStore;
}
const rawAddress = address?.length ? address : reduxStore.getState().settings.accountAddress;

/**
* This fallback can be removed once Redux is no longer the source of truth for the current
* accountAddress. It's needed to ensure there's an address available immediately upon app
* launch, which currently is not the case — the initial Redux address is an empty string.
*/
const accountAddress = rawAddress?.length ? rawAddress : cachedAddress ?? rawAddress;

if (cachedStore && cachedAddress === accountAddress) return cachedStore;

const newStore = createUserNftsStore({ address, internal });
userNftsStoreManager.setState({ cachedStore: newStore, cachedAddress: address });
Expand All @@ -79,13 +79,11 @@ export function useNftsStore(address: string, internal?: boolean) {
return getOrCreateStore(address, internal);
}

export function useUserNftsStore() {
export function useUserNftsStore<T>(selector: (state: ReturnType<UserNftsStoreType['getState']>) => T) {
const { accountAddress } = useAccountSettings();
return useNftsStore(accountAddress, true);
return useNftsStore(accountAddress, true)(selector);
}

// SORT

export type NftSortAction = `${NftCollectionSortCriterion}|${SortDirection}`;
type NftSortByAddress = Record<string, NftSortAction>;
type NftSort = {
Expand Down

0 comments on commit 48d39a2

Please sign in to comment.