about summary refs log tree commit diff
path: root/src/data/useGetProfile.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/useGetProfile.ts')
-rw-r--r--src/data/useGetProfile.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/data/useGetProfile.ts b/src/data/useGetProfile.ts
new file mode 100644
index 000000000..58f24a4e8
--- /dev/null
+++ b/src/data/useGetProfile.ts
@@ -0,0 +1,33 @@
+import React from 'react'
+import {useQuery} from '@tanstack/react-query'
+import {BskyAgent} from '@atproto/api'
+
+import {useSession} from '#/state/session'
+
+export function useGetProfile({did}: {did: string}) {
+  const {accounts} = useSession()
+  const account = React.useMemo(
+    () => accounts.find(a => a.did === did),
+    [did, accounts],
+  )
+
+  return useQuery({
+    enabled: !!account,
+    queryKey: ['getProfile', account],
+    queryFn: async () => {
+      if (!account) {
+        throw new Error(`useGetProfile: local account not found for ${did}`)
+      }
+
+      const agent = new BskyAgent({
+        // needs to be public data, so remap PDS URLs to App View for now
+        service: account.service.includes('bsky.social')
+          ? 'https://api.bsky.app'
+          : account.service,
+      })
+
+      const res = await agent.getProfile({actor: did})
+      return res.data
+    },
+  })
+}