From 7b5033118895448ae36c0ac2d76683ecd838f5e2 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 7 Dec 2023 17:20:17 -0800 Subject: [PATCH] Various search fixes (#2145) * Add posts-search query to shadow cache search * Update user search to use correct endpoint * Fix: include cursor in post search --- src/state/cache/post-shadow.ts | 4 +++ src/state/cache/profile-shadow.ts | 2 ++ src/state/queries/actor-search.ts | 42 ++++++++++++++++++++++++++++++ src/state/queries/search-posts.ts | 40 +++++++++++++++++++++++++--- src/view/screens/Search/Search.tsx | 34 +++--------------------- 5 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 src/state/queries/actor-search.ts diff --git a/src/state/cache/post-shadow.ts b/src/state/cache/post-shadow.ts index e02d4f1e..55913e48 100644 --- a/src/state/cache/post-shadow.ts +++ b/src/state/cache/post-shadow.ts @@ -6,6 +6,7 @@ import {Shadow, castAsShadow} from './types' import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '../queries/notifications/feed' import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '../queries/post-feed' import {findAllPostsInQueryData as findAllPostsInThreadQueryData} from '../queries/post-thread' +import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '../queries/search-posts' import {queryClient} from 'lib/react-query' export type {Shadow} from './types' @@ -98,4 +99,7 @@ function* findPostsInCache( yield node.post } } + for (let post of findAllPostsInSearchQueryData(queryClient, uri)) { + yield post + } } diff --git a/src/state/cache/profile-shadow.ts b/src/state/cache/profile-shadow.ts index f85e1ad8..79a1f228 100644 --- a/src/state/cache/profile-shadow.ts +++ b/src/state/cache/profile-shadow.ts @@ -11,6 +11,7 @@ import {findAllProfilesInQueryData as findAllProfilesInProfileQueryData} from '. import {findAllProfilesInQueryData as findAllProfilesInProfileFollowersQueryData} from '../queries/profile-followers' import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows' import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows' +import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search' import {Shadow, castAsShadow} from './types' import {queryClient} from 'lib/react-query' export type {Shadow} from './types' @@ -98,4 +99,5 @@ function* findProfilesInCache(did: string): Generator { yield* findAllProfilesInProfileFollowersQueryData(queryClient, did) yield* findAllProfilesInProfileFollowsQueryData(queryClient, did) yield* findAllProfilesInSuggestedFollowsQueryData(queryClient, did) + yield* findAllProfilesInActorSearchQueryData(queryClient, did) } diff --git a/src/state/queries/actor-search.ts b/src/state/queries/actor-search.ts new file mode 100644 index 00000000..f7251154 --- /dev/null +++ b/src/state/queries/actor-search.ts @@ -0,0 +1,42 @@ +import {AppBskyActorDefs} from '@atproto/api' +import {QueryClient, useQuery} from '@tanstack/react-query' + +import {getAgent} from '#/state/session' +import {STALE} from '#/state/queries' + +export const RQKEY = (prefix: string) => ['actor-search', prefix] + +export function useActorSearch(prefix: string) { + return useQuery({ + staleTime: STALE.MINUTES.ONE, + queryKey: RQKEY(prefix || ''), + async queryFn() { + const res = await getAgent().searchActors({ + term: prefix, + }) + return res.data.actors + }, + enabled: !!prefix, + }) +} + +export function* findAllProfilesInQueryData( + queryClient: QueryClient, + did: string, +) { + const queryDatas = queryClient.getQueriesData( + { + queryKey: ['actor-search'], + }, + ) + for (const [_queryKey, queryData] of queryDatas) { + if (!queryData) { + continue + } + for (const actor of queryData) { + if (actor.did === did) { + yield actor + } + } + } +} diff --git a/src/state/queries/search-posts.ts b/src/state/queries/search-posts.ts index 03f3ba33..e0b317ca 100644 --- a/src/state/queries/search-posts.ts +++ b/src/state/queries/search-posts.ts @@ -1,7 +1,13 @@ -import {AppBskyFeedSearchPosts} from '@atproto/api' -import {useInfiniteQuery, InfiniteData, QueryKey} from '@tanstack/react-query' +import {AppBskyFeedDefs, AppBskyFeedSearchPosts} from '@atproto/api' +import { + useInfiniteQuery, + InfiniteData, + QueryKey, + QueryClient, +} from '@tanstack/react-query' import {getAgent} from '#/state/session' +import {embedViewRecordToPostView, getEmbeddedPost} from './util' const searchPostsQueryKey = ({query}: {query: string}) => [ 'search-posts', @@ -17,10 +23,11 @@ export function useSearchPostsQuery({query}: {query: string}) { string | undefined >({ queryKey: searchPostsQueryKey({query}), - queryFn: async () => { + queryFn: async ({pageParam}) => { const res = await getAgent().app.bsky.feed.searchPosts({ q: query, limit: 25, + cursor: pageParam, }) return res.data }, @@ -28,3 +35,30 @@ export function useSearchPostsQuery({query}: {query: string}) { getNextPageParam: lastPage => lastPage.cursor, }) } + +export function* findAllPostsInQueryData( + queryClient: QueryClient, + uri: string, +): Generator { + const queryDatas = queryClient.getQueriesData< + InfiniteData + >({ + queryKey: ['search-posts'], + }) + for (const [_queryKey, queryData] of queryDatas) { + if (!queryData?.pages) { + continue + } + for (const page of queryData?.pages) { + for (const post of page.posts) { + if (post.uri === uri) { + yield post + } + const quotedPost = getEmbeddedPost(post.embed) + if (quotedPost?.uri === uri) { + yield embedViewRecordToPostView(quotedPost) + } + } + } + } +} diff --git a/src/view/screens/Search/Search.tsx b/src/view/screens/Search/Search.tsx index f031abcc..b4db270b 100644 --- a/src/view/screens/Search/Search.tsx +++ b/src/view/screens/Search/Search.tsx @@ -36,6 +36,7 @@ import {useTheme} from 'lib/ThemeContext' import {useSession} from '#/state/session' import {useGetSuggestedFollowersByActor} from '#/state/queries/suggested-follows' import {useSearchPostsQuery} from '#/state/queries/search-posts' +import {useActorSearch} from '#/state/queries/actor-search' import {useActorAutocompleteFn} from '#/state/queries/actor-autocomplete' import {useSetDrawerOpen} from '#/state/shell' import {useAnalytics} from '#/lib/analytics/analytics' @@ -278,38 +279,9 @@ function SearchScreenPostResults({query}: {query: string}) { function SearchScreenUserResults({query}: {query: string}) { const {_} = useLingui() - const [isFetched, setIsFetched] = React.useState(false) - const [results, setResults] = React.useState< - AppBskyActorDefs.ProfileViewBasic[] - >([]) - const search = useActorAutocompleteFn() + const {data: results, isFetched} = useActorSearch(query) - React.useEffect(() => { - async function getResults() { - try { - const searchResults = await search({query, limit: 30}) - - if (searchResults) { - setResults(searchResults) - } - } catch (e: any) { - logger.error(`SearchScreenUserResults: failed to get results`, { - error: e.toString(), - }) - } finally { - setIsFetched(true) - } - } - - if (query) { - getResults() - } else { - setResults([]) - setIsFetched(false) - } - }, [query, search, setResults]) - - return isFetched ? ( + return isFetched && results ? ( <> {results.length ? (