Skip to content

Commit

Permalink
fix: prevent unnecessary nft requerying
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Aug 19, 2024
1 parent 34e0837 commit 06603c9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { hexToCV } from '@stacks/transactions';
import { QueryFunctionContext, type UseQueryResult, useQueries } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { getPrincipalFromContractId } from '@leather.io/utils';
import { getPrincipalFromContractId, oneMonthInMs, oneWeekInMs } from '@leather.io/utils';

import { StacksQueryPrefixes } from '../../../query-prefixes';
import { StacksClient, useStacksClient } from '../../stacks-client';
Expand All @@ -12,7 +12,8 @@ import { useGetNonFungibleTokenHoldingsQuery } from './non-fungible-token-holdin
const queryOptions = {
refetchOnWindowFocus: false,
refetchOnMount: false,
staleTime: 10 * 1000,
staleTime: oneWeekInMs,
gcTime: oneMonthInMs,
} as const;

function getTokenId(hex: string) {
Expand All @@ -34,10 +35,21 @@ export function createGetNonFungibleTokenMetadataQueryOptions({
client,
tokenId,
}: CreateGetNonFungibleTokenMetadataQueryOptionsArgs) {
const queryKey = [StacksQueryPrefixes.GetNftMetadata, address, tokenId];

return {
enabled: !!tokenId,
queryKey: [StacksQueryPrefixes.GetNftMetadata, address, tokenId],
queryFn: ({ signal }: QueryFunctionContext) => client.getNftMetadata(address, tokenId, signal),
queryKey,
queryFn: async ({ signal }: QueryFunctionContext) => {
try {
return await client.getNftMetadata(address, tokenId, signal);
} catch (error) {
if (statusCodeNotFoundOrNotProcessable((error as AxiosError).request.status)) {
return null;
}
throw error;
}
},
retry(_count: number, error: AxiosError) {
if (statusCodeNotFoundOrNotProcessable(error.request.status)) return false;
return true;
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export * from './math';
export * from './money';
export * from './sort-assets';
export * from './truncate-middle';
export * from './time';

export { spamFilter } from './spam-filter/spam-filter';
export { extractPhraseFromString } from './extract-phrase-from-string/extract-phrase-from-string';
export { pxStringToNumber } from './px-string-to-number/px-string-to-number';
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const fiveMinInMs = 5 * 60 * 1000;
export const oneDayInMs = 24 * 60 * 60 * 1000;
export const oneWeekInMs = 7 * oneDayInMs;
export const oneMonthInMs = 30 * oneDayInMs;

0 comments on commit 06603c9

Please sign in to comment.