Hack fix for no search results
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import {useEffect, useRef} from 'react'
|
||||||
import {AppBskyFeedDefs, AppBskyFeedSearchPosts} from '@atproto/api'
|
import {AppBskyFeedDefs, AppBskyFeedSearchPosts} from '@atproto/api'
|
||||||
import {
|
import {
|
||||||
useInfiniteQuery,
|
useInfiniteQuery,
|
||||||
@@ -14,19 +15,22 @@ const searchPostsQueryKey = ({query}: {query: string}) => [
|
|||||||
query,
|
query,
|
||||||
]
|
]
|
||||||
|
|
||||||
export function useSearchPostsQuery({query}: {query: string}) {
|
const PAGE_SIZE = 30
|
||||||
return useInfiniteQuery<
|
|
||||||
|
export function useSearchPostsQuery({query: q}: {query: string}) {
|
||||||
|
const lastPageCountRef = useRef(0)
|
||||||
|
const query = useInfiniteQuery<
|
||||||
AppBskyFeedSearchPosts.OutputSchema,
|
AppBskyFeedSearchPosts.OutputSchema,
|
||||||
Error,
|
Error,
|
||||||
InfiniteData<AppBskyFeedSearchPosts.OutputSchema>,
|
InfiniteData<AppBskyFeedSearchPosts.OutputSchema>,
|
||||||
QueryKey,
|
QueryKey,
|
||||||
string | undefined
|
string | undefined
|
||||||
>({
|
>({
|
||||||
queryKey: searchPostsQueryKey({query}),
|
queryKey: searchPostsQueryKey({query: q}),
|
||||||
queryFn: async ({pageParam}) => {
|
queryFn: async ({pageParam}) => {
|
||||||
const res = await getAgent().app.bsky.feed.searchPosts({
|
const res = await getAgent().app.bsky.feed.searchPosts({
|
||||||
q: query,
|
q,
|
||||||
limit: 25,
|
limit: PAGE_SIZE,
|
||||||
cursor: pageParam,
|
cursor: pageParam,
|
||||||
})
|
})
|
||||||
return res.data
|
return res.data
|
||||||
@@ -34,6 +38,34 @@ export function useSearchPostsQuery({query}: {query: string}) {
|
|||||||
initialPageParam: undefined,
|
initialPageParam: undefined,
|
||||||
getNextPageParam: lastPage => lastPage.cursor,
|
getNextPageParam: lastPage => lastPage.cursor,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const {isFetching, hasNextPage, data} = query
|
||||||
|
if (isFetching || !hasNextPage) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// avoid double-fires of fetchNextPage()
|
||||||
|
if (
|
||||||
|
lastPageCountRef.current !== 0 &&
|
||||||
|
lastPageCountRef.current === data?.pages?.length
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch next page if we haven't gotten a full page of content
|
||||||
|
let count = 0
|
||||||
|
for (const page of data?.pages || []) {
|
||||||
|
count += page.posts.length
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count < PAGE_SIZE && (data?.pages.length || 0) < 10) {
|
||||||
|
query.fetchNextPage()
|
||||||
|
lastPageCountRef.current = data?.pages?.length || 0
|
||||||
|
}
|
||||||
|
}, [query])
|
||||||
|
|
||||||
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* findAllPostsInQueryData(
|
export function* findAllPostsInQueryData(
|
||||||
|
|||||||
Reference in New Issue
Block a user