about summary refs log tree commit diff
path: root/src/data/useGetProfile.ts
blob: 58f24a4e85ad37fde85a23125e694ce656b43015 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
    },
  })
}