|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 4 | +import { ArrowLeftIcon, ArrowRightIcon } from "lucide-react"; |
| 5 | +import { useTheme } from "next-themes"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { useForm } from "react-hook-form"; |
| 8 | +import type { ThirdwebClient } from "thirdweb"; |
| 9 | +import { defineChain } from "thirdweb/chains"; |
| 10 | +import { CheckoutWidget, useActiveWalletChain } from "thirdweb/react"; |
| 11 | +import { z } from "zod"; |
| 12 | +import { SingleNetworkSelector } from "@/components/blocks/NetworkSelectors"; |
| 13 | +import { TokenSelector } from "@/components/blocks/TokenSelector"; |
| 14 | +import { Button } from "@/components/ui/button"; |
| 15 | +import { DecimalInput } from "@/components/ui/decimal-input"; |
| 16 | +import { |
| 17 | + Dialog, |
| 18 | + DialogContent, |
| 19 | + DialogDescription, |
| 20 | + DialogHeader, |
| 21 | + DialogTitle, |
| 22 | +} from "@/components/ui/dialog"; |
| 23 | +import { |
| 24 | + Form, |
| 25 | + FormControl, |
| 26 | + FormField, |
| 27 | + FormItem, |
| 28 | + FormLabel, |
| 29 | + FormMessage, |
| 30 | +} from "@/components/ui/form"; |
| 31 | +import { getSDKTheme } from "@/utils/sdk-component-theme"; |
| 32 | +import { WalletAddress } from "../wallet-address"; |
| 33 | + |
| 34 | +const formSchema = z.object({ |
| 35 | + chainId: z.number({ |
| 36 | + required_error: "Chain is required", |
| 37 | + }), |
| 38 | + token: z.object( |
| 39 | + { |
| 40 | + chainId: z.number(), |
| 41 | + address: z.string(), |
| 42 | + symbol: z.string(), |
| 43 | + name: z.string(), |
| 44 | + decimals: z.number(), |
| 45 | + }, |
| 46 | + { |
| 47 | + required_error: "Token is required", |
| 48 | + }, |
| 49 | + ), |
| 50 | + amount: z |
| 51 | + .string() |
| 52 | + .min(1, "Amount is required") |
| 53 | + .refine((value) => { |
| 54 | + const num = Number(value); |
| 55 | + return !Number.isNaN(num) && num > 0; |
| 56 | + }, "Amount must be greater than 0"), |
| 57 | +}); |
| 58 | + |
| 59 | +type FormData = z.infer<typeof formSchema>; |
| 60 | + |
| 61 | +type FundWalletModalProps = { |
| 62 | + open: boolean; |
| 63 | + onOpenChange: (open: boolean) => void; |
| 64 | + title: string; |
| 65 | + description: string; |
| 66 | + recipientAddress: string; |
| 67 | + client: ThirdwebClient; |
| 68 | + defaultChainId?: number; |
| 69 | + checkoutWidgetTitle?: string; |
| 70 | +}; |
| 71 | + |
| 72 | +export function FundWalletModal(props: FundWalletModalProps) { |
| 73 | + return ( |
| 74 | + <Dialog open={props.open} onOpenChange={props.onOpenChange}> |
| 75 | + <DialogContent className="p-0 gap-0 !w-full !max-w-md"> |
| 76 | + <FundWalletModalContent |
| 77 | + open={props.open} |
| 78 | + onOpenChange={props.onOpenChange} |
| 79 | + title={props.title} |
| 80 | + description={props.description} |
| 81 | + recipientAddress={props.recipientAddress} |
| 82 | + client={props.client} |
| 83 | + defaultChainId={props.defaultChainId} |
| 84 | + checkoutWidgetTitle={props.checkoutWidgetTitle} |
| 85 | + /> |
| 86 | + </DialogContent> |
| 87 | + </Dialog> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function FundWalletModalContent(props: FundWalletModalProps) { |
| 92 | + const [step, setStep] = useState<"form" | "checkout">("form"); |
| 93 | + const activeChain = useActiveWalletChain(); |
| 94 | + const { theme } = useTheme(); |
| 95 | + |
| 96 | + const form = useForm<FormData>({ |
| 97 | + defaultValues: { |
| 98 | + chainId: props.defaultChainId || activeChain?.id || 1, |
| 99 | + token: undefined, |
| 100 | + amount: "0.1", |
| 101 | + }, |
| 102 | + mode: "onChange", |
| 103 | + resolver: zodResolver(formSchema), |
| 104 | + }); |
| 105 | + |
| 106 | + const selectedChainId = form.watch("chainId"); |
| 107 | + |
| 108 | + return ( |
| 109 | + <div> |
| 110 | + {step === "form" ? ( |
| 111 | + <Form {...form}> |
| 112 | + <form |
| 113 | + onSubmit={form.handleSubmit(() => { |
| 114 | + setStep("checkout"); |
| 115 | + })} |
| 116 | + > |
| 117 | + <DialogHeader className="p-4 lg:p-6"> |
| 118 | + <DialogTitle>{props.title}</DialogTitle> |
| 119 | + <DialogDescription>{props.description}</DialogDescription> |
| 120 | + </DialogHeader> |
| 121 | + |
| 122 | + <div className="space-y-4 px-4 lg:px-6 pb-8"> |
| 123 | + <div> |
| 124 | + <h3 className="text-sm font-medium"> Recipient</h3> |
| 125 | + <WalletAddress |
| 126 | + address={props.recipientAddress} |
| 127 | + client={props.client} |
| 128 | + className="py-1 h-auto" |
| 129 | + iconClassName="size-4" |
| 130 | + /> |
| 131 | + </div> |
| 132 | + |
| 133 | + <FormField |
| 134 | + control={form.control} |
| 135 | + name="chainId" |
| 136 | + render={({ field }) => ( |
| 137 | + <FormItem> |
| 138 | + <FormLabel>Chain</FormLabel> |
| 139 | + <FormControl> |
| 140 | + <SingleNetworkSelector |
| 141 | + className="bg-card" |
| 142 | + chainId={field.value} |
| 143 | + onChange={(token) => { |
| 144 | + field.onChange(token); |
| 145 | + form.resetField("token", { |
| 146 | + defaultValue: undefined, |
| 147 | + }); |
| 148 | + }} |
| 149 | + client={props.client} |
| 150 | + placeholder="Select a chain" |
| 151 | + disableDeprecated |
| 152 | + disableTestnets |
| 153 | + disableChainId |
| 154 | + /> |
| 155 | + </FormControl> |
| 156 | + <FormMessage /> |
| 157 | + </FormItem> |
| 158 | + )} |
| 159 | + /> |
| 160 | + |
| 161 | + <FormField |
| 162 | + control={form.control} |
| 163 | + name="token" |
| 164 | + render={({ field }) => { |
| 165 | + return ( |
| 166 | + <FormItem> |
| 167 | + <FormLabel>Token</FormLabel> |
| 168 | + <FormControl> |
| 169 | + <TokenSelector |
| 170 | + className="bg-card" |
| 171 | + disableAddress |
| 172 | + selectedToken={ |
| 173 | + field.value |
| 174 | + ? { |
| 175 | + chainId: field.value.chainId, |
| 176 | + address: field.value.address, |
| 177 | + } |
| 178 | + : undefined |
| 179 | + } |
| 180 | + onChange={field.onChange} |
| 181 | + chainId={selectedChainId} |
| 182 | + client={props.client} |
| 183 | + placeholder="Select a token" |
| 184 | + enabled={!!selectedChainId} |
| 185 | + addNativeTokenIfMissing={true} |
| 186 | + showCheck={true} |
| 187 | + /> |
| 188 | + </FormControl> |
| 189 | + <FormMessage /> |
| 190 | + </FormItem> |
| 191 | + ); |
| 192 | + }} |
| 193 | + /> |
| 194 | + |
| 195 | + <FormField |
| 196 | + control={form.control} |
| 197 | + name="amount" |
| 198 | + render={({ field }) => ( |
| 199 | + <FormItem> |
| 200 | + <FormLabel>Amount</FormLabel> |
| 201 | + <FormControl> |
| 202 | + <div className="relative"> |
| 203 | + <DecimalInput |
| 204 | + className="bg-card !text-xl h-auto font-semibold" |
| 205 | + value={field.value} |
| 206 | + onChange={field.onChange} |
| 207 | + placeholder="0.1" |
| 208 | + /> |
| 209 | + {form.watch("token") && ( |
| 210 | + <div className="absolute right-4 top-1/2 -translate-y-1/2 text-sm text-muted-foreground"> |
| 211 | + {form.watch("token").symbol} |
| 212 | + </div> |
| 213 | + )} |
| 214 | + </div> |
| 215 | + </FormControl> |
| 216 | + <FormMessage /> |
| 217 | + </FormItem> |
| 218 | + )} |
| 219 | + /> |
| 220 | + </div> |
| 221 | + |
| 222 | + <div className="flex justify-end gap-3 p-4 lg:p-6 bg-card border-t rounded-b-lg"> |
| 223 | + <Button |
| 224 | + variant="outline" |
| 225 | + onClick={() => props.onOpenChange(false)} |
| 226 | + > |
| 227 | + Cancel |
| 228 | + </Button> |
| 229 | + <Button type="submit" className="gap-2"> |
| 230 | + Next |
| 231 | + <ArrowRightIcon className="w-4 h-4" /> |
| 232 | + </Button> |
| 233 | + </div> |
| 234 | + </form> |
| 235 | + </Form> |
| 236 | + ) : ( |
| 237 | + <div> |
| 238 | + <DialogHeader className="sr-only"> |
| 239 | + <DialogTitle>{props.title}</DialogTitle> |
| 240 | + <DialogDescription>{props.description}</DialogDescription> |
| 241 | + </DialogHeader> |
| 242 | + |
| 243 | + <div> |
| 244 | + <CheckoutWidget |
| 245 | + client={props.client} |
| 246 | + // eslint-disable-next-line no-restricted-syntax |
| 247 | + chain={defineChain(form.getValues("chainId"))} |
| 248 | + tokenAddress={form.getValues("token").address as `0x${string}`} |
| 249 | + amount={form.getValues("amount")} |
| 250 | + seller={props.recipientAddress as `0x${string}`} |
| 251 | + showThirdwebBranding={false} |
| 252 | + className="!w-full !max-w-full !min-w-0 !rounded-b-none !border-none" |
| 253 | + theme={getSDKTheme(theme === "dark" ? "dark" : "light")} |
| 254 | + name={props.checkoutWidgetTitle} |
| 255 | + /> |
| 256 | + </div> |
| 257 | + |
| 258 | + <div className="flex justify-end gap-3 p-4 lg:p-6 bg-card border-t rounded-b-lg"> |
| 259 | + <Button |
| 260 | + variant="outline" |
| 261 | + onClick={() => setStep("form")} |
| 262 | + className="gap-2" |
| 263 | + > |
| 264 | + <ArrowLeftIcon className="size-4 text-muted-foreground" /> |
| 265 | + Back |
| 266 | + </Button> |
| 267 | + |
| 268 | + <Button |
| 269 | + className="gap-2" |
| 270 | + variant="outline" |
| 271 | + onClick={() => props.onOpenChange(false)} |
| 272 | + > |
| 273 | + Cancel |
| 274 | + </Button> |
| 275 | + </div> |
| 276 | + </div> |
| 277 | + )} |
| 278 | + </div> |
| 279 | + ); |
| 280 | +} |
0 commit comments