Move the current agent to a global and reset RQ queries on agent change (#1946)

This commit is contained in:
Paul Frazee
2023-11-16 18:26:22 -08:00
committed by GitHub
parent 3043b32468
commit 357c752a21
40 changed files with 218 additions and 260 deletions
+6 -9
View File
@@ -1,14 +1,13 @@
import React from 'react'
import {useQueryClient, useMutation} from '@tanstack/react-query'
import {useSession} from '#/state/session'
import {getAgent} from '#/state/session'
import {STALE} from '#/state/queries'
const fetchHandleQueryKey = (handleOrDid: string) => ['handle', handleOrDid]
const fetchDidQueryKey = (handleOrDid: string) => ['did', handleOrDid]
export function useFetchHandle() {
const {agent} = useSession()
const queryClient = useQueryClient()
return React.useCallback(
@@ -17,23 +16,22 @@ export function useFetchHandle() {
const res = await queryClient.fetchQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: fetchHandleQueryKey(handleOrDid),
queryFn: () => agent.getProfile({actor: handleOrDid}),
queryFn: () => getAgent().getProfile({actor: handleOrDid}),
})
return res.data.handle
}
return handleOrDid
},
[agent, queryClient],
[queryClient],
)
}
export function useUpdateHandleMutation() {
const {agent} = useSession()
const queryClient = useQueryClient()
return useMutation({
mutationFn: async ({handle}: {handle: string}) => {
await agent.updateHandle({handle})
await getAgent().updateHandle({handle})
},
onSuccess(_data, variables) {
queryClient.invalidateQueries({
@@ -44,7 +42,6 @@ export function useUpdateHandleMutation() {
}
export function useFetchDid() {
const {agent} = useSession()
const queryClient = useQueryClient()
return React.useCallback(
@@ -55,13 +52,13 @@ export function useFetchDid() {
queryFn: async () => {
let identifier = handleOrDid
if (!identifier.startsWith('did:')) {
const res = await agent.resolveHandle({handle: identifier})
const res = await getAgent().resolveHandle({handle: identifier})
identifier = res.data.did
}
return identifier
},
})
},
[agent, queryClient],
[queryClient],
)
}