|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type React from "react"; |
| 4 | +import { useId, useState } from "react"; |
| 5 | +import { defineChain } from "thirdweb/chains"; |
| 6 | +import { BridgeNetworkSelector } from "@/components/blocks/NetworkSelectors"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import { Label } from "@/components/ui/label"; |
| 9 | +import { TokenSelector } from "@/components/ui/TokenSelector"; |
| 10 | +import { THIRDWEB_CLIENT } from "@/lib/client"; |
| 11 | +import type { TokenMetadata } from "@/lib/types"; |
| 12 | +import type { X402PlaygroundOptions } from "./types"; |
| 13 | + |
| 14 | +export function X402LeftSection(props: { |
| 15 | + options: X402PlaygroundOptions; |
| 16 | + setOptions: React.Dispatch<React.SetStateAction<X402PlaygroundOptions>>; |
| 17 | +}) { |
| 18 | + const { options, setOptions } = props; |
| 19 | + |
| 20 | + // Local state for chain and token selection |
| 21 | + const [selectedChain, setSelectedChain] = useState<number | undefined>(() => { |
| 22 | + return options.chain?.id; |
| 23 | + }); |
| 24 | + |
| 25 | + const [selectedToken, setSelectedToken] = useState< |
| 26 | + { chainId: number; address: string } | undefined |
| 27 | + >(() => { |
| 28 | + if (options.tokenAddress && options.chain?.id) { |
| 29 | + return { |
| 30 | + address: options.tokenAddress, |
| 31 | + chainId: options.chain.id, |
| 32 | + }; |
| 33 | + } |
| 34 | + return undefined; |
| 35 | + }); |
| 36 | + |
| 37 | + const chainId = useId(); |
| 38 | + const tokenId = useId(); |
| 39 | + const amountId = useId(); |
| 40 | + const payToId = useId(); |
| 41 | + |
| 42 | + const handleChainChange = (chainId: number) => { |
| 43 | + setSelectedChain(chainId); |
| 44 | + // Clear token selection when chain changes |
| 45 | + setSelectedToken(undefined); |
| 46 | + |
| 47 | + setOptions((v) => ({ |
| 48 | + ...v, |
| 49 | + chain: defineChain(chainId), |
| 50 | + tokenAddress: "0x0000000000000000000000000000000000000000" as const, |
| 51 | + tokenSymbol: "", |
| 52 | + tokenDecimals: 18, |
| 53 | + })); |
| 54 | + }; |
| 55 | + |
| 56 | + const handleTokenChange = (token: TokenMetadata) => { |
| 57 | + setSelectedToken({ |
| 58 | + address: token.address, |
| 59 | + chainId: selectedChain!, |
| 60 | + }); |
| 61 | + |
| 62 | + setOptions((v) => ({ |
| 63 | + ...v, |
| 64 | + tokenAddress: token.address as `0x${string}`, |
| 65 | + tokenSymbol: token.symbol ?? "", |
| 66 | + tokenDecimals: token.decimals ?? 18, |
| 67 | + })); |
| 68 | + }; |
| 69 | + |
| 70 | + const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 71 | + setOptions((v) => ({ |
| 72 | + ...v, |
| 73 | + amount: e.target.value, |
| 74 | + })); |
| 75 | + }; |
| 76 | + |
| 77 | + const handlePayToChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 78 | + setOptions((v) => ({ |
| 79 | + ...v, |
| 80 | + payTo: e.target.value as `0x${string}`, |
| 81 | + })); |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <div className="space-y-6"> |
| 86 | + <div> |
| 87 | + <h2 className="mb-4 text-xl font-semibold">Configuration</h2> |
| 88 | + <div className="space-y-4"> |
| 89 | + {/* Chain selection */} |
| 90 | + <div className="flex flex-col gap-2"> |
| 91 | + <Label htmlFor={chainId}>Chain</Label> |
| 92 | + <BridgeNetworkSelector |
| 93 | + chainId={selectedChain} |
| 94 | + onChange={handleChainChange} |
| 95 | + placeholder="Select a chain" |
| 96 | + className="bg-card" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + |
| 100 | + {/* Token selection - only show if chain is selected */} |
| 101 | + {selectedChain && ( |
| 102 | + <div className="flex flex-col gap-2"> |
| 103 | + <Label htmlFor={tokenId}>Token</Label> |
| 104 | + <TokenSelector |
| 105 | + chainId={selectedChain} |
| 106 | + client={THIRDWEB_CLIENT} |
| 107 | + enabled={true} |
| 108 | + onChange={handleTokenChange} |
| 109 | + placeholder="Select a token" |
| 110 | + selectedToken={selectedToken} |
| 111 | + className="bg-card" |
| 112 | + /> |
| 113 | + </div> |
| 114 | + )} |
| 115 | + |
| 116 | + {/* Amount input */} |
| 117 | + <div className="flex flex-col gap-2"> |
| 118 | + <Label htmlFor={amountId}>Amount</Label> |
| 119 | + <Input |
| 120 | + id={amountId} |
| 121 | + type="text" |
| 122 | + placeholder="0.01" |
| 123 | + value={options.amount} |
| 124 | + onChange={handleAmountChange} |
| 125 | + className="bg-card" |
| 126 | + /> |
| 127 | + {options.tokenSymbol && ( |
| 128 | + <p className="text-sm text-muted-foreground"> |
| 129 | + Amount in {options.tokenSymbol} |
| 130 | + </p> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + |
| 134 | + {/* Pay To input */} |
| 135 | + <div className="flex flex-col gap-2"> |
| 136 | + <Label htmlFor={payToId}>Pay To Address</Label> |
| 137 | + <Input |
| 138 | + id={payToId} |
| 139 | + type="text" |
| 140 | + placeholder="0x..." |
| 141 | + value={options.payTo} |
| 142 | + onChange={handlePayToChange} |
| 143 | + className="bg-card" |
| 144 | + /> |
| 145 | + <p className="text-sm text-muted-foreground"> |
| 146 | + The wallet address that will receive the payment |
| 147 | + </p> |
| 148 | + </div> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </div> |
| 152 | + ); |
| 153 | +} |
0 commit comments