1- import { zodResolver } from "@hookform/resolvers/zod" ;
21import { memo , useCallback , useState } from "react" ;
3- import { FormProvider , useForm } from "react-hook-form" ;
4- import type { AbiFunction , Address } from "viem" ;
5- import { isAddress } from "viem" ;
6- import { z } from "zod" ;
2+ import { FormProvider } from "react-hook-form" ;
3+ import type { AbiFunction } from "viem" ;
74import { AbiItemFormWithPreview } from "../../abi-form/abi-item-form-with-preview.js" ;
8- import type { AddressData } from "../../address-autocomplete-input.js" ;
95import {
106 AccordionContent ,
117 AccordionItem ,
128 AccordionTrigger ,
139} from "../../shadcn/accordion.js" ;
14- import type { ExecutionParams } from "../types.js" ;
15- import { useFunctionExecution } from "../use-function-execution.js" ;
16- import { DefaultResultDisplay } from "./result-display.js" ;
1710import {
1811 ActionButtons ,
1912 ConnectWalletAlert ,
13+ DefaultResultDisplay ,
2014 MsgSenderInput ,
21- } from "./shared-components.js" ;
15+ } from "../shared/components.js" ;
16+ import { useMsgSenderForm } from "../shared/form-utils.js" ;
17+ import type { BaseExecutionProps , ExecutionParams } from "../shared/types.js" ;
18+ import { useFunctionExecution } from "../shared/use-function-execution.js" ;
19+ import { isWriteFunction } from "../shared/utils.js" ;
2220
23- const executionFormSchema = z . object ( {
24- msgSender : z
25- . string ( )
26- . refine (
27- ( val ) => {
28- if ( ! val ) return true ;
29- return isAddress ( val ) ;
30- } ,
31- { message : "Invalid address format" } ,
32- )
33- . optional ( ) ,
34- } ) ;
35-
36- interface FunctionItemProps {
21+ interface FunctionItemProps extends BaseExecutionProps {
3722 func : AbiFunction ;
3823 index : number ;
39- address : Address ;
40- chainId : number ;
41- sender ?: Address ;
42- addresses ?: AddressData [ ] ;
43- requiresConnection : boolean ;
44- isConnected : boolean ;
4524 onQuery : ( params : ExecutionParams ) => Promise < `0x${string } `> ;
4625 onWrite : ( params : ExecutionParams ) => Promise < `0x${string } `> ;
4726 onSimulate ?: ( params : ExecutionParams ) => Promise < `0x${string } `> ;
48- addressRenderer ?: ( address : Address ) => React . ReactNode ;
49- onHashClick ?: ( hash : string ) => void ;
5027}
5128
5229export const FunctionItem = memo (
@@ -68,19 +45,9 @@ export const FunctionItem = memo(
6845 const [ callData , setCallData ] = useState < string > ( "" ) ;
6946 const { result, isSimulating, isExecuting, simulate, execute } =
7047 useFunctionExecution ( ) ;
48+ const { form, msgSender } = useMsgSenderForm ( sender ) ;
7149
72- const form = useForm ( {
73- mode : "onChange" ,
74- resolver : zodResolver ( executionFormSchema ) ,
75- defaultValues : {
76- msgSender : "" ,
77- } ,
78- } ) ;
79-
80- const msgSender = form . watch ( ) . msgSender || "" ;
81-
82- const isWrite =
83- func . stateMutability !== "view" && func . stateMutability !== "pure" ;
50+ const isWrite = isWriteFunction ( func ) ;
8451
8552 const handleCallDataChange = useCallback (
8653 ( newCallData : string | undefined ) => {
@@ -93,7 +60,7 @@ export const FunctionItem = memo(
9360 simulate ( {
9461 abiFunction : func ,
9562 callData,
96- msgSender : msgSender ? ( msgSender as Address ) : undefined ,
63+ msgSender,
9764 onQuery,
9865 onWrite,
9966 onSimulate,
@@ -104,7 +71,7 @@ export const FunctionItem = memo(
10471 execute ( {
10572 abiFunction : func ,
10673 callData,
107- msgSender : msgSender ? ( msgSender as Address ) : undefined ,
74+ msgSender,
10875 onQuery,
10976 onWrite,
11077 onSimulate,
@@ -128,10 +95,6 @@ export const FunctionItem = memo(
12895 < div className = "mt-4 space-y-6" >
12996 { isWrite && < MsgSenderInput /> }
13097
131- { isWrite && requiresConnection && ! isConnected && (
132- < ConnectWalletAlert />
133- ) }
134-
13598 < AbiItemFormWithPreview
13699 addresses = { addresses }
137100 key = { func . name }
@@ -153,6 +116,10 @@ export const FunctionItem = memo(
153116 }
154117 />
155118
119+ { isWrite && requiresConnection && ! isConnected && (
120+ < ConnectWalletAlert />
121+ ) }
122+
156123 < ActionButtons
157124 isWrite = { isWrite }
158125 callData = { callData }
0 commit comments