about summary refs log tree commit diff
path: root/src/state/queries/post.ts
diff options
context:
space:
mode:
authorEric Bailey <git@esb.lol>2023-11-15 19:09:13 -0600
committerGitHub <noreply@github.com>2023-11-15 17:09:13 -0800
commitf23e9978d839322aab7304d2b6f183722e3ad4c1 (patch)
treea21f5e4056182c3f9b3bff6ef16246d4100a23d0 /src/state/queries/post.ts
parent9bcd00b83174cca3baef0519ba688e403662eea5 (diff)
downloadvoidsky-f23e9978d839322aab7304d2b6f183722e3ad4c1.tar.zst
Update post embed fetching to use new methods (#1916)
* Update post embed fetching to use new methods

* Use session agent
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<