|
| 1 | +"use client"; |
| 2 | +import { useQuery } from "@tanstack/react-query"; |
| 3 | +import type { ThirdwebClient } from "../../../client/client.js"; |
| 4 | +import { getLastAuthProvider } from "../../../react/core/utils/storage.js"; |
| 5 | +import { webLocalStorage } from "../../../utils/storage/webStorage.js"; |
| 6 | +import { isEcosystemWallet } from "../../../wallets/ecosystem/is-ecosystem-wallet.js"; |
| 7 | +import { ClientScopedStorage } from "../../../wallets/in-app/core/authentication/client-scoped-storage.js"; |
| 8 | +import type { Ecosystem } from "../../../wallets/in-app/core/wallet/types.js"; |
| 9 | +import { useActiveWallet } from "../../core/hooks/wallets/useActiveWallet.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Creates a link to another thirdweb-supported site with wallet connection parameters. |
| 13 | + * |
| 14 | + * @note The target site must support the connected wallet (ecosystem or in-app). |
| 15 | + * |
| 16 | + * @param {Object} props - The props to pass to the anchor tag |
| 17 | + * @param {String} props.href - The URL of the site to link to |
| 18 | + * @param {ThirdwebClient} props.client - The current site's thirdweb client |
| 19 | + * @param {Ecosystem} [props.ecosystem] - The ecosystem to use for the wallet connection in the target site |
| 20 | + * @param {React.ReactNode} props.children - The content to render inside the link |
| 21 | + * |
| 22 | + * @example |
| 23 | + * ```tsx |
| 24 | + * import { SiteLink } from "thirdweb/react"; |
| 25 | + * |
| 26 | + * <SiteLink href="https://thirdweb.com" client={thirdwebClient} ecosystem={{ id: "ecosystem.thirdweb" }}> |
| 27 | + * Visit Site |
| 28 | + * </SiteLink> |
| 29 | + * ``` |
| 30 | + */ |
| 31 | +export function SiteLink({ |
| 32 | + href, |
| 33 | + client, |
| 34 | + ecosystem, |
| 35 | + children, |
| 36 | + ...props |
| 37 | +}: { |
| 38 | + href: string; |
| 39 | + client: ThirdwebClient; |
| 40 | + ecosystem?: Ecosystem; |
| 41 | + children: React.ReactNode; |
| 42 | +} & Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href">) { |
| 43 | + if (!client.clientId) { |
| 44 | + throw new Error("The SiteLink client must have a clientId"); |
| 45 | + } |
| 46 | + |
| 47 | + const activeWallet = useActiveWallet(); |
| 48 | + const walletId = activeWallet?.id; |
| 49 | + |
| 50 | + const { |
| 51 | + data: { authProvider, authCookie } = {}, |
| 52 | + } = useQuery({ |
| 53 | + queryKey: ["site-link", walletId, href, client.clientId, ecosystem], |
| 54 | + enabled: |
| 55 | + activeWallet && (isEcosystemWallet(activeWallet) || walletId === "inApp"), |
| 56 | + queryFn: async () => { |
| 57 | + const storage = new ClientScopedStorage({ |
| 58 | + storage: webLocalStorage, |
| 59 | + clientId: client.clientId, |
| 60 | + ecosystem, |
| 61 | + }); |
| 62 | + |
| 63 | + const authProvider = await getLastAuthProvider(webLocalStorage); |
| 64 | + const authCookie = await storage.getAuthCookie(); |
| 65 | + |
| 66 | + return { authProvider, authCookie }; |
| 67 | + }, |
| 68 | + }); |
| 69 | + |
| 70 | + const url = new URL(href); |
| 71 | + if (walletId) { |
| 72 | + url.searchParams.set("walletId", walletId); |
| 73 | + } |
| 74 | + if (authProvider) { |
| 75 | + url.searchParams.set("authProvider", authProvider); |
| 76 | + } |
| 77 | + if (authCookie) { |
| 78 | + url.searchParams.set("authCookie", authCookie); |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <a href={encodeURI(url.toString())} {...props}> |
| 83 | + {children} |
| 84 | + </a> |
| 85 | + ); |
| 86 | +} |
0 commit comments