Skip to content

Commit 37abbe5

Browse files
committed
fix: stabilize zone authorization state
1 parent 97a41d5 commit 37abbe5

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/lib/useZoneAuthorization.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useMutation, useQuery } from '@tanstack/react-query'
3+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
44
import type { Hex } from 'viem'
55
import { Storage as ZoneStorage } from 'viem/tempo'
66

@@ -29,6 +29,7 @@ export function useZoneAuthorization(parameters: {
2929
zoneClient: ZoneAuthClientLike | undefined
3030
}) {
3131
const { address, chainId, queryKey, zoneClient } = parameters
32+
const queryClient = useQueryClient()
3233

3334
const statusQuery = useQuery({
3435
enabled: Boolean(address && zoneClient),
@@ -85,8 +86,15 @@ export function useZoneAuthorization(parameters: {
8586

8687
return zoneClient.zone.signAuthorizationToken()
8788
},
88-
onSuccess: async () => {
89-
await statusQuery.refetch()
89+
onSuccess: async (result) => {
90+
if (address) {
91+
queryClient.setQueryData(queryKey, {
92+
account: address,
93+
expiresAt: BigInt(result.authentication.expiresAt),
94+
})
95+
}
96+
97+
await queryClient.invalidateQueries({ queryKey })
9098
},
9199
})
92100

@@ -109,7 +117,10 @@ function withTimeout<T>(promise: Promise<T>, timeoutMs: number) {
109117
reject(error)
110118
}, timeoutMs)
111119

112-
promise.finally(() => clearTimeout(timeout))
120+
promise.then(
121+
() => clearTimeout(timeout),
122+
() => clearTimeout(timeout),
123+
)
113124
}),
114125
])
115126
}

0 commit comments

Comments
 (0)