33import { CoinsIcon , ExternalLinkIcon , EyeIcon , LoaderIcon , SendIcon } from "lucide-react" ;
44import Link from "next/link" ;
55import { useParams , useSearchParams } from "next/navigation" ;
6+ import { useRouter } from "next/navigation" ;
67import { toast } from "sonner" ;
7- import {
8- Abi ,
9- AbiFunction ,
10- AbiParameter ,
11- Address ,
12- Hex ,
13- decodeEventLog ,
14- encodeFunctionData ,
15- stringify ,
16- toFunctionHash ,
17- } from "viem" ;
8+ import { Abi , AbiFunction , AbiParameter , Address , Hex , decodeEventLog , encodeFunctionData , stringify } from "viem" ;
189import { useAccount , useConfig , usePublicClient } from "wagmi" ;
1910import { waitForTransactionReceipt , writeContract } from "wagmi/actions" ;
2011import { z } from "zod" ;
21- import { useCallback , useState } from "react" ;
12+ import { useCallback , useEffect , useState } from "react" ;
2213import { useForm } from "react-hook-form" ;
2314import { zodResolver } from "@hookform/resolvers/zod" ;
2415import { encodeSystemCall } from "@latticexyz/world/internal" ;
@@ -36,8 +27,8 @@ import {
3627import { Input } from "../../../../../../../components/ui/Input" ;
3728import { ScrollIntoViewLink } from "../../../../../components/ScrollIntoViewLink" ;
3829import { useChain } from "../../../../../hooks/useChain" ;
39- import { useHashState } from "../../../../../hooks/useHashState" ;
4030import { blockExplorerTransactionUrl } from "../../../../../utils/blockExplorerTransactionUrl" ;
31+ import { getFunctionElementId } from "../../../../../utils/getFunctionElementId" ;
4132import { encodeFunctionArgs } from "../../explore/utils/encodeFunctionArgs" ;
4233
4334export enum FunctionType {
@@ -49,6 +40,7 @@ type Props = {
4940 worldAbi : Abi ;
5041 functionAbi : AbiFunction ;
5142 systemId ?: Hex ;
43+ useSearchParamsArgs : boolean ;
5244} ;
5345
5446type DecodedEvent = {
@@ -86,7 +78,7 @@ const getInputPlaceholder = (input: AbiParameter): string => {
8678 return `[${ componentsString } ]` ;
8779} ;
8880
89- export function FunctionField ( { systemId, worldAbi, functionAbi } : Props ) {
81+ export function FunctionField ( { systemId, worldAbi, functionAbi, useSearchParamsArgs } : Props ) {
9082 const searchParams = useSearchParams ( ) ;
9183 const publicClient = usePublicClient ( ) ;
9284 const operationType : FunctionType =
@@ -98,22 +90,31 @@ export function FunctionField({ systemId, worldAbi, functionAbi }: Props) {
9890 const account = useAccount ( ) ;
9991 const { worldAddress } = useParams ( ) ;
10092 const { id : chainId } = useChain ( ) ;
101- const [ functionHash ] = useHashState ( ) ;
10293 const [ isLoading , setIsLoading ] = useState ( false ) ;
10394 const [ result , setResult ] = useState < string > ( ) ;
10495 const [ events , setEvents ] = useState < DecodedEvent [ ] > ( ) ;
10596 const [ txHash , setTxHash ] = useState < Hex > ( ) ;
10697 const txUrl = blockExplorerTransactionUrl ( { hash : txHash , chainId } ) ;
10798 const inputLabels = functionAbi . inputs . map ( getInputLabel ) ;
99+ const router = useRouter ( ) ;
108100
109101 const form = useForm < z . infer < typeof formSchema > > ( {
110102 resolver : zodResolver ( formSchema ) ,
111103 defaultValues : {
112- inputs : functionHash === toFunctionHash ( functionAbi ) ? JSON . parse ( searchParams . get ( "args" ) || "[]" ) : [ ] ,
113- value : functionHash === toFunctionHash ( functionAbi ) ? searchParams . get ( "value" ) ?? "" : "" ,
104+ inputs : useSearchParamsArgs ? JSON . parse ( searchParams . get ( "args" ) || "[]" ) : [ ] ,
105+ value : useSearchParamsArgs ? searchParams . get ( "value" ) ?? "" : "" ,
114106 } ,
115107 } ) ;
116108
109+ useEffect ( ( ) => {
110+ if ( useSearchParamsArgs ) {
111+ const params = new URLSearchParams ( searchParams . toString ( ) ) ;
112+ params . delete ( "args" ) ;
113+ params . delete ( "value" ) ;
114+ router . replace ( `?${ params . toString ( ) } ${ window . location . hash } ` ) ;
115+ }
116+ } , [ useSearchParamsArgs , searchParams , router ] ) ;
117+
117118 const getShareableUrl = useCallback ( ( ) => {
118119 const values = form . watch ( ) ;
119120 const params = new URLSearchParams ( searchParams . toString ( ) ) ;
@@ -131,11 +132,11 @@ export function FunctionField({ systemId, worldAbi, functionAbi }: Props) {
131132 }
132133
133134 const url = new URL ( window . location . href ) ;
134- url . hash = toFunctionHash ( functionAbi ) ;
135+ url . hash = getFunctionElementId ( functionAbi , systemId ) ;
135136 url . search = params . toString ( ) ;
136137
137138 return url . toString ( ) ;
138- } , [ form , functionAbi , searchParams ] ) ;
139+ } , [ form , functionAbi , searchParams , systemId ] ) ;
139140
140141 const onSubmit = useCallback (
141142 async ( values : z . infer < typeof formSchema > ) => {
@@ -232,13 +233,13 @@ export function FunctionField({ systemId, worldAbi, functionAbi }: Props) {
232233 < Form { ...form } >
233234 < form
234235 onSubmit = { form . handleSubmit ( onSubmit ) }
235- id = { toFunctionHash ( functionAbi ) }
236+ id = { getFunctionElementId ( functionAbi , systemId ) }
236237 className = "space-y-4 rounded border border-white/10 bg-black/20 p-3 pb-4"
237238 >
238239 < div className = "flex items-center justify-between" >
239240 < h3 className = "font-semibold" >
240241 < ScrollIntoViewLink
241- elementId = { toFunctionHash ( functionAbi ) }
242+ elementId = { getFunctionElementId ( functionAbi , systemId ) }
242243 className = "group inline-flex items-center hover:no-underline"
243244 >
244245 < span className = "text-orange-500 group-hover:underline" > { functionAbi . name } </ span >
0 commit comments