De-closure actor-autocomplete
This commit is contained in:
@@ -1,29 +1,30 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {AppBskyActorDefs} from '@atproto/api'
|
import {AppBskyActorDefs} from '@atproto/api'
|
||||||
import {useQuery, useQueryClient} from '@tanstack/react-query'
|
import {
|
||||||
|
useQuery,
|
||||||
|
useQueryClient,
|
||||||
|
QueryFunctionContext,
|
||||||
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {getAgent} from '#/state/session'
|
import {getAgent} from '#/state/session'
|
||||||
import {useMyFollowsQuery} from '#/state/queries/my-follows'
|
import {useMyFollowsQuery} from '#/state/queries/my-follows'
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
|
|
||||||
export const RQKEY = (prefix: string) => ['actor-autocomplete', prefix]
|
export const RQKEY = (prefix: string, limit = 8) =>
|
||||||
|
['actor-autocomplete', prefix, limit] as [string, string, number]
|
||||||
|
|
||||||
|
interface ActorAutocompleteQueryMeta {
|
||||||
|
follows: AppBskyActorDefs.ProfileViewBasic[] | undefined
|
||||||
|
}
|
||||||
export function useActorAutocompleteQuery(prefix: string) {
|
export function useActorAutocompleteQuery(prefix: string) {
|
||||||
const {data: follows, isFetching} = useMyFollowsQuery()
|
const {data: follows, isFetching} = useMyFollowsQuery()
|
||||||
|
|
||||||
return useQuery<AppBskyActorDefs.ProfileViewBasic[]>({
|
return useQuery<AppBskyActorDefs.ProfileViewBasic[]>({
|
||||||
staleTime: STALE.MINUTES.ONE,
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: RQKEY(prefix || ''),
|
queryKey: RQKEY(prefix || ''),
|
||||||
async queryFn() {
|
queryFn: actorAutoCompleteQueryFn,
|
||||||
const res = prefix
|
meta: {follows},
|
||||||
? await getAgent().searchActorsTypeahead({
|
|
||||||
term: prefix,
|
|
||||||
limit: 8,
|
|
||||||
})
|
|
||||||
: undefined
|
|
||||||
return computeSuggestions(prefix, follows, res?.data.actors)
|
|
||||||
},
|
|
||||||
enabled: !isFetching,
|
enabled: !isFetching,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -40,12 +41,9 @@ export function useActorAutocompleteFn() {
|
|||||||
try {
|
try {
|
||||||
res = await queryClient.fetchQuery({
|
res = await queryClient.fetchQuery({
|
||||||
staleTime: STALE.MINUTES.ONE,
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: RQKEY(query || ''),
|
queryKey: RQKEY(query || '', limit),
|
||||||
queryFn: () =>
|
meta: {follows},
|
||||||
getAgent().searchActorsTypeahead({
|
queryFn: actorAutoCompleteQueryFn,
|
||||||
term: query,
|
|
||||||
limit,
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('useActorSearch: searchActorsTypeahead failed', {
|
logger.error('useActorSearch: searchActorsTypeahead failed', {
|
||||||
@@ -53,13 +51,27 @@ export function useActorAutocompleteFn() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return res
|
||||||
return computeSuggestions(query, follows, res?.data.actors)
|
|
||||||
},
|
},
|
||||||
[follows, queryClient],
|
[follows, queryClient],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function actorAutoCompleteQueryFn({
|
||||||
|
queryKey,
|
||||||
|
meta,
|
||||||
|
}: QueryFunctionContext) {
|
||||||
|
const [_, prefix, limit] = queryKey as ReturnType<typeof RQKEY>
|
||||||
|
const {follows} = meta as any as ActorAutocompleteQueryMeta
|
||||||
|
const res = prefix
|
||||||
|
? await getAgent().searchActorsTypeahead({
|
||||||
|
term: prefix,
|
||||||
|
limit,
|
||||||
|
})
|
||||||
|
: undefined
|
||||||
|
return computeSuggestions(prefix, follows, res?.data.actors)
|
||||||
|
}
|
||||||
|
|
||||||
function computeSuggestions(
|
function computeSuggestions(
|
||||||
prefix: string,
|
prefix: string,
|
||||||
follows: AppBskyActorDefs.ProfileViewBasic[] | undefined,
|
follows: AppBskyActorDefs.ProfileViewBasic[] | undefined,
|
||||||
|
|||||||
Reference in New Issue
Block a user