Search - only enable queries once tab is active (#3471)
* only enable queries once tab is active * remove hasBeenTrue hook * make enabled optional
This commit is contained in:
parent
310d865440
commit
9007810cdb
3 changed files with 61 additions and 16 deletions
|
@ -5,19 +5,25 @@ import {STALE} from '#/state/queries'
|
|||
import {getAgent} from '#/state/session'
|
||||
|
||||
const RQKEY_ROOT = 'actor-search'
|
||||
export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]
|
||||
export const RQKEY = (query: string) => [RQKEY_ROOT, query]
|
||||
|
||||
export function useActorSearch(prefix: string) {
|
||||
export function useActorSearch({
|
||||
query,
|
||||
enabled,
|
||||
}: {
|
||||
query: string
|
||||
enabled?: boolean
|
||||
}) {
|
||||
return useQuery<AppBskyActorDefs.ProfileView[]>({
|
||||
staleTime: STALE.MINUTES.ONE,
|
||||
queryKey: RQKEY(prefix || ''),
|
||||
queryKey: RQKEY(query || ''),
|
||||
async queryFn() {
|
||||
const res = await getAgent().searchActors({
|
||||
q: prefix,
|
||||
q: query,
|
||||
})
|
||||
return res.data.actors
|
||||
},
|
||||
enabled: !!prefix,
|
||||
enabled: enabled && !!query,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,11 @@ const searchPostsQueryKey = ({query, sort}: {query: string; sort?: string}) => [
|
|||
export function useSearchPostsQuery({
|
||||
query,
|
||||
sort,
|
||||
enabled,
|
||||
}: {
|
||||
query: string
|
||||
sort?: 'top' | 'latest'
|
||||
enabled?: boolean
|
||||
}) {
|
||||
return useInfiniteQuery<
|
||||
AppBskyFeedSearchPosts.OutputSchema,
|
||||
|
@ -47,6 +49,7 @@ export function useSearchPostsQuery({
|
|||
},
|
||||
initialPageParam: undefined,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
enabled,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue