Cleanup getProfile

zio/stable
Eric Bailey 2023-11-09 20:39:54 -06:00
parent ab878ba9a6
commit 2d7b89c6a1
3 changed files with 9 additions and 27 deletions

View File

@ -0,0 +1,5 @@
import {BskyAgent} from '@atproto/api'
export const PUBLIC_BSKY_AGENT = new BskyAgent({
service: 'https://api.bsky.app',
})

View File

@ -1,32 +1,12 @@
import React from 'react'
import {useQuery} from '@tanstack/react-query' import {useQuery} from '@tanstack/react-query'
import {BskyAgent} from '@atproto/api'
import {useSession} from '#/state/session' import {PUBLIC_BSKY_AGENT} from '#/data'
export function useGetProfile({did}: {did: string}) { export function useGetProfile({did}: {did: string}) {
const {accounts} = useSession()
const account = React.useMemo(
() => accounts.find(a => a.did === did),
[did, accounts],
)
return useQuery({ return useQuery({
enabled: !!account, queryKey: ['getProfile', did],
queryKey: ['getProfile', account],
queryFn: async () => { queryFn: async () => {
if (!account) { const res = await PUBLIC_BSKY_AGENT.getProfile({actor: did})
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 return res.data
}, },
}) })

View File

@ -4,6 +4,7 @@ import {BskyAgent, AtpPersistSessionHandler} from '@atproto/api'
import {networkRetry} from '#/lib/async/retry' import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger' import {logger} from '#/logger'
import * as persisted from '#/state/persisted' import * as persisted from '#/state/persisted'
import {PUBLIC_BSKY_AGENT} from '#/data'
export type SessionAccount = persisted.PersistedAccount export type SessionAccount = persisted.PersistedAccount
@ -43,10 +44,6 @@ export type ApiContext = {
clearCurrentAccount: () => void clearCurrentAccount: () => void
} }
export const PUBLIC_BSKY_AGENT = new BskyAgent({
service: 'https://api.bsky.app',
})
const StateContext = React.createContext<StateContext>({ const StateContext = React.createContext<StateContext>({
agent: PUBLIC_BSKY_AGENT, agent: PUBLIC_BSKY_AGENT,
hasSession: false, hasSession: false,