[SDK] Migrate profile and graph read queries to the appview client (#11356)

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-08-13 22:26:15 +03:00
committed by GitHub
parent 855457c9fa
commit 0912119c77
14 changed files with 180 additions and 148 deletions
+11 -9
View File
@@ -1,6 +1,8 @@
import {type DidString} from '@atproto/syntax'
import {type QueryClient, useInfiniteQuery} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export const RQKEY_ROOT = 'actor-starter-packs'
export const RQKEY_WITH_MEMBERSHIP_ROOT = 'actor-starter-packs-with-membership'
@@ -17,17 +19,17 @@ export function useActorStarterPacksQuery({
did?: string
enabled?: boolean
}) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery({
queryKey: RQKEY(did),
queryFn: async ({pageParam}: {pageParam?: string}) => {
const res = await agent.app.bsky.graph.getActorStarterPacks({
actor: did!,
return await client.call(app.bsky.graph.getActorStarterPacks, {
// the enabled flag prevents this from running until did is set
actor: did! as DidString,
limit: 10,
cursor: pageParam,
})
return res.data
},
enabled: Boolean(did) && enabled,
initialPageParam: undefined,
@@ -42,17 +44,17 @@ export function useActorStarterPacksWithMembershipsQuery({
did?: string
enabled?: boolean
}) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery({
queryKey: RQKEY_WITH_MEMBERSHIP(did),
queryFn: async ({pageParam}: {pageParam?: string}) => {
const res = await agent.app.bsky.graph.getStarterPacksWithMembership({
actor: did!,
return await client.call(app.bsky.graph.getStarterPacksWithMembership, {
// the enabled flag prevents this from running until did is set
actor: did! as DidString,
limit: 10,
cursor: pageParam,
})
return res.data
},
enabled: Boolean(did) && enabled,
initialPageParam: undefined,
+11 -12
View File
@@ -1,7 +1,5 @@
import {
type AppBskyActorDefs,
type AppBskyGraphGetKnownFollowers,
} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -9,7 +7,8 @@ import {
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const PAGE_SIZE = 50
type RQPageParam = string | undefined
@@ -18,22 +17,22 @@ const RQKEY_ROOT = 'profile-known-followers'
export const RQKEY = (did: string) => [RQKEY_ROOT, did]
export function useProfileKnownFollowersQuery(did: string | undefined) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetKnownFollowers.OutputSchema,
app.bsky.graph.getKnownFollowers.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetKnownFollowers.OutputSchema>,
InfiniteData<app.bsky.graph.getKnownFollowers.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(did || ''),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getKnownFollowers({
actor: did!,
return await client.call(app.bsky.graph.getKnownFollowers, {
// the enabled flag prevents this from running until did is set
actor: did! as DidString,
limit: PAGE_SIZE,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -46,7 +45,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetKnownFollowers.OutputSchema>
InfiniteData<app.bsky.graph.getKnownFollowers.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+14 -9
View File
@@ -1,9 +1,9 @@
import {
type AppBskyActorDefs,
type AppBskyGraphDefs,
type AppBskyGraphGetList,
type AtpAgent,
} from '@atproto/api'
import {type AtUriString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -13,7 +13,8 @@ import {
} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {useAgent, useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const PAGE_SIZE = 30
type RQPageParam = string | undefined
@@ -24,23 +25,23 @@ export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
export const RQKEY_ALL = (uri: string) => [RQKEY_ROOT_ALL, uri]
export function useListMembersQuery(uri?: string, limit: number = PAGE_SIZE) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetList.OutputSchema,
app.bsky.graph.getList.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetList.OutputSchema>,
InfiniteData<app.bsky.graph.getList.$OutputBody>,
QueryKey,
RQPageParam
>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(uri ?? ''),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getList({
list: uri!, // the enabled flag will prevent this from running until uri is set
return await client.call(app.bsky.graph.getList, {
// the enabled flag will prevent this from running until uri is set
list: uri! as AtUriString,
limit,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -60,6 +61,10 @@ export function useAllListMembersQuery(uri?: string) {
})
}
/*
* Still on the legacy agent: four call sites outside this cluster pass their
* own `AtpAgent`, so the parameter type is migrated with those consumers.
*/
export async function getAllListMembers(agent: AtpAgent, uri: string) {
let hasMore = true
let cursor: string | undefined
@@ -95,7 +100,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetList.OutputSchema>
InfiniteData<app.bsky.graph.getList.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+20 -15
View File
@@ -1,7 +1,5 @@
import {
type AppBskyActorDefs,
type AppBskyGraphGetListsWithMembership,
} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {type AtIdentifierString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -10,10 +8,11 @@ import {
} from '@tanstack/react-query'
import {createQueryKey} from '#/state/queries/util'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
export type ListWithMembership =
AppBskyGraphGetListsWithMembership.ListWithMembership
app.bsky.graph.getListsWithMembership.ListWithMembership
const listsWithMembershipQueryKeyRoot = 'lists-with-membership'
export const createListsWithMembershipQueryKey = (args: {actor: string}) =>
@@ -26,23 +25,23 @@ export function useListsWithMembershipQuery({
actor: string | undefined
enabled?: boolean
}) {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetListsWithMembership.OutputSchema,
app.bsky.graph.getListsWithMembership.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetListsWithMembership.OutputSchema>,
InfiniteData<app.bsky.graph.getListsWithMembership.$OutputBody>,
QueryKey,
string | undefined
>({
queryKey: createListsWithMembershipQueryKey({actor: actor ?? ''}),
queryFn: async ({pageParam}: {pageParam?: string}) => {
const res = await agent.app.bsky.graph.getListsWithMembership({
actor: actor!, // the enabled flag prevents this from running until actor is set
return await client.call(app.bsky.graph.getListsWithMembership, {
// the enabled flag prevents this from running until actor is set
actor: actor! as AtIdentifierString,
limit: 50,
cursor: pageParam,
})
return res.data
},
enabled: Boolean(actor) && enabled,
initialPageParam: undefined,
@@ -64,7 +63,7 @@ export function updateListMembershipOptimistically({
subject: AppBskyActorDefs.ProfileView
}) {
queryClient.setQueryData<
InfiniteData<AppBskyGraphGetListsWithMembership.OutputSchema>
InfiniteData<app.bsky.graph.getListsWithMembership.$OutputBody>
>(createListsWithMembershipQueryKey({actor}), old => {
if (!old) return old
@@ -74,12 +73,18 @@ export function updateListMembershipOptimistically({
...page,
listsWithMembership: page.listsWithMembership.map(lwm => {
if (lwm.list.uri === listUri) {
/*
* Callers hand over a plain at-uri and an old-world ProfileView,
* whose `uri`/`did` are unbranded strings. They are runtime
* identical to the generated branded forms, so the synthesised
* listItem is asserted rather than re-validated.
*/
return {
...lwm,
listItem: {
uri: membershipUri,
subject,
},
} as app.bsky.graph.defs.ListItemView,
}
}
return lwm
@@ -99,7 +104,7 @@ export function removeListMembershipOptimistically({
listUri: string
}) {
queryClient.setQueryData<
InfiniteData<AppBskyGraphGetListsWithMembership.OutputSchema>
InfiniteData<app.bsky.graph.getListsWithMembership.$OutputBody>
>(createListsWithMembershipQueryKey({actor}), old => {
if (!old) return old
+8 -8
View File
@@ -1,4 +1,4 @@
import {type AppBskyActorDefs, type AppBskyGraphGetBlocks} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {
type InfiniteData,
type QueryClient,
@@ -6,28 +6,28 @@ import {
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const RQKEY_ROOT = 'my-blocked-accounts'
export const RQKEY = () => [RQKEY_ROOT]
type RQPageParam = string | undefined
export function useMyBlockedAccountsQuery() {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetBlocks.OutputSchema,
app.bsky.graph.getBlocks.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetBlocks.OutputSchema>,
InfiniteData<app.bsky.graph.getBlocks.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getBlocks({
return await client.call(app.bsky.graph.getBlocks, {
limit: 30,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -39,7 +39,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetBlocks.OutputSchema>
InfiniteData<app.bsky.graph.getBlocks.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+20 -18
View File
@@ -1,9 +1,11 @@
import {type AppBskyGraphDefs} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {type QueryClient, useQuery} from '@tanstack/react-query'
import {accumulate} from '#/lib/async/accumulate'
import {STALE} from '#/state/queries'
import {useAgent, useSession} from '#/state/session'
import {useAppviewClient, useSession} from '#/state/session'
import {app} from '#/lexicons'
export type MyListsFilter =
| 'all'
@@ -16,7 +18,7 @@ export const RQKEY = (filter: MyListsFilter) => [RQKEY_ROOT, filter]
export function useMyListsQuery(filter: MyListsFilter) {
const {currentAccount} = useSession()
const agent = useAgent()
const client = useAppviewClient()
return useQuery<AppBskyGraphDefs.ListView[]>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(filter),
@@ -24,42 +26,42 @@ export function useMyListsQuery(filter: MyListsFilter) {
let lists: AppBskyGraphDefs.ListView[] = []
const promises = [
accumulate(cursor =>
agent.app.bsky.graph
.getLists({
actor: currentAccount!.did,
client
.call(app.bsky.graph.getLists, {
actor: currentAccount!.did as DidString,
cursor,
limit: 50,
})
.then(res => ({
cursor: res.data.cursor,
items: res.data.lists,
.then(data => ({
cursor: data.cursor,
items: data.lists,
})),
),
]
if (filter === 'all-including-subscribed' || filter === 'mod') {
promises.push(
accumulate(cursor =>
agent.app.bsky.graph
.getListMutes({
client
.call(app.bsky.graph.getListMutes, {
cursor,
limit: 50,
})
.then(res => ({
cursor: res.data.cursor,
items: res.data.lists,
.then(data => ({
cursor: data.cursor,
items: data.lists,
})),
),
)
promises.push(
accumulate(cursor =>
agent.app.bsky.graph
.getListBlocks({
client
.call(app.bsky.graph.getListBlocks, {
cursor,
limit: 50,
})
.then(res => ({
cursor: res.data.cursor,
items: res.data.lists,
.then(data => ({
cursor: data.cursor,
items: data.lists,
})),
),
)
+8 -8
View File
@@ -1,4 +1,4 @@
import {type AppBskyActorDefs, type AppBskyGraphGetMutes} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {
type InfiniteData,
type QueryClient,
@@ -6,28 +6,28 @@ import {
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
const RQKEY_ROOT = 'my-muted-accounts'
export const RQKEY = () => [RQKEY_ROOT]
type RQPageParam = string | undefined
export function useMyMutedAccountsQuery() {
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetMutes.OutputSchema,
app.bsky.graph.getMutes.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetMutes.OutputSchema>,
InfiniteData<app.bsky.graph.getMutes.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getMutes({
return await client.call(app.bsky.graph.getMutes, {
limit: 30,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -39,7 +39,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetMutes.OutputSchema>
InfiniteData<app.bsky.graph.getMutes.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+6 -4
View File
@@ -1,3 +1,4 @@
import {type DidString} from '@atproto/syntax'
import {msg} from '@lingui/core/macro'
import {useLingui} from '@lingui/react'
import {useMutation, useQueryClient} from '@tanstack/react-query'
@@ -5,14 +6,15 @@ import {useMutation, useQueryClient} from '@tanstack/react-query'
import {logger} from '#/logger'
import {RQKEY as FEED_RQKEY} from '#/state/queries/post-feed'
import * as Toast from '#/components/Toast'
import {app} from '#/lexicons'
import {updatePostShadow} from '../cache/post-shadow'
import {useAgent, useSession} from '../session'
import {useAppviewClient, useSession} from '../session'
import {useProfileUpdateMutation} from './profile'
export function usePinnedPostMutation() {
const {_} = useLingui()
const {currentAccount} = useSession()
const agent = useAgent()
const client = useAppviewClient()
const queryClient = useQueryClient()
const {mutateAsync: profileUpdateMutate} = useProfileUpdateMutation()
@@ -33,8 +35,8 @@ export function usePinnedPostMutation() {
// get the currently pinned post so we can optimistically remove the pin from it
if (!currentAccount) throw new Error('Not signed in')
const {data: profile} = await agent.getProfile({
actor: currentAccount.did,
const profile = await client.call(app.bsky.actor.getProfile, {
actor: currentAccount.did as DidString,
})
prevPinnedPost = profile.pinnedPost?.uri
if (prevPinnedPost && prevPinnedPost !== postUri) {
+11 -12
View File
@@ -1,14 +1,13 @@
import {
type AppBskyFeedGetActorFeeds,
moderateFeedGenerator,
} from '@atproto/api'
import {moderateFeedGenerator} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryKey,
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
import {useModerationOpts} from '../preferences/moderation-opts'
const PAGE_SIZE = 50
@@ -24,25 +23,25 @@ export function useProfileFeedgensQuery(
) {
const moderationOpts = useModerationOpts()
const enabled = opts?.enabled !== false && Boolean(moderationOpts)
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyFeedGetActorFeeds.OutputSchema,
app.bsky.feed.getActorFeeds.$OutputBody,
Error,
InfiniteData<AppBskyFeedGetActorFeeds.OutputSchema>,
InfiniteData<app.bsky.feed.getActorFeeds.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(did),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.feed.getActorFeeds({
actor: did,
const data = await client.call(app.bsky.feed.getActorFeeds, {
actor: did as DidString,
limit: PAGE_SIZE,
cursor: pageParam,
})
res.data.feeds.sort((a, b) => {
data.feeds.sort((a, b) => {
return (b.likeCount || 0) - (a.likeCount || 0)
})
return res.data
return data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
+18 -14
View File
@@ -1,7 +1,5 @@
import {
type AppBskyActorDefs,
type AppBskyGraphGetFollowers,
} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -9,8 +7,9 @@ import {
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {useAnalytics} from '#/analytics'
import {app} from '#/lexicons'
const DEFAULT_SORT = 'latest'
const PAGE_SIZE = 30
@@ -33,26 +32,31 @@ export function useProfileFollowersQuery(
) {
const ax = useAnalytics()
const isSortEnabled = ax.features.enabled(ax.features.FollowSortEnable)
const agent = useAgent()
const client = useAppviewClient()
const sortParam = isSortEnabled ? sort || DEFAULT_SORT : undefined
return useInfiniteQuery<
AppBskyGraphGetFollowers.OutputSchema,
app.bsky.graph.getFollowers.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetFollowers.OutputSchema>,
InfiniteData<app.bsky.graph.getFollowers.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(did || '', sortParam),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getFollowers({
actor: did || '',
/*
* The vendored lexicon does not declare `sort`, so it is spread in only
* when set and the whole params object is asserted. lex forwards
* undeclared params verbatim but rejects an undeclared key whose value
* is `undefined`, hence the conditional spread.
*/
return await client.call(app.bsky.graph.getFollowers, {
actor: (did || '') as DidString,
limit: PAGE_SIZE,
cursor: pageParam,
sort: sortParam,
})
return res.data
...(sortParam ? {sort: sortParam} : {}),
} as app.bsky.graph.getFollowers.$Params)
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -65,7 +69,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetFollowers.OutputSchema>
InfiniteData<app.bsky.graph.getFollowers.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+18 -11
View File
@@ -1,4 +1,5 @@
import {type AppBskyActorDefs, type AppBskyGraphGetFollows} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -7,8 +8,9 @@ import {
} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {useAnalytics} from '#/analytics'
import {app} from '#/lexicons'
const DEFAULT_SORT = 'latest'
const PAGE_SIZE = 30
@@ -34,27 +36,32 @@ export function useProfileFollowsQuery(
) {
const ax = useAnalytics()
const isSortEnabled = ax.features.enabled(ax.features.FollowSortEnable)
const agent = useAgent()
const client = useAppviewClient()
const sortParam = isSortEnabled ? sort || DEFAULT_SORT : undefined
return useInfiniteQuery<
AppBskyGraphGetFollows.OutputSchema,
app.bsky.graph.getFollows.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetFollows.OutputSchema>,
InfiniteData<app.bsky.graph.getFollows.$OutputBody>,
QueryKey,
RQPageParam
>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(did || '', sortParam),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getFollows({
actor: did || '',
/*
* The vendored lexicon does not declare `sort`, so it is spread in only
* when set and the whole params object is asserted. lex forwards
* undeclared params verbatim but rejects an undeclared key whose value
* is `undefined`, hence the conditional spread.
*/
return await client.call(app.bsky.graph.getFollows, {
actor: (did || '') as DidString,
limit: limit || PAGE_SIZE,
cursor: pageParam,
sort: sortParam,
})
return res.data
...(sortParam ? {sort: sortParam} : {}),
} as app.bsky.graph.getFollows.$Params)
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
@@ -67,7 +74,7 @@ export function* findAllProfilesInQueryData(
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyGraphGetFollows.OutputSchema>
InfiniteData<app.bsky.graph.getFollows.$OutputBody>
>({
queryKey: [RQKEY_ROOT],
})
+9 -9
View File
@@ -1,11 +1,13 @@
import {type AppBskyGraphGetLists, moderateUserList} from '@atproto/api'
import {moderateUserList} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryKey,
useInfiniteQuery,
} from '@tanstack/react-query'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
import {useModerationOpts} from '../preferences/moderation-opts'
const PAGE_SIZE = 30
@@ -17,23 +19,21 @@ export const RQKEY = (did: string) => [RQKEY_ROOT, did]
export function useProfileListsQuery(did: string, opts?: {enabled?: boolean}) {
const moderationOpts = useModerationOpts()
const enabled = opts?.enabled !== false && Boolean(moderationOpts)
const agent = useAgent()
const client = useAppviewClient()
return useInfiniteQuery<
AppBskyGraphGetLists.OutputSchema,
app.bsky.graph.getLists.$OutputBody,
Error,
InfiniteData<AppBskyGraphGetLists.OutputSchema>,
InfiniteData<app.bsky.graph.getLists.$OutputBody>,
QueryKey,
RQPageParam
>({
queryKey: RQKEY(did),
async queryFn({pageParam}: {pageParam: RQPageParam}) {
const res = await agent.app.bsky.graph.getLists({
actor: did,
return await client.call(app.bsky.graph.getLists, {
actor: did as DidString,
limit: PAGE_SIZE,
cursor: pageParam,
})
return res.data
},
initialPageParam: undefined,
getNextPageParam: lastPage => lastPage.cursor,
+15 -14
View File
@@ -1,9 +1,6 @@
import {useCallback, useMemo} from 'react'
import {
type AppBskyActorDefs,
type AppBskyActorGetSuggestions,
type AppBskyGraphGetSuggestedFollowsByActor,
} from '@atproto/api'
import {type AppBskyActorDefs} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
type QueryClient,
@@ -12,7 +9,8 @@ import {
} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
import type * as bsky from '#/types/bsky'
const suggestedFollowsQueryKeyRoot = 'suggested-follows'
@@ -32,18 +30,21 @@ export function useSuggestedFollowsByActorQuery({
enabled?: boolean
staleTime?: number
}) {
const agent = useAgent()
const client = useAppviewClient()
return useQuery({
staleTime,
queryKey: suggestedFollowsByActorQueryKey(did),
queryFn: async () => {
const res = await agent.app.bsky.graph.getSuggestedFollowsByActor({
actor: did,
})
const suggestions = res.data.suggestions.filter(
const data = await client.call(
app.bsky.graph.getSuggestedFollowsByActor,
{
actor: did as DidString,
},
)
const suggestions = data.suggestions.filter(
profile => !profile.viewer?.following,
)
return {suggestions, recId: res.data.recIdStr}
return {suggestions, recId: data.recIdStr}
},
enabled,
})
@@ -112,7 +113,7 @@ function* findAllProfilesInSuggestedFollowsQueryData(
did: string,
) {
const queryDatas = queryClient.getQueriesData<
InfiniteData<AppBskyActorGetSuggestions.OutputSchema>
InfiniteData<app.bsky.actor.getSuggestions.$OutputBody>
>({
queryKey: [suggestedFollowsQueryKeyRoot],
})
@@ -135,7 +136,7 @@ function* findAllProfilesInSuggestedFollowsByActorQueryData(
did: string,
) {
const queryDatas =
queryClient.getQueriesData<AppBskyGraphGetSuggestedFollowsByActor.OutputSchema>(
queryClient.getQueriesData<app.bsky.graph.getSuggestedFollowsByActor.$OutputBody>(
{
queryKey: [suggestedFollowsByActorQueryKeyRoot],
},
@@ -1,9 +1,11 @@
import {useCallback} from 'react'
import {type DidString} from '@atproto/syntax'
import {useQueryClient} from '@tanstack/react-query'
import {logger} from '#/logger'
import {updateProfileShadow} from '#/state/cache/profile-shadow'
import {useAgent} from '#/state/session'
import {useAppviewClient} from '#/state/session'
import {app} from '#/lexicons'
import type * as bsky from '#/types/bsky'
/**
@@ -13,13 +15,17 @@ import type * as bsky from '#/types/bsky'
*/
export function useUpdateProfileVerificationCache() {
const qc = useQueryClient()
const agent = useAgent()
const client = useAppviewClient()
return useCallback(
async ({profile}: {profile: bsky.profile.AnyProfileView}) => {
try {
const {data: updated} = await agent.getProfile({
actor: profile.did ?? '',
if (!profile.did) {
logger.warn(`useUpdateProfileVerificationCache: no did`, {profile})
return
}
const updated = await client.call(app.bsky.actor.getProfile, {
actor: profile.did as DidString,
})
updateProfileShadow(qc, profile.did, {
verification: updated.verification,
@@ -30,6 +36,6 @@ export function useUpdateProfileVerificationCache() {
})
}
},
[agent, qc],
[client, qc],
)
}