Search - only enable queries once tab is active (#3471)

* only enable queries once tab is active

* remove hasBeenTrue hook

* make enabled optional
zio/stable
Samuel Newman 2024-04-10 23:32:31 +01:00 committed by GitHub
parent 310d865440
commit 9007810cdb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 61 additions and 16 deletions

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -195,9 +195,11 @@ type SearchResultSlice =
function SearchScreenPostResults({
query,
sort,
active,
}: {
query: string
sort?: 'top' | 'latest'
active: boolean
}) {
const {_} = useLingui()
const {currentAccount} = useSession()
@ -216,7 +218,7 @@ function SearchScreenPostResults({
fetchNextPage,
isFetchingNextPage,
hasNextPage,
} = useSearchPostsQuery({query: augmentedQuery, sort})
} = useSearchPostsQuery({query: augmentedQuery, sort, enabled: active})
const onPullToRefresh = React.useCallback(async () => {
setIsPTR(true)
@ -297,9 +299,19 @@ function SearchScreenPostResults({
)
}
function SearchScreenUserResults({query}: {query: string}) {
function SearchScreenUserResults({
query,
active,
}: {
query: string
active: boolean
}) {
const {_} = useLingui()
const {data: results, isFetched} = useActorSearch(query)
const {data: results, isFetched} = useActorSearch({
query,
enabled: active,
})
return isFetched && results ? (
<>
@ -335,6 +347,7 @@ export function SearchScreenInner({
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
const {hasSession} = useSession()
const {isDesktop} = useWebMediaQueries()
const [activeTab, setActiveTab] = React.useState(0)
const {_} = useLingui()
const isNewSearch = useGate('new_search')
@ -343,6 +356,7 @@ export function SearchScreenInner({
(index: number) => {
setMinimalShellMode(false)
setDrawerSwipeDisabled(index > 0)
setActiveTab(index)
},
[setDrawerSwipeDisabled, setMinimalShellMode],
)
@ -354,22 +368,38 @@ export function SearchScreenInner({
return [
{
title: _(msg`Top`),
component: <SearchScreenPostResults query={query} sort="top" />,
component: (
<SearchScreenPostResults
query={query}
sort="top"
active={activeTab === 0}
/>
),
},
{
title: _(msg`Latest`),
component: <SearchScreenPostResults query={query} sort="latest" />,
component: (
<SearchScreenPostResults
query={query}
sort="latest"
active={activeTab === 1}
/>
),
},
{
title: _(msg`People`),
component: <SearchScreenUserResults query={query} />,
component: (
<SearchScreenUserResults query={query} active={activeTab === 2} />
),
},
]
} else {
return [
{
title: _(msg`People`),
component: <SearchScreenUserResults query={query} />,
component: (
<SearchScreenUserResults query={query} active={activeTab === 0} />
),
},
]
}
@ -378,23 +408,29 @@ export function SearchScreenInner({
return [
{
title: _(msg`Posts`),
component: <SearchScreenPostResults query={query} />,
component: (
<SearchScreenPostResults query={query} active={activeTab === 0} />
),
},
{
title: _(msg`Users`),
component: <SearchScreenUserResults query={query} />,
component: (
<SearchScreenUserResults query={query} active={activeTab === 1} />
),
},
]
} else {
return [
{
title: _(msg`Users`),
component: <SearchScreenUserResults query={query} />,
component: (
<SearchScreenUserResults query={query} active={activeTab === 0} />
),
},
]
}
}
}, [hasSession, isNewSearch, _, query])
}, [hasSession, isNewSearch, _, query, activeTab])
if (hasSession) {
return query ? (