1+ import { useEffect } from "react" ;
2+ import { Hex } from "viem" ;
13import { Button } from "../ui/Button" ;
24import { useSetupSession } from "./useSetupSession" ;
35import { ConnectedClient } from "../common" ;
4- import { useEffect } from "react" ;
56import { useSessionClient } from "../useSessionClient" ;
67import { useShowQueryError } from "../errors/useShowQueryError" ;
78import { useShowMutationError } from "../errors/useShowMutationError" ;
89import { StepContentProps } from "./common" ;
10+ import { usePrerequisites } from "./usePrerequisites" ;
911
1012export type Props = StepContentProps & {
1113 userClient : ConnectedClient ;
1214 registerSpender : boolean ;
1315 registerDelegation : boolean ;
16+ sessionAddress ?: Hex ;
1417} ;
1518
1619export function Session ( { isActive, isExpanded, userClient, registerSpender, registerDelegation } : Props ) {
1720 const sessionClient = useShowQueryError ( useSessionClient ( userClient . account . address ) ) ;
1821 const setup = useShowMutationError ( useSetupSession ( { userClient } ) ) ;
1922 const hasSession = ! registerDelegation && ! registerDelegation ;
23+ const { data : prerequisites } = usePrerequisites ( userClient . account . address ) ;
24+ const { hasAllowance, hasGasBalance, hasQuarryGasBalance } = prerequisites ?? { } ;
2025
2126 useEffect ( ( ) => {
2227 // There seems to be a tanstack-query bug(?) where multiple simultaneous renders loses
2328 // state between the two mutations. They're not treated as shared state but rather
2429 // individual mutations, even though the keys match. And the one we want the status of
2530 // seems to stay pending. This is sorta resolved by triggering this after a timeout.
2631 const timer = setTimeout ( ( ) => {
27- if ( isActive && setup . status === "idle" && sessionClient . data && ! hasSession ) {
32+ if (
33+ isActive &&
34+ setup . status === "idle" &&
35+ sessionClient . data &&
36+ ! hasSession &&
37+ ( hasAllowance || hasGasBalance || hasQuarryGasBalance )
38+ ) {
2839 setup . mutate ( {
2940 sessionClient : sessionClient . data ,
3041 registerSpender,
@@ -33,7 +44,17 @@ export function Session({ isActive, isExpanded, userClient, registerSpender, reg
3344 }
3445 } ) ;
3546 return ( ) => clearTimeout ( timer ) ;
36- } , [ hasSession , isActive , registerDelegation , registerSpender , sessionClient , setup ] ) ;
47+ } , [
48+ hasSession ,
49+ isActive ,
50+ registerDelegation ,
51+ registerSpender ,
52+ sessionClient ,
53+ setup ,
54+ hasAllowance ,
55+ hasGasBalance ,
56+ hasQuarryGasBalance ,
57+ ] ) ;
3758
3859 return (
3960 < div className = "flex flex-col gap-4" >
0 commit comments