@@ -13,6 +13,7 @@ import {
1313} from "@/components/ui/table" ;
1414import { getThirdwebClient } from "@/constants/thirdweb.server" ;
1515import { zodResolver } from "@hookform/resolvers/zod" ;
16+ import { useMutation } from "@tanstack/react-query" ;
1617import {
1718 type ColumnDef ,
1819 flexRender ,
@@ -95,8 +96,30 @@ export function DataTable({
9596 "Successfully deployed contract" ,
9697 "Failed to deploy contract" ,
9798 ) ;
98- const [ isAddingRow , setIsAddingRow ] = useState ( false ) ;
99- const [ addRowError , setAddRowError ] = useState < string | null > ( null ) ;
99+
100+ const addRowMutation = useMutation ( {
101+ mutationFn : async ( chain : { chainId : number ; name : string } ) => {
102+ const code = await eth_getCode (
103+ getRpcClient ( {
104+ client : getThirdwebClient ( ) ,
105+ // eslint-disable-next-line no-restricted-syntax
106+ chain : defineChain ( chain . chainId ) ,
107+ } ) ,
108+ { address : coreContract . address } ,
109+ ) ;
110+
111+ const newRow : CrossChain = {
112+ id : chain . chainId ,
113+ network : chain . name ,
114+ chainId : chain . chainId ,
115+ status : code ?. length > 2 ? "DEPLOYED" : "NOT_DEPLOYED" ,
116+ } ;
117+
118+ if ( ! customChainData . some ( ( row ) => row . chainId === chain . chainId ) ) {
119+ setCustomChainData ( ( prevData ) => [ ...prevData , newRow ] ) ;
120+ }
121+ } ,
122+ } ) ;
100123
101124 const [ customChainData , setCustomChainData ] = useState < CrossChain [ ] > ( ( ) => {
102125 try {
@@ -290,43 +313,6 @@ export function DataTable({
290313 }
291314 } ;
292315
293- const handleAddRow = async ( chain : { chainId : number ; name : string } ) => {
294- const existingChain = customChainData . find (
295- ( row ) => row . chainId === chain . chainId ,
296- ) ;
297- if ( existingChain ) {
298- return ;
299- }
300-
301- setIsAddingRow ( true ) ;
302-
303- try {
304- const code = await eth_getCode (
305- getRpcClient ( {
306- client : getThirdwebClient ( ) ,
307- // eslint-disable-next-line no-restricted-syntax
308- chain : defineChain ( chain . chainId ) ,
309- } ) ,
310- { address : coreContract . address } ,
311- ) ;
312-
313- const newRow : CrossChain = {
314- id : chain . chainId ,
315- network : chain . name ,
316- chainId : chain . chainId ,
317- status : code ?. length > 2 ? "DEPLOYED" : "NOT_DEPLOYED" ,
318- } ;
319-
320- if ( ! customChainData . some ( ( row ) => row . chainId === chain . chainId ) ) {
321- setCustomChainData ( ( prevData ) => [ ...prevData , newRow ] ) ;
322- }
323- } catch {
324- setAddRowError ( "Error while adding the selected chain!" ) ;
325- } finally {
326- setIsAddingRow ( false ) ;
327- }
328- } ;
329-
330316 return (
331317 < Form { ...form } >
332318 < form >
@@ -378,20 +364,22 @@ export function DataTable({
378364 </ TableContainer >
379365 </ div >
380366
381- { isAddingRow && ! addRowError && (
367+ { addRowMutation . isPending && (
382368 < div className = "my-4 animate-pulse text-center text-blue-500" >
383369 Processing...
384370 </ div >
385371 ) }
386372
387- { addRowError && (
388- < div className = "my-4 text-center text-red-500" > { addRowError } </ div >
373+ { addRowMutation . isError && (
374+ < div className = "my-4 text-center text-red-500" >
375+ Error while adding selected chain!
376+ </ div >
389377 ) }
390378
391379 < div className = "mt-4" >
392380 < SingleNetworkSelector
393- onAddRow = { handleAddRow }
394- isAddingRow = { isAddingRow }
381+ onAddRow = { addRowMutation . mutate }
382+ isAddingRow = { addRowMutation . isPending }
395383 className = "w-full"
396384 />
397385 </ div >
0 commit comments