Skip to content

[SDK] Fix caching issue with headless UI when custom resolvers are used #5657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fluffy-pets-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Fix caching issues for headless component; improve code coverage
14 changes: 13 additions & 1 deletion packages/thirdweb/src/react/web/ui/prebuilt/Chain/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import type { JSX } from "react";
import { getChainMetadata } from "../../../../../chains/utils.js";
import type { ThirdwebClient } from "../../../../../client/client.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { resolveScheme } from "../../../../../utils/ipfs.js";
import { useChainContext } from "./provider.js";

Expand Down Expand Up @@ -121,7 +122,18 @@ export function ChainIcon({
}: ChainIconProps) {
const { chain } = useChainContext();
const iconQuery = useQuery({
queryKey: ["_internal_chain_icon_", chain.id] as const,
queryKey: [
"_internal_chain_icon_",
chain.id,
{
resolver:
typeof iconResolver === "string"
? iconResolver
: typeof iconResolver === "function"
? getFunctionId(iconResolver)
: undefined,
},
] as const,
queryFn: async () => {
if (typeof iconResolver === "string") {
return iconResolver;
Expand Down
45 changes: 36 additions & 9 deletions packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { describe, expect, it } from "vitest";
import { render, screen, waitFor } from "~test/react-render.js";
import { ethereum } from "../../../../../chains/chain-definitions/ethereum.js";
import { defineChain } from "../../../../../chains/utils.js";
import { ChainName } from "./name.js";
import { ChainName, fetchChainName } from "./name.js";
import { ChainProvider } from "./provider.js";

describe.runIf(process.env.TW_SECRET_KEY)("ChainName component", () => {
it("should return the correct chain name, if the name exists in the chain object", () => {
it("should return the correct chain name, if the name exists in the chain object", async () => {
render(
<ChainProvider chain={ethereum}>
<ChainName />
</ChainProvider>,
);
waitFor(() =>
await waitFor(() =>
expect(
screen.getByText("Ethereum", {
exact: true,
Expand All @@ -22,13 +22,13 @@ describe.runIf(process.env.TW_SECRET_KEY)("ChainName component", () => {
);
});

it("should return the correct chain name, if the name is loaded from the server", () => {
it("should return the correct chain name, if the name is loaded from the server", async () => {
render(
<ChainProvider chain={defineChain(1)}>
<ChainName />
</ChainProvider>,
);
waitFor(() =>
await waitFor(() =>
expect(
screen.getByText("Ethereum Mainnet", {
exact: true,
Expand All @@ -38,13 +38,13 @@ describe.runIf(process.env.TW_SECRET_KEY)("ChainName component", () => {
);
});

it("should return the correct FORMATTED chain name", () => {
it("should return the correct FORMATTED chain name", async () => {
render(
<ChainProvider chain={ethereum}>
<ChainName formatFn={(str: string) => `${str}-formatted`} />
</ChainProvider>,
);
waitFor(() =>
await waitFor(() =>
expect(
screen.getByText("Ethereum-formatted", {
exact: true,
Expand All @@ -54,14 +54,14 @@ describe.runIf(process.env.TW_SECRET_KEY)("ChainName component", () => {
);
});

it("should fallback properly when fail to resolve chain name", () => {
it("should fallback properly when fail to resolve chain name", async () => {
render(
<ChainProvider chain={defineChain(-1)}>
<ChainName fallbackComponent={<span>oops</span>} />
</ChainProvider>,
);

waitFor(() =>
await waitFor(() =>
expect(
screen.getByText("oops", {
exact: true,
Expand All @@ -70,4 +70,31 @@ describe.runIf(process.env.TW_SECRET_KEY)("ChainName component", () => {
).toBeInTheDocument(),
);
});

it("fetchChainName should respect nameResolver as a string", async () => {
const res = await fetchChainName({
chain: ethereum,
nameResolver: "eth_mainnet",
});
expect(res).toBe("eth_mainnet");
});

it("fetchChainName should respect nameResolver as a non-async function", async () => {
const res = await fetchChainName({
chain: ethereum,
nameResolver: () => "eth_mainnet",
});
expect(res).toBe("eth_mainnet");
});

it("fetchChainName should respect nameResolver as an async function", async () => {
const res = await fetchChainName({
chain: ethereum,
nameResolver: async () => {
await new Promise((resolve) => setTimeout(resolve, 2000));
return "eth_mainnet";
},
});
expect(res).toBe("eth_mainnet");
});
});
48 changes: 35 additions & 13 deletions packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import type React from "react";
import type { JSX } from "react";
import type { Chain } from "../../../../../chains/types.js";
import { getChainMetadata } from "../../../../../chains/utils.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { useChainContext } from "./provider.js";

/**
Expand Down Expand Up @@ -155,19 +157,19 @@
}: ChainNameProps) {
const { chain } = useChainContext();
const nameQuery = useQuery({
queryKey: ["_internal_chain_name_", chain.id] as const,
queryFn: async () => {
if (typeof nameResolver === "string") {
return nameResolver;
}
if (typeof nameResolver === "function") {
return nameResolver();
}
if (chain.name) {
return chain.name;
}
return getChainMetadata(chain).then((data) => data.name);
},
queryKey: [
"_internal_chain_name_",
chain.id,
{
resolver:
typeof nameResolver === "string"
? nameResolver

Check warning on line 166 in packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.tsx#L166

Added line #L166 was not covered by tests
: typeof nameResolver === "function"
? getFunctionId(nameResolver)

Check warning on line 168 in packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/prebuilt/Chain/name.tsx#L168

Added line #L168 was not covered by tests
: undefined,
},
] as const,
queryFn: async () => fetchChainName({ chain, nameResolver }),
...queryOptions,
});

Expand All @@ -183,3 +185,23 @@

return <span {...restProps}>{displayValue}</span>;
}

/**
* @internal Exported for tests only
*/
export async function fetchChainName(props: {
chain: Chain;
nameResolver?: string | (() => string) | (() => Promise<string>);
}) {
const { nameResolver, chain } = props;
if (typeof nameResolver === "string") {
return nameResolver;
}
if (typeof nameResolver === "function") {
return nameResolver();
}
if (chain.name) {
return chain.name;
}
return getChainMetadata(chain).then((data) => data.name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import type { JSX } from "react";
import type { ThirdwebContract } from "../../../../../contract/contract.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { useNFTContext } from "./provider.js";
import { getNFTInfo } from "./utils.js";

Expand Down Expand Up @@ -100,7 +101,7 @@
typeof descriptionResolver === "string"
? descriptionResolver
: typeof descriptionResolver === "function"
? descriptionResolver.toString()
? getFunctionId(descriptionResolver)

Check warning on line 104 in packages/thirdweb/src/react/web/ui/prebuilt/NFT/description.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L104 was not covered by tests
: undefined,
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/thirdweb/src/react/web/ui/prebuilt/NFT/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import type { JSX } from "react";
import type { ThirdwebContract } from "../../../../../contract/contract.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { MediaRenderer } from "../../MediaRenderer/MediaRenderer.js";
import type { MediaRendererProps } from "../../MediaRenderer/types.js";
import { useNFTContext } from "./provider.js";
Expand Down Expand Up @@ -140,7 +141,7 @@
typeof mediaResolver === "object"
? mediaResolver
: typeof mediaResolver === "function"
? mediaResolver.toString()
? getFunctionId(mediaResolver)

Check warning on line 144 in packages/thirdweb/src/react/web/ui/prebuilt/NFT/media.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L144 was not covered by tests
: undefined,
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/thirdweb/src/react/web/ui/prebuilt/NFT/name.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { type UseQueryOptions, useQuery } from "@tanstack/react-query";
import type { JSX } from "react";
import type { ThirdwebContract } from "../../../../../contract/contract.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { useNFTContext } from "./provider.js";
import { getNFTInfo } from "./utils.js";

Expand Down Expand Up @@ -102,7 +103,7 @@
typeof nameResolver === "string"
? nameResolver
: typeof nameResolver === "function"
? nameResolver.toString()
? getFunctionId(nameResolver)

Check warning on line 106 in packages/thirdweb/src/react/web/ui/prebuilt/NFT/name.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L106 was not covered by tests
: undefined,
},
],
Expand Down
15 changes: 14 additions & 1 deletion packages/thirdweb/src/react/web/ui/prebuilt/Token/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getChainMetadata } from "../../../../../chains/utils.js";
import { NATIVE_TOKEN_ADDRESS } from "../../../../../constants/addresses.js";
import { getContract } from "../../../../../contract/contract.js";
import { getContractMetadata } from "../../../../../extensions/common/read/getContractMetadata.js";
import { getFunctionId } from "../../../../../utils/function-id.js";
import { resolveScheme } from "../../../../../utils/ipfs.js";
import { useTokenContext } from "./provider.js";

Expand Down Expand Up @@ -117,7 +118,19 @@ export function TokenIcon({
}: TokenIconProps) {
const { address, client, chain } = useTokenContext();
const iconQuery = useQuery({
queryKey: ["_internal_token_icon_", chain.id, address] as const,
queryKey: [
"_internal_token_icon_",
chain.id,
address,
{
resolver:
typeof iconResolver === "string"
? iconResolver
: typeof iconResolver === "function"
? getFunctionId(iconResolver)
: undefined,
},
] as const,
queryFn: async () => {
if (typeof iconResolver === "string") {
return iconResolver;
Expand Down
Loading
Loading