|
| 1 | +'use client' |
| 2 | +import * as React from 'react' |
| 3 | +import { Label } from '@/components/ui/label' |
| 4 | +import { Textarea } from '@/components/ui/textarea' |
| 5 | +import { Button } from '@/components/ui/button' |
| 6 | +import CodeBlock from '@/components/ui/code-block' |
| 7 | +import { NetworkSelect } from '@/components/ui/network-select' |
| 8 | +import { Input } from '@/components/ui/input' |
| 9 | +import { ExampleTransactions } from '../../components/ui/examples' |
| 10 | +import { useCalldataForm } from './useCalldataForm' |
| 11 | +import { CalldataFormProps } from './types' |
| 12 | + |
| 13 | +const PATH = 'calldata' |
| 14 | + |
| 15 | +export default function DecodingForm({ decoded, contractAddress, chainID, data, isLoading }: CalldataFormProps) { |
| 16 | + const { form, onSubmit } = useCalldataForm({ |
| 17 | + data, |
| 18 | + chainID, |
| 19 | + contractAddress, |
| 20 | + }) |
| 21 | + |
| 22 | + return ( |
| 23 | + <div className="grid h-full items-stretch gap-6 grid-cols-1 lg:grid-cols-[1fr_200px]"> |
| 24 | + <div className="md:order-1 flex flex-col space-y-4"> |
| 25 | + <form onSubmit={form.handleSubmit(onSubmit)}> |
| 26 | + <div className="flex w-full gap-2 flex-col"> |
| 27 | + <Textarea |
| 28 | + className="min-h-[100px] flex-1" |
| 29 | + placeholder="Paste transaction calldata or click on examples" |
| 30 | + {...form.register('data', { required: true })} |
| 31 | + /> |
| 32 | + <div className="flex w-full lg:items-center gap-2 flex-col lg:flex-row"> |
| 33 | + <div> |
| 34 | + <NetworkSelect |
| 35 | + defaultValue={form.watch('chainID')} |
| 36 | + onValueChange={(value) => { |
| 37 | + form.setValue('chainID', value) |
| 38 | + }} |
| 39 | + /> |
| 40 | + </div> |
| 41 | + <Input |
| 42 | + className="flex-1" |
| 43 | + placeholder="Optional: contract address" |
| 44 | + defaultValue={contractAddress} |
| 45 | + {...form.register('contractAddress')} |
| 46 | + /> |
| 47 | + <div className="flex gap-2"> |
| 48 | + <Button type="submit" disabled={isLoading}> |
| 49 | + {isLoading ? 'Decoding...' : 'Decode'} |
| 50 | + </Button> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + </form> |
| 55 | + |
| 56 | + {decoded && ( |
| 57 | + <div className="grid gap-6 h-full"> |
| 58 | + <div className="flex flex-col gap-2 min-h-[40vh] lg:min-h-[initial]"> |
| 59 | + <Label>Decoded calldata:</Label> |
| 60 | + <CodeBlock language="json" value={JSON.stringify(decoded, null, 2)} readonly lineNumbers={false} /> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + |
| 66 | + <div className="md:order-2"> |
| 67 | + <ExampleTransactions path={PATH} /> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + ) |
| 71 | +} |
0 commit comments