Skip to content

Commit 2017892

Browse files
committed
fix(sdk): build suspense into nft provider
1 parent f4eeed3 commit 2017892

File tree

5 files changed

+45
-156
lines changed

5 files changed

+45
-156
lines changed

packages/thirdweb/src/extensions/prebuilts/deploy-published.test.ts

Lines changed: 0 additions & 132 deletions
This file was deleted.

packages/thirdweb/src/react/web/ui/prebuilt/NFT/description.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { useSuspenseQuery } from "@tanstack/react-query";
4+
import { Suspense } from "react";
45
import { getNFTInfo } from "./media.js";
56
import { useNFTContext } from "./provider.js";
67

@@ -37,9 +38,10 @@ import { useNFTContext } from "./provider.js";
3738
export function Description(props: {
3839
className: string;
3940
style: React.CSSProperties;
41+
fallback?: React.ReactNode;
4042
}) {
4143
const { contract, tokenId } = useNFTContext();
42-
const nftQuery = useSuspenseQuery({
44+
const { data } = useSuspenseQuery({
4345
queryKey: [
4446
"__nft_info_internal__",
4547
contract.chain.id,
@@ -48,6 +50,10 @@ export function Description(props: {
4850
],
4951
queryFn: () => getNFTInfo({ contract, tokenId }),
5052
});
51-
const description = nftQuery.data?.metadata.description || "";
52-
return <span {...props}>{description}</span>;
53+
54+
return (
55+
<Suspense fallback={props.fallback ?? null}>
56+
<span {...props}>{data.metadata.description}</span>
57+
</Suspense>
58+
);
5359
}

packages/thirdweb/src/react/web/ui/prebuilt/NFT/media.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useSuspenseQuery } from "@tanstack/react-query";
2+
import { Suspense } from "react";
23
import { getNFT as getNFT721 } from "../../../../../extensions/erc721/read/getNFT.js";
34
import { getNFT as getNFT1155 } from "../../../../../extensions/erc1155/read/getNFT.js";
45
import type { NFT } from "../../../../../utils/nft/parseNft.js";
@@ -16,7 +17,12 @@ import { type NFTProviderProps, useNFTContext } from "./provider.js";
1617
export type NFTMediaProps = Omit<
1718
MediaRendererProps,
1819
"src" | "poster" | "client"
19-
>;
20+
> & {
21+
/**
22+
* Fallback content to display while the NFT data is being fetched
23+
*/
24+
fallback?: React.ReactNode;
25+
};
2026

2127
/**
2228
* This component fetches and displays an NFT's media. It uses thirdweb [`MediaRenderer`](https://portal.thirdweb.com/refernces/typescript/v5/MediaRenderer) under the hood
@@ -60,7 +66,7 @@ export type NFTMediaProps = Omit<
6066
*/
6167
export function Media(props: NFTMediaProps) {
6268
const { contract, tokenId } = useNFTContext();
63-
const nftQuery = useSuspenseQuery({
69+
const { data } = useSuspenseQuery({
6470
queryKey: [
6571
"__nft_info_internal__",
6672
contract.chain.id,
@@ -69,17 +75,18 @@ export function Media(props: NFTMediaProps) {
6975
],
7076
queryFn: () => getNFTInfo({ contract, tokenId }),
7177
});
72-
const animation_url = nftQuery.data?.metadata.animation_url;
73-
const image =
74-
nftQuery.data?.metadata.image || nftQuery.data?.metadata.image_url;
78+
const animation_url = data.metadata.animation_url;
79+
const image = data.metadata.image || data.metadata.image_url;
7580

7681
return (
77-
<MediaRenderer
78-
client={contract.client}
79-
src={animation_url || image}
80-
poster={image}
81-
{...props}
82-
/>
82+
<Suspense fallback={props.fallback ?? null}>
83+
<MediaRenderer
84+
client={contract.client}
85+
src={animation_url || image}
86+
poster={image}
87+
{...props}
88+
/>
89+
</Suspense>
8390
);
8491
}
8592

packages/thirdweb/src/react/web/ui/prebuilt/NFT/name.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useSuspenseQuery } from "@tanstack/react-query";
2+
import { Suspense } from "react";
23
import { getNFTInfo } from "./media.js";
34
import { useNFTContext } from "./provider.js";
45

@@ -37,9 +38,10 @@ import { useNFTContext } from "./provider.js";
3738
export function Name(props: {
3839
className?: string;
3940
style?: React.CSSProperties;
41+
fallback?: React.ReactNode;
4042
}) {
4143
const { contract, tokenId } = useNFTContext();
42-
const nftQuery = useSuspenseQuery({
44+
const { data } = useSuspenseQuery({
4345
queryKey: [
4446
"__nft_info_internal__",
4547
contract.chain.id,
@@ -48,6 +50,10 @@ export function Name(props: {
4850
],
4951
queryFn: () => getNFTInfo({ contract, tokenId }),
5052
});
51-
const name = nftQuery.data?.metadata.name || "";
52-
return <span {...props}>{name}</span>;
53+
54+
return (
55+
<Suspense fallback={props.fallback ?? null}>
56+
<span {...props}>{data.metadata.name}</span>
57+
</Suspense>
58+
);
5359
}

packages/thirdweb/src/react/web/ui/prebuilt/NFT/provider.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext } from "react";
1+
import { Suspense, createContext, useContext } from "react";
22
import type { ThirdwebContract } from "../../../../../contract/contract.js";
33

44
/**
@@ -14,6 +14,10 @@ export type NFTProviderProps = {
1414
* The tokenId whose info you want to display
1515
*/
1616
tokenId: bigint;
17+
/**
18+
* Fallback content to display while the NFT data is being fetched
19+
*/
20+
fallback?: React.ReactNode;
1721
};
1822

1923
/**
@@ -57,19 +61,17 @@ export function useNFTContext() {
5761
* client: yourThirdwebClient,
5862
* });
5963
*
60-
* <NFT.Provider contract={contract} tokenId={0n}>
61-
* <Suspense fallback={"Loading media..."}>
62-
* <NFT.Media />
63-
* <NFT.Description />
64-
* </Suspense>
64+
* <NFT.Provider contract={contract} tokenId={0n} fallback={"Loading media..."}>
65+
* <NFT.Media />
66+
* <NFT.Description />
6567
* </NFT>
6668
* ```
6769
* @nft
6870
*/
6971
export function Provider(props: React.PropsWithChildren<NFTProviderProps>) {
7072
return (
7173
<NFTProviderContext.Provider value={props}>
72-
{props.children}
74+
<Suspense fallback={props.fallback ?? null}>{props.children}</Suspense>
7375
</NFTProviderContext.Provider>
7476
);
7577
}

0 commit comments

Comments
 (0)