about summary refs log tree commit diff
path: root/src/state/queries/post.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/post.ts')
-rw-r--r--src/state/queries/post.ts38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts
index ffff7f967..2aaa9a347 100644
--- a/src/state/queries/post.ts
+++ b/src/state/queries/post.ts
@@ -1,5 +1,6 @@
-import {AppBskyFeedDefs} from '@atproto/api'
-import {useQuery, useMutation} from '@tanstack/react-query'
+import React from 'react'
+import {AppBskyFeedDefs, AtUri} from '@atproto/api'
+import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
 import {useSession} from '../session'
 import {updatePostShadow} from '../cache/post-shadow'
 
@@ -21,6 +22,39 @@ export function usePostQuery(uri: string | undefined) {
   })
 }
 
+export function useGetPost() {
+  const queryClient = useQueryClient()
+  const {agent} = useSession()
+  return React.useCallback(
+    async ({uri}: {uri: string}) => {
+      return queryClient.fetchQuery({
+        queryKey: RQKEY(uri || ''),
+        async queryFn() {
+          const urip = new AtUri(uri)
+
+          if (!urip.host.startsWith('did:')) {
+            const res = await agent.resolveHandle({
+              handle: urip.host,
+            })
+            urip.host = res.data.did
+          }
+
+          const res = await agent.getPosts({
+            uris: [urip.toString()!],
+          })
+
+          if (res.success && res.data.posts[0]) {
+            return res.data.posts[0]
+          }
+
+          throw new Error('useGetPost: post not found')
+        },
+      })
+    },
+    [agent, queryClient],
+  )
+}
+
 export function usePostLikeMutation() {
   const {agent} = useSession()
   return useMutation<