-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
CNCT-2620 <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces new headless components for wallet management in the `thirdweb` library, enhancing user interface options for wallet context, icons, and names. ### Detailed summary - Added `WalletProvider`, `WalletIcon`, and `WalletName` components. - Updated `ChainProvider` to mark it as `@beta`. - Introduced utility functions `getQueryKeys` for both chain and wallet components. - Updated documentation and tests for new wallet components. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
12 changed files
with
576 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"thirdweb": minor | ||
--- | ||
|
||
Add headless components for Wallets: WalletProvider, WalletIcon and WalletName |
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
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
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
30 changes: 30 additions & 0 deletions
30
packages/thirdweb/src/react/web/ui/prebuilt/Wallet/icon.test.tsx
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,30 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { render, waitFor } from "~test/react-render.js"; | ||
import { WalletIcon, fetchWalletImage } from "./icon.js"; | ||
import { WalletProvider } from "./provider.js"; | ||
|
||
describe("WalletIcon", () => { | ||
it("should fetch wallet image", async () => { | ||
expect(await fetchWalletImage({ id: "io.metamask" })).toBe( | ||
"data:image/webp;base64,UklGRsgFAABXRUJQVlA4ILwFAAAwHgCdASqAAIAAPm0ylEWkIyIXTGzMQAbEoAzQSPDffG8qhx7LpfeDUfnDee/tP6gP2u9Yf0a/7L0/+pc3nivc0JlTXBvMVYW1Wt7JbdRJ1a5UhpH9+sdhAvGug20WzG+wRD+P/7/raQ1NqZ5EfcOwosQsP7NRVflPNArBLCbFJWE3EvTLNOmMfR2f+r+E2y6y1lWanc3n9RtF4lYbcW38t3UG/W2jXng4loXFOy3Vjm/r/1KHwXlpAuhAIzK/XF+ChayRFhBD+lbRrgQ3CSVjdmXw6agYPoMQ+tpjPUN/3qdGcvnN1sKWt57OGozQVSjMu0aA58MPKVWiAP77l9VfptKhZ7in3+r1DqZZQEYqO1p+MENah/WS/QQDvPuew8P0OXfahcjbCg7h4QEEEjDyfoFUbQHdw+meAsydryxVs/Ij+eB619Uj/sMgtwIifIyTieCT/hdsDmpLqGD+vOIArZfzSkordRkngojxVgvWRht84IaCHCs5Dn208UHFmyQE4KCxHef1iLYeAEPxnIAovGHIl7rCjiYE12obAM3ZCnt+RFqcT3q2rorCswc4/8f8TOhCSo63/dszpMkNQonGwtyhAFnm1EKqLPDcy99ggKQL95VYePoHcv8sHOG5zJ7lircX7VPpxkloNVWnlp/drJjQrp0h5BsOnqYn756+wcFw5qapAOnYCKUHujsoSz2BlLs1702rFpvi1iVczo2aO9GN1TaM3zBqMX0NN0YEU8pz/3+xwkK5M5q+Qb4FNJeugjdOp2kcYoOCbyAg/0OyGswYHPwDN/opV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y/sqC6qZ/8+tsTAHzziGF6NKoaD0FVhZ2CER5AonJdBz5sg0rcD7arFe96uzujFDCQAAAA", | ||
); | ||
}); | ||
|
||
it("should throw error if WalletId is not supported", async () => { | ||
await expect(() => | ||
// @ts-ignore For test | ||
fetchWalletImage({ id: "__undefined__" }), | ||
).rejects.toThrowError("Wallet with id __undefined__ not found"); | ||
}); | ||
|
||
it("should render an image", async () => { | ||
const { container } = render( | ||
<WalletProvider id="io.cosmostation"> | ||
<WalletIcon /> | ||
</WalletProvider>, | ||
); | ||
await waitFor(() => { | ||
expect(container.querySelector("img")).not.toBe(null); | ||
}); | ||
}); | ||
}); |
120 changes: 120 additions & 0 deletions
120
packages/thirdweb/src/react/web/ui/prebuilt/Wallet/icon.tsx
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,120 @@ | ||
"use client"; | ||
|
||
import { type UseQueryOptions, useQuery } from "@tanstack/react-query"; | ||
import type { JSX } from "react"; | ||
import { getWalletInfo } from "../../../../../wallets/__generated__/getWalletInfo.js"; | ||
import type { WalletId } from "../../../../../wallets/wallet-types.js"; | ||
import { useWalletContext } from "./provider.js"; | ||
|
||
export interface WalletIconProps | ||
extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, "src"> { | ||
/** | ||
* This component will be shown while the icon of the wallet is being fetched | ||
* If not passed, the component will return `null`. | ||
* | ||
* You can/should pass a loading sign or spinner to this prop. | ||
* @example | ||
* ```tsx | ||
* <WalletIcon loadingComponent={<Spinner />} /> | ||
* ``` | ||
*/ | ||
loadingComponent?: JSX.Element; | ||
/** | ||
* This component will be shown if the icon fails to be retreived | ||
* If not passed, the component will return `null`. | ||
* | ||
* You can/should pass a descriptive text/component to this prop, indicating that the | ||
* icon was not fetched succesfully | ||
* @example | ||
* ```tsx | ||
* <WalletIcon fallbackComponent={<span>Failed to load</span>} | ||
* /> | ||
* ``` | ||
*/ | ||
fallbackComponent?: JSX.Element; | ||
/** | ||
* Optional `useQuery` params | ||
*/ | ||
queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">; | ||
} | ||
|
||
/** | ||
* This component tries to resolve the icon of a given wallet, then return an image. | ||
* @returns an <img /> with the src of the wallet icon | ||
* | ||
* @example | ||
* ### Basic usage | ||
* ```tsx | ||
* import { WalletProvider, WalletIcon } from "thirdweb/react"; | ||
* | ||
* <WalletProvider id="io.metamask"> | ||
* <WalletIcon /> | ||
* </WalletProvider> | ||
* ``` | ||
* | ||
* Result: An <img /> component with the src of the icon | ||
* ```html | ||
* <img src="metamask-icon.png" /> | ||
* ``` | ||
* | ||
* ### Show a loading sign while the icon is being loaded | ||
* ```tsx | ||
* <WalletIcon loadingComponent={<Spinner />} /> | ||
* ``` | ||
* | ||
* ### Fallback to a dummy image if the wallet icon fails to resolve | ||
* ```tsx | ||
* <WalletIcon fallbackComponent={<img src="blank-image.png" />} /> | ||
* ``` | ||
* | ||
* ### Usage with queryOptions | ||
* WalletIcon uses useQuery() from tanstack query internally. | ||
* It allows you to pass a custom queryOptions of your choice for more control of the internal fetching logic | ||
* ```tsx | ||
* <WalletIcon queryOptions={{ enabled: someLogic, retry: 3, }} /> | ||
* ``` | ||
* | ||
* @component | ||
* @wallet | ||
* @beta | ||
*/ | ||
export function WalletIcon({ | ||
loadingComponent, | ||
fallbackComponent, | ||
queryOptions, | ||
...restProps | ||
}: WalletIconProps) { | ||
const imageQuery = useWalletIcon({ queryOptions }); | ||
if (imageQuery.isLoading) { | ||
return loadingComponent || null; | ||
} | ||
if (!imageQuery.data) { | ||
return fallbackComponent || null; | ||
} | ||
return <img src={imageQuery.data} {...restProps} alt={restProps.alt} />; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
function useWalletIcon(props: { | ||
queryOptions?: Omit<UseQueryOptions<string>, "queryFn" | "queryKey">; | ||
}) { | ||
const { id } = useWalletContext(); | ||
const imageQuery = useQuery({ | ||
queryKey: ["walletIcon", id], | ||
queryFn: async () => fetchWalletImage({ id }), | ||
...props.queryOptions, | ||
}); | ||
return imageQuery; | ||
} | ||
|
||
/** | ||
* @internal Exported for tests only | ||
*/ | ||
export async function fetchWalletImage(props: { | ||
id: WalletId; | ||
}) { | ||
const image_src = await getWalletInfo(props.id, true); | ||
return image_src; | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/thirdweb/src/react/web/ui/prebuilt/Wallet/name.test.tsx
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,55 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { render, waitFor } from "~test/react-render.js"; | ||
import { getFunctionId } from "../../../../../utils/function-id.js"; | ||
import { WalletName, fetchWalletName, getQueryKeys } from "./name.js"; | ||
import { WalletProvider } from "./provider.js"; | ||
|
||
describe.runIf(process.env.TW_SECRET_KEY)("WalletName", () => { | ||
it("fetchWalletName: should fetch wallet name from id", async () => { | ||
const name = await fetchWalletName({ id: "io.metamask" }); | ||
expect(name).toBe("MetaMask"); | ||
}); | ||
|
||
it("fetchWalletName should throw error if failed to get name", async () => { | ||
// @ts-ignore for test | ||
await expect(() => fetchWalletName({ id: "test___" })).rejects.toThrowError( | ||
"Wallet with id test___ not found", | ||
); | ||
}); | ||
|
||
it("fetchWalletName should work with formatFn", async () => { | ||
const formatFn = (str: string) => `${str} Wallet`; | ||
expect(await fetchWalletName({ id: "io.metamask", formatFn })).toBe( | ||
"MetaMask Wallet", | ||
); | ||
}); | ||
|
||
it("getQueryKeys should work without a formatFn", () => { | ||
expect(getQueryKeys({ id: "ai.hacken" })).toStrictEqual([ | ||
"walletName", | ||
"ai.hacken", | ||
]); | ||
}); | ||
|
||
it("getQueryKeys should work WITH a formatFn", () => { | ||
const fn = (str: string) => `test:${str}`; | ||
const fnId = getFunctionId(fn); | ||
expect(getQueryKeys({ id: "ai.hacken", formatFn: fn })).toStrictEqual([ | ||
"walletName", | ||
"ai.hacken", | ||
{ resolver: fnId }, | ||
]); | ||
}); | ||
|
||
it("should render a span", async () => { | ||
const { container } = render( | ||
<WalletProvider id="io.metamask"> | ||
<WalletName /> | ||
</WalletProvider>, | ||
); | ||
|
||
await waitFor(() => { | ||
expect(container.querySelector("span")).not.toBe(null); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.