forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-agents.ts
More file actions
69 lines (60 loc) · 2.25 KB
/
Copy pathuse-agents.ts
File metadata and controls
69 lines (60 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use client'
import { useIdentityToken, usePrivy } from '@privy-io/react-auth'
import { useCallback, useEffect, useState } from 'react'
import { listAgents, revokeAgent, type AgentGrant } from '@/lib/api'
export type AgentsState =
| { status: 'loading' }
| { status: 'ready'; agents: AgentGrant[] }
| { status: 'failed' }
export interface UseAgents {
state: AgentsState
revoke: (id: string) => Promise<void>
/** Ask again. The connect dialog polls this while it waits for a handshake. */
refresh: () => void
}
/**
* The agents this account has authorised.
*
* Refetches after a revoke rather than editing the list in place: revoking
* changes what the row says, not whether it exists, and the service is the one
* that decides what it now says. Guessing here would be a second description of
* a revoked grant, drifting from the first.
*/
export function useAgents(): UseAgents {
const { ready, authenticated, getAccessToken } = usePrivy()
const { identityToken } = useIdentityToken()
const [state, setState] = useState<AgentsState>({ status: 'loading' })
const [nonce, setNonce] = useState(0)
const credentials = useCallback(async () => {
const accessToken = await getAccessToken()
if (!accessToken) throw new Error('Not signed in')
return { accessToken, identityToken }
}, [getAccessToken, identityToken])
useEffect(() => {
if (!ready || !authenticated) return
let cancelled = false
void (async () => {
try {
const { agents } = await listAgents(await credentials())
if (!cancelled) setState({ status: 'ready', agents })
} catch {
if (!cancelled) setState({ status: 'failed' })
}
})()
return () => {
cancelled = true
}
}, [ready, authenticated, credentials, nonce])
const revoke = useCallback(
async (id: string) => {
await revokeAgent(await credentials(), id)
setNonce((n) => n + 1)
},
[credentials],
)
// Stable, and that is load-bearing rather than tidy: the connect dialog puts
// this in a setInterval keyed on its identity, so a new function every render
// would tear the interval down and restart it before it could ever fire.
const refresh = useCallback(() => setNonce((n) => n + 1), [])
return { state, revoke, refresh }
}