|
| 1 | +"use client"; |
| 2 | +import { payAppThirdwebClient } from "app/pay/constants"; |
| 3 | +import { useTheme } from "next-themes"; |
| 4 | +import { useEffect } from "react"; |
| 5 | +import { createThirdwebClient, NATIVE_TOKEN_ADDRESS, toTokens } from "thirdweb"; |
| 6 | +import { AutoConnect, CheckoutWidget } from "thirdweb/react"; |
| 7 | +import { checksumAddress } from "thirdweb/utils"; |
| 8 | +import { useV5DashboardChain } from "@/hooks/chains/v5-adapter"; |
| 9 | + |
| 10 | +export function PayPageWidget({ |
| 11 | + chainId, |
| 12 | + recipientAddress, |
| 13 | + paymentLinkId, |
| 14 | + amount, |
| 15 | + token, |
| 16 | + name, |
| 17 | + image, |
| 18 | + redirectUri, |
| 19 | + theme, |
| 20 | + purchaseData, |
| 21 | + clientId, |
| 22 | +}: { |
| 23 | + chainId: number; |
| 24 | + recipientAddress: string; |
| 25 | + paymentLinkId?: string; |
| 26 | + amount?: bigint; |
| 27 | + token: { name: string; symbol: string; address: string; decimals: number }; |
| 28 | + name?: string; |
| 29 | + image?: string; |
| 30 | + redirectUri?: string; |
| 31 | + clientId: string; |
| 32 | + theme?: "light" | "dark"; |
| 33 | + purchaseData: Record<string, unknown> | undefined; |
| 34 | +}) { |
| 35 | + const { theme: browserTheme, setTheme } = useTheme(); |
| 36 | + |
| 37 | + // eslint-disable-next-line no-restricted-syntax |
| 38 | + useEffect(() => { |
| 39 | + if (theme) { |
| 40 | + setTheme(theme); |
| 41 | + } |
| 42 | + }, [theme, setTheme]); |
| 43 | + const chain = useV5DashboardChain(chainId); |
| 44 | + |
| 45 | + return ( |
| 46 | + <> |
| 47 | + <AutoConnect |
| 48 | + client={ |
| 49 | + clientId ? createThirdwebClient({ clientId }) : payAppThirdwebClient |
| 50 | + } |
| 51 | + /> |
| 52 | + <CheckoutWidget |
| 53 | + amount={amount ? toTokens(amount, token.decimals) : "0"} |
| 54 | + chain={chain} |
| 55 | + client={ |
| 56 | + clientId ? createThirdwebClient({ clientId }) : payAppThirdwebClient |
| 57 | + } |
| 58 | + image={image} |
| 59 | + name={name} |
| 60 | + onSuccess={() => { |
| 61 | + if (!redirectUri) return; |
| 62 | + const url = new URL(redirectUri); |
| 63 | + return window.open(url.toString()); |
| 64 | + }} |
| 65 | + paymentLinkId={paymentLinkId} |
| 66 | + purchaseData={purchaseData} |
| 67 | + seller={checksumAddress(recipientAddress)} |
| 68 | + theme={theme ?? (browserTheme === "light" ? "light" : "dark")} |
| 69 | + tokenAddress={ |
| 70 | + token.address === NATIVE_TOKEN_ADDRESS |
| 71 | + ? undefined |
| 72 | + : checksumAddress(token.address) |
| 73 | + } |
| 74 | + /> |
| 75 | + </> |
| 76 | + ); |
| 77 | +} |
0 commit comments