about summary refs log tree commit diff
path: root/src/state/queries/search-posts.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2023-12-07 17:20:17 -0800
committerGitHub <noreply@github.com>2023-12-07 17:20:17 -0800
commit7b5033118895448ae36c0ac2d76683ecd838f5e2 (patch)
treea9561eb741de580106d182460fc0ba4a5e825a68 /src/state/queries/search-posts.ts
parent448a403c81367c46b1323c4b545e02aaea0b92da (diff)
downloadvoidsky-7b5033118895448ae36c0ac2d76683ecd838f5e2.tar.zst
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
Diffstat (limited to 'src/state/queries/search-posts.ts')
-rw-r--r--src/state/queries/search-posts.ts40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/state/queries/search-posts.ts b/src/state/queries/search-posts.ts
index 03f3ba339..e0b317ca9 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<AppBskyFeedDefs.PostView, undefined> {
+  const queryDatas = queryClient.getQueriesData<
+    InfiniteData<AppBskyFeedSearchPosts.OutputSchema>
+  >({
+    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)
+        }
+      }
+    }
+  }
+}