Skip to content

Commit

Permalink
Search - only enable queries once tab is active (#3471)
Browse files Browse the repository at this point in the history
* only enable queries once tab is active

* remove hasBeenTrue hook

* make enabled optional
  • Loading branch information
mozzius authored Apr 10, 2024
1 parent 310d865 commit 9007810
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
16 changes: 11 additions & 5 deletions src/state/queries/actor-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/state/queries/search-posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,6 +49,7 @@ export function useSearchPostsQuery({
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
enabled,
})
}

Expand Down
58 changes: 47 additions & 11 deletions src/view/screens/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ type SearchResultSlice =
function SearchScreenPostResults({
query,
sort,
active,
}: {
query: string
sort?: 'top' | 'latest'
active: boolean
}) {
const {_} = useLingui()
const {currentAccount} = useSession()
Expand All @@ -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)
Expand Down Expand Up @@ -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 ? (
<>
Expand Down Expand Up @@ -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')
Expand All @@ -343,6 +356,7 @@ export function SearchScreenInner({
(index: number) => {
setMinimalShellMode(false)
setDrawerSwipeDisabled(index > 0)
setActiveTab(index)
},
[setDrawerSwipeDisabled, setMinimalShellMode],
)
Expand All @@ -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} />
),
},
]
}
Expand All @@ -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 ? (
Expand Down

0 comments on commit 9007810

Please sign in to comment.