-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incessant requerying of nfts with missing metadata
- Loading branch information
1 parent
1f9fc20
commit 4b54db2
Showing
6 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { hexToCV } from '@stacks/transactions'; | ||
import { useQueries } from '@tanstack/react-query'; | ||
import type { AxiosError } from 'axios'; | ||
|
||
import { | ||
createGetNonFungibleTokenMetadataQueryOptions, | ||
useGetNonFungibleTokenHoldingsQuery, | ||
useStacksClient, | ||
} from '@leather.io/query'; | ||
|
||
import { getPrincipalFromContractId } from '@app/common/utils'; | ||
import { useStacksNftMissingMetadata } from '@app/store/missing-stacks-nfts/missing-stacks-nfts.slice'; | ||
|
||
function getTokenId(hex: string) { | ||
const clarityValue = hexToCV(hex); | ||
return clarityValue.type === 1 ? Number(clarityValue.value) : 0; | ||
} | ||
|
||
function statusCodeNotFoundOrNotProcessable(status: number) { | ||
return status === 404 || status === 422; | ||
} | ||
|
||
export function useStacksNonFungibleTokensMetadata(address: string) { | ||
const nftHoldings = useGetNonFungibleTokenHoldingsQuery(address); | ||
const stacksClient = useStacksClient(); | ||
|
||
const { addKnownMissingStacksNft, isMissingStacksNftMetadata } = useStacksNftMissingMetadata(); | ||
|
||
return useQueries({ | ||
queries: (nftHoldings.data?.results ?? []).map(nft => { | ||
const address = getPrincipalFromContractId(nft.asset_identifier); | ||
const tokenId = getTokenId(nft.value.hex); | ||
|
||
const identifier = address + tokenId; | ||
|
||
return { | ||
...createGetNonFungibleTokenMetadataQueryOptions({ | ||
address, | ||
client: stacksClient, | ||
tokenId, | ||
}), | ||
enabled: !isMissingStacksNftMetadata(identifier), | ||
retry(_count: number, error: AxiosError) { | ||
if (statusCodeNotFoundOrNotProcessable(error.request.status)) { | ||
addKnownMissingStacksNft(identifier); | ||
return false; | ||
} | ||
return true; | ||
}, | ||
}; | ||
}), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/app/store/missing-stacks-nfts/missing-stacks-nfts.slice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useMemo } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { type PayloadAction, createSlice } from '@reduxjs/toolkit'; | ||
|
||
import { type RootState, useAppDispatch } from '..'; | ||
|
||
export const missingDataSlice = createSlice({ | ||
name: 'missingStacksNfts', | ||
initialState: { | ||
stacksNfts: [] as string[], | ||
}, | ||
reducers: { | ||
addKnownMissingStacksNft(state, action: PayloadAction<string>) { | ||
state.stacksNfts.push(action.payload); | ||
}, | ||
}, | ||
}); | ||
|
||
const { addKnownMissingStacksNft } = missingDataSlice.actions; | ||
|
||
const selectMisingStacksNfts = (state: RootState) => state.missingData; | ||
|
||
export function useStacksNftMissingMetadata() { | ||
const dispatch = useAppDispatch(); | ||
const missingData = useSelector(selectMisingStacksNfts); | ||
return useMemo( | ||
() => ({ | ||
missingNftIds: missingData.stacksNfts, | ||
isMissingStacksNftMetadata(id: string) { | ||
return missingData.stacksNfts.includes(id); | ||
}, | ||
addKnownMissingStacksNft(id: string) { | ||
dispatch(addKnownMissingStacksNft(id)); | ||
}, | ||
}), | ||
[dispatch, missingData.stacksNfts] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters