Skip to content

Commit

Permalink
fix compressed nft hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ngundotra committed Jun 17, 2024
1 parent 06b1864 commit 77158e1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
1 change: 0 additions & 1 deletion app/components/account/CompressedNftCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export function CompressedNftCard({ account }: { account: Account }) {
export function CompressedNftAccountHeader({ account }: { account: Account }) {
const { url } = useCluster();
const compressedNft = useCompressedNft({ address: account.pubkey.toString(), url });
if (!compressedNft) throw new Error('Compressed NFT not found');

if (compressedNft) {
return (
Expand Down
65 changes: 34 additions & 31 deletions app/providers/compressed-nft.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import useSWRImmutable from 'swr/immutable';

type CacheType<P> = Record<
string,
void | { __type: 'promise'; promise: Promise<void> } | { __type: 'result'; result: P }
Expand Down Expand Up @@ -43,42 +45,43 @@ function makeCache<Params, CacheValueType>(
};
}

export const useMetadataJsonLink = makeCache<string, any>(
'nftMetadataJson',
(url: string) => url,
async (url: string) => {
export function useMetadataJsonLink(url: string) {
const { data, error } = useSWRImmutable(url, async (url: string) => {
return fetch(url).then(response => response.json());
}
);
});
return error ? null : data;
}

export const useCompressedNft = makeCache<{ address: string; url: string }, CompressedNft | null>(
'compressedNft',
({ address, url }) => `${address}-${url}`,
async ({ address, url }) => {
return fetch(`${url}`, {
body: JSON.stringify({
id: address,
jsonrpc: '2.0',
method: 'getAsset',
params: {
export function useCompressedNft({ address, url }: { address: string; url: string }): CompressedNft | null {
const { data, error } = useSWRImmutable(
[address, url],
async (address: string, url: string): Promise<CompressedNft | null> => {
return fetch(`${url}`, {
body: JSON.stringify({
id: address,
jsonrpc: '2.0',
method: 'getAsset',
params: {
id: address,
},
}),
headers: {
'content-type': 'application/json',
},
}),
headers: {
'content-type': 'application/json',
},
method: 'POST',
})
.then(response => response.json())
.then((response: DasApiResponse<CompressedNft>) => {
if ('error' in response) {
throw new Error(response.error.message);
}
method: 'POST',
})
.then(response => response.json())
.then((response: DasApiResponse<CompressedNft>) => {
if ('error' in response) {
return null;
}

return response.result;
});
}
);
return response.result;
});
}
);
return error ? null : data ?? null;
}

export const useCompressedNftProof = makeCache<{ address: string; url: string }, CompressedNftProof | null>(
'compressedNftProof',
Expand Down

0 comments on commit 77158e1

Please sign in to comment.