about summary refs log tree commit diff
path: root/src/state/queries/search-posts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/search-posts.ts')
-rw-r--r--src/state/queries/search-posts.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/state/queries/search-posts.ts b/src/state/queries/search-posts.ts
new file mode 100644
index 000000000..03f3ba339
--- /dev/null
+++ b/src/state/queries/search-posts.ts
@@ -0,0 +1,30 @@
+import {AppBskyFeedSearchPosts} from '@atproto/api'
+import {useInfiniteQuery, InfiniteData, QueryKey} from '@tanstack/react-query'
+
+import {getAgent} from '#/state/session'
+
+const searchPostsQueryKey = ({query}: {query: string}) => [
+  'search-posts',
+  query,
+]
+
+export function useSearchPostsQuery({query}: {query: string}) {
+  return useInfiniteQuery<
+    AppBskyFeedSearchPosts.OutputSchema,
+    Error,
+    InfiniteData<AppBskyFeedSearchPosts.OutputSchema>,
+    QueryKey,
+    string | undefined
+  >({
+    queryKey: searchPostsQueryKey({query}),
+    queryFn: async () => {
+      const res = await getAgent().app.bsky.feed.searchPosts({
+        q: query,
+        limit: 25,
+      })
+      return res.data
+    },
+    initialPageParam: undefined,
+    getNextPageParam: lastPage => lastPage.cursor,
+  })
+}