refactor uri resolution query
This commit is contained in:
@@ -28,14 +28,7 @@ export const RQKEY = (did: string) => ['profile', did]
|
|||||||
export const profileBasicKey = (did: string) => ['profileBasic', did]
|
export const profileBasicKey = (did: string) => ['profileBasic', did]
|
||||||
export const profilesQueryKey = (handles: string[]) => ['profiles', handles]
|
export const profilesQueryKey = (handles: string[]) => ['profiles', handles]
|
||||||
|
|
||||||
export function useProfileQuery({
|
export function useProfileQuery({did}: {did: string | undefined}) {
|
||||||
did,
|
|
||||||
queryClient,
|
|
||||||
}: {
|
|
||||||
did: string | undefined
|
|
||||||
staleTime?: number
|
|
||||||
queryClient: QueryClient
|
|
||||||
}) {
|
|
||||||
// return useQuery({
|
// return useQuery({
|
||||||
// // WARNING
|
// // WARNING
|
||||||
// // this staleTime is load-bearing
|
// // this staleTime is load-bearing
|
||||||
@@ -50,7 +43,7 @@ export function useProfileQuery({
|
|||||||
// },
|
// },
|
||||||
// enabled: !!did,
|
// enabled: !!did,
|
||||||
// })
|
// })
|
||||||
|
const queryClient = useQueryClient()
|
||||||
// TODO Figure out a good staleTime for this. We should refetch on every profile push, because we need to check for
|
// TODO Figure out a good staleTime for this. We should refetch on every profile push, because we need to check for
|
||||||
// blocks
|
// blocks
|
||||||
return useQuery<AppBskyActorDefs.ProfileView>({
|
return useQuery<AppBskyActorDefs.ProfileView>({
|
||||||
@@ -101,7 +94,6 @@ export function usePrefetchProfileQuery() {
|
|||||||
return prefetchProfileQuery
|
return prefetchProfileQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
// We don't need a useQuery hook for this. We never actually use this except for
|
|
||||||
export function cacheProfileBasic(
|
export function cacheProfileBasic(
|
||||||
queryClient: QueryClient,
|
queryClient: QueryClient,
|
||||||
profile: AppBskyActorDefs.ProfileViewBasic,
|
profile: AppBskyActorDefs.ProfileViewBasic,
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import {QueryClient, useQuery, UseQueryResult} from '@tanstack/react-query'
|
import {
|
||||||
|
QueryClient,
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
UseQueryResult,
|
||||||
|
} from '@tanstack/react-query'
|
||||||
import {
|
import {
|
||||||
AtUri,
|
AtUri,
|
||||||
AppBskyActorDefs,
|
AppBskyActorDefs,
|
||||||
@@ -6,6 +11,7 @@ import {
|
|||||||
AppBskyEmbedRecord,
|
AppBskyEmbedRecord,
|
||||||
AppBskyEmbedRecordWithMedia,
|
AppBskyEmbedRecordWithMedia,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {profileBasicKey as RQKEY_PROFILE_BASIC} from 'state/queries/profile'
|
||||||
|
|
||||||
import {getAgent} from '#/state/session'
|
import {getAgent} from '#/state/session'
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
@@ -13,6 +19,37 @@ import {ThreadNode} from './post-thread'
|
|||||||
|
|
||||||
export const RQKEY = (didOrHandle: string) => ['resolved-did', didOrHandle]
|
export const RQKEY = (didOrHandle: string) => ['resolved-did', didOrHandle]
|
||||||
|
|
||||||
|
// export function useResolveUriQuery(uri: string | undefined): UriUseQueryResult {
|
||||||
|
// const urip = new AtUri(uri || '')
|
||||||
|
// const res = useResolveDidQuery(urip.host)
|
||||||
|
// if (res.data) {
|
||||||
|
// urip.host = res.data
|
||||||
|
// return {
|
||||||
|
// ...res,
|
||||||
|
// data: {did: urip.host, uri: urip.toString()},
|
||||||
|
// } as UriUseQueryResult
|
||||||
|
// }
|
||||||
|
// return res as UriUseQueryResult
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// export function useResolveDidQuery(didOrHandle: string | undefined) {
|
||||||
|
// return useQuery<string, Error>({
|
||||||
|
// staleTime: STALE.HOURS.ONE,
|
||||||
|
// queryKey: RQKEY(didOrHandle || ''),
|
||||||
|
// async queryFn() {
|
||||||
|
// if (!didOrHandle) {
|
||||||
|
// return ''
|
||||||
|
// }
|
||||||
|
// if (!didOrHandle.startsWith('did:')) {
|
||||||
|
// const res = await getAgent().resolveHandle({handle: didOrHandle})
|
||||||
|
// didOrHandle = res.data.did
|
||||||
|
// }
|
||||||
|
// return didOrHandle
|
||||||
|
// },
|
||||||
|
// enabled: !!didOrHandle,
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
type UriUseQueryResult = UseQueryResult<{did: string; uri: string}, Error>
|
type UriUseQueryResult = UseQueryResult<{did: string; uri: string}, Error>
|
||||||
export function useResolveUriQuery(uri: string | undefined): UriUseQueryResult {
|
export function useResolveUriQuery(uri: string | undefined): UriUseQueryResult {
|
||||||
const urip = new AtUri(uri || '')
|
const urip = new AtUri(uri || '')
|
||||||
@@ -28,20 +65,42 @@ export function useResolveUriQuery(uri: string | undefined): UriUseQueryResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useResolveDidQuery(didOrHandle: string | undefined) {
|
export function useResolveDidQuery(didOrHandle: string | undefined) {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
return useQuery<string, Error>({
|
return useQuery<string, Error>({
|
||||||
staleTime: STALE.HOURS.ONE,
|
staleTime: STALE.HOURS.ONE,
|
||||||
queryKey: RQKEY(didOrHandle || ''),
|
queryKey: RQKEY(didOrHandle ?? ''),
|
||||||
async queryFn() {
|
queryFn: async () => {
|
||||||
if (!didOrHandle) {
|
if (!didOrHandle) return ''
|
||||||
return ''
|
|
||||||
}
|
const res = await getAgent().resolveHandle({handle: didOrHandle})
|
||||||
if (!didOrHandle.startsWith('did:')) {
|
return res.data.did
|
||||||
const res = await getAgent().resolveHandle({handle: didOrHandle})
|
},
|
||||||
didOrHandle = res.data.did
|
initialData: () => {
|
||||||
}
|
// Return undefined if no did or handle
|
||||||
return didOrHandle
|
if (!didOrHandle) return
|
||||||
|
|
||||||
|
let item: AppBskyActorDefs.ProfileViewBasic | undefined
|
||||||
|
|
||||||
|
if (!didOrHandle?.startsWith('did:')) {
|
||||||
|
// If this is a handle all we have to do is use the query key
|
||||||
|
item = queryClient.getQueryData<AppBskyActorDefs.ProfileViewBasic>(
|
||||||
|
RQKEY_PROFILE_BASIC(didOrHandle),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// If it is a did we need to search the queries data
|
||||||
|
item = queryClient
|
||||||
|
.getQueriesData<AppBskyActorDefs.ProfileViewBasic>({
|
||||||
|
queryKey: ['profileBasic'],
|
||||||
|
exact: false,
|
||||||
|
})
|
||||||
|
.find(q => q[1]?.did === didOrHandle)?.[1]
|
||||||
|
}
|
||||||
|
// Return nothing if we don't find one
|
||||||
|
if (!item) return undefined
|
||||||
|
|
||||||
|
return item.did
|
||||||
},
|
},
|
||||||
enabled: !!didOrHandle,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user