Move the current agent to a global and reset RQ queries on agent change (#1946)
This commit is contained in:
@@ -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],
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user