Search - only enable queries once tab is active (#3471)
* only enable queries once tab is active * remove hasBeenTrue hook * make enabled optionalzio/stable
parent
310d865440
commit
9007810cdb
|
@ -5,19 +5,25 @@ import {STALE} from '#/state/queries'
|
||||||
import {getAgent} from '#/state/session'
|
import {getAgent} from '#/state/session'
|
||||||
|
|
||||||
const RQKEY_ROOT = 'actor-search'
|
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[]>({
|
return useQuery<AppBskyActorDefs.ProfileView[]>({
|
||||||
staleTime: STALE.MINUTES.ONE,
|
staleTime: STALE.MINUTES.ONE,
|
||||||
queryKey: RQKEY(prefix || ''),
|
queryKey: RQKEY(query || ''),
|
||||||
async queryFn() {
|
async queryFn() {
|
||||||
const res = await getAgent().searchActors({
|
const res = await getAgent().searchActors({
|
||||||
q: prefix,
|
q: query,
|
||||||
})
|
})
|
||||||
return res.data.actors
|
return res.data.actors
|
||||||
},
|
},
|
||||||
enabled: !!prefix,
|
enabled: enabled && !!query,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,9 +19,11 @@ const searchPostsQueryKey = ({query, sort}: {query: string; sort?: string}) => [
|
||||||
export function useSearchPostsQuery({
|
export function useSearchPostsQuery({
|
||||||
query,
|
query,
|
||||||
sort,
|
sort,
|
||||||
|
enabled,
|
||||||
}: {
|
}: {
|
||||||
query: string
|
query: string
|
||||||
sort?: 'top' | 'latest'
|
sort?: 'top' | 'latest'
|
||||||
|
enabled?: boolean
|
||||||
}) {
|
}) {
|
||||||
return useInfiniteQuery<
|
return useInfiniteQuery<
|
||||||
AppBskyFeedSearchPosts.OutputSchema,
|
AppBskyFeedSearchPosts.OutputSchema,
|
||||||
|
@ -47,6 +49,7 @@ export function useSearchPostsQuery({
|
||||||
},
|
},
|
||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
|
enabled,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -195,9 +195,11 @@ type SearchResultSlice =
|
||||||
function SearchScreenPostResults({
|
function SearchScreenPostResults({
|
||||||
query,
|
query,
|
||||||
sort,
|
sort,
|
||||||
|
active,
|
||||||
}: {
|
}: {
|
||||||
query: string
|
query: string
|
||||||
sort?: 'top' | 'latest'
|
sort?: 'top' | 'latest'
|
||||||
|
active: boolean
|
||||||
}) {
|
}) {
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {currentAccount} = useSession()
|
const {currentAccount} = useSession()
|
||||||
|
@ -216,7 +218,7 @@ function SearchScreenPostResults({
|
||||||
fetchNextPage,
|
fetchNextPage,
|
||||||
isFetchingNextPage,
|
isFetchingNextPage,
|
||||||
hasNextPage,
|
hasNextPage,
|
||||||
} = useSearchPostsQuery({query: augmentedQuery, sort})
|
} = useSearchPostsQuery({query: augmentedQuery, sort, enabled: active})
|
||||||
|
|
||||||
const onPullToRefresh = React.useCallback(async () => {
|
const onPullToRefresh = React.useCallback(async () => {
|
||||||
setIsPTR(true)
|
setIsPTR(true)
|
||||||
|
@ -297,9 +299,19 @@ function SearchScreenPostResults({
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function SearchScreenUserResults({query}: {query: string}) {
|
function SearchScreenUserResults({
|
||||||
|
query,
|
||||||
|
active,
|
||||||
|
}: {
|
||||||
|
query: string
|
||||||
|
active: boolean
|
||||||
|
}) {
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {data: results, isFetched} = useActorSearch(query)
|
|
||||||
|
const {data: results, isFetched} = useActorSearch({
|
||||||
|
query,
|
||||||
|
enabled: active,
|
||||||
|
})
|
||||||
|
|
||||||
return isFetched && results ? (
|
return isFetched && results ? (
|
||||||
<>
|
<>
|
||||||
|
@ -335,6 +347,7 @@ export function SearchScreenInner({
|
||||||
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
|
const setDrawerSwipeDisabled = useSetDrawerSwipeDisabled()
|
||||||
const {hasSession} = useSession()
|
const {hasSession} = useSession()
|
||||||
const {isDesktop} = useWebMediaQueries()
|
const {isDesktop} = useWebMediaQueries()
|
||||||
|
const [activeTab, setActiveTab] = React.useState(0)
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
|
|
||||||
const isNewSearch = useGate('new_search')
|
const isNewSearch = useGate('new_search')
|
||||||
|
@ -343,6 +356,7 @@ export function SearchScreenInner({
|
||||||
(index: number) => {
|
(index: number) => {
|
||||||
setMinimalShellMode(false)
|
setMinimalShellMode(false)
|
||||||
setDrawerSwipeDisabled(index > 0)
|
setDrawerSwipeDisabled(index > 0)
|
||||||
|
setActiveTab(index)
|
||||||
},
|
},
|
||||||
[setDrawerSwipeDisabled, setMinimalShellMode],
|
[setDrawerSwipeDisabled, setMinimalShellMode],
|
||||||
)
|
)
|
||||||
|
@ -354,22 +368,38 @@ export function SearchScreenInner({
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
title: _(msg`Top`),
|
title: _(msg`Top`),
|
||||||
component: <SearchScreenPostResults query={query} sort="top" />,
|
component: (
|
||||||
|
<SearchScreenPostResults
|
||||||
|
query={query}
|
||||||
|
sort="top"
|
||||||
|
active={activeTab === 0}
|
||||||
|
/>
|
||||||
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: _(msg`Latest`),
|
title: _(msg`Latest`),
|
||||||
component: <SearchScreenPostResults query={query} sort="latest" />,
|
component: (
|
||||||
|
<SearchScreenPostResults
|
||||||
|
query={query}
|
||||||
|
sort="latest"
|
||||||
|
active={activeTab === 1}
|
||||||
|
/>
|
||||||
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: _(msg`People`),
|
title: _(msg`People`),
|
||||||
component: <SearchScreenUserResults query={query} />,
|
component: (
|
||||||
|
<SearchScreenUserResults query={query} active={activeTab === 2} />
|
||||||
|
),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
title: _(msg`People`),
|
title: _(msg`People`),
|
||||||
component: <SearchScreenUserResults query={query} />,
|
component: (
|
||||||
|
<SearchScreenUserResults query={query} active={activeTab === 0} />
|
||||||
|
),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -378,23 +408,29 @@ export function SearchScreenInner({
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
title: _(msg`Posts`),
|
title: _(msg`Posts`),
|
||||||
component: <SearchScreenPostResults query={query} />,
|
component: (
|
||||||
|
<SearchScreenPostResults query={query} active={activeTab === 0} />
|
||||||
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: _(msg`Users`),
|
title: _(msg`Users`),
|
||||||
component: <SearchScreenUserResults query={query} />,
|
component: (
|
||||||
|
<SearchScreenUserResults query={query} active={activeTab === 1} />
|
||||||
|
),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
title: _(msg`Users`),
|
title: _(msg`Users`),
|
||||||
component: <SearchScreenUserResults query={query} />,
|
component: (
|
||||||
|
<SearchScreenUserResults query={query} active={activeTab === 0} />
|
||||||
|
),
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [hasSession, isNewSearch, _, query])
|
}, [hasSession, isNewSearch, _, query, activeTab])
|
||||||
|
|
||||||
if (hasSession) {
|
if (hasSession) {
|
||||||
return query ? (
|
return query ? (
|
||||||
|
|
Loading…
Reference in New Issue