migrate the group membership queries to the chat client

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Samuel Newman
2026-08-03 16:23:13 +03:00
parent 9553dd374b
commit 304726b83d
5 changed files with 44 additions and 41 deletions
@@ -4,16 +4,17 @@ import {
type ChatBskyConvoListConvos,
type ChatBskyGroupAddMembers,
} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
useMutation,
useQueryClient,
} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/lib/constants'
import {logger} from '#/logger'
import {useProfileQuery} from '#/state/queries/profile'
import {useAgent, useSession} from '#/state/session'
import {useChatClient, useSession} from '#/state/session'
import {chat} from '#/lexicons'
import type * as bsky from '#/types/bsky'
import {RQKEY as CONVO_KEY} from './conversation'
import {RQKEY_ROOT as CONVO_LIST_KEY} from './list-conversations'
@@ -30,7 +31,7 @@ export function useAddGroupMembers(
},
) {
const queryClient = useQueryClient()
const agent = useAgent()
const client = useChatClient()
const {currentAccount} = useSession()
const {data: myProfile} = useProfileQuery({did: currentAccount?.did})
@@ -42,11 +43,11 @@ export function useAddGroupMembers(
profiles: bsky.profile.AnyProfileView[]
}) => {
if (!convoId) throw new Error('No convoId provided')
const {data} = await agent.chat.bsky.group.addMembers(
{convoId, members},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data
return await client.call(chat.bsky.group.addMembers, {
convoId,
// callers pass already-resolved actor dids
members: members as DidString[],
})
},
onMutate: ({profiles}) => {
if (!convoId) return
@@ -1,9 +1,10 @@
import {type ChatBskyGroupCreateGroup} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/lib/constants'
import {logger} from '#/logger'
import {useAgent} from '#/state/session'
import {useChatClient} from '#/state/session'
import {chat} from '#/lexicons'
import {precacheConvoQuery} from './conversation'
export function useCreateGroupChat({
@@ -14,16 +15,15 @@ export function useCreateGroupChat({
onError?: (error: Error) => void
}) {
const queryClient = useQueryClient()
const agent = useAgent()
const client = useChatClient()
return useMutation({
mutationFn: async ({name, members}: {name: string; members: string[]}) => {
const {data} = await agent.chat.bsky.group.createGroup(
{name, members},
{headers: DM_SERVICE_HEADERS},
)
return data
return await client.call(chat.bsky.group.createGroup, {
name,
// callers pass already-resolved actor dids
members: members as DidString[],
})
},
onSuccess: data => {
precacheConvoQuery(queryClient, data.convo)
@@ -1,9 +1,9 @@
import {ChatBskyConvoDefs, type ChatBskyGroupEditGroup} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/lib/constants'
import {logger} from '#/logger'
import {useAgent} from '#/state/session'
import {useChatClient} from '#/state/session'
import {chat} from '#/lexicons'
import {
rollbackConvoOptimistic,
updateConvoOptimistic,
@@ -20,16 +20,15 @@ export function useEditGroupChatName(
},
) {
const queryClient = useQueryClient()
const agent = useAgent()
const client = useChatClient()
return useMutation({
mutationFn: async ({name: groupName}: {name: string}) => {
if (!convoId) throw new Error('No convoId provided')
const {data} = await agent.chat.bsky.group.editGroup(
{convoId, name: groupName},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data
return await client.call(chat.bsky.group.editGroup, {
convoId,
name: groupName,
})
},
onMutate: ({name: groupName}) => {
if (!convoId) return
@@ -1,8 +1,9 @@
import {type DidString} from '@atproto/syntax'
import {useInfiniteQuery} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/lib/constants'
import {createQueryKey} from '#/state/queries/util'
import {useAgent} from '#/state/session'
import {useChatClient} from '#/state/session'
import {chat} from '#/lexicons'
const listMutualGroupsQueryKeyRoot = 'list-mutual-groups'
@@ -18,7 +19,7 @@ export function useListMutualGroupsQuery({
enabled?: boolean
limit?: number
}) {
const agent = useAgent()
const client = useChatClient()
const isEnabled = enabled !== false && !!subject
return useInfiniteQuery({
@@ -27,11 +28,12 @@ export function useListMutualGroupsQuery({
enabled: isEnabled,
queryKey: createListMutualGroupsQueryKey({subject: subject ?? ''}),
queryFn: async ({pageParam}) => {
const {data} = await agent.chat.bsky.group.listMutualGroups(
{subject: subject!, cursor: pageParam, limit},
{headers: DM_SERVICE_HEADERS},
)
return data
return await client.call(chat.bsky.group.listMutualGroups, {
// guarded by `enabled`, and callers pass a resolved actor did
subject: subject as DidString,
cursor: pageParam,
limit,
})
},
initialPageParam: undefined as string | undefined,
getNextPageParam: page => page.cursor,
@@ -4,15 +4,16 @@ import {
type ChatBskyConvoListConvos,
type ChatBskyGroupRemoveMembers,
} from '@atproto/api'
import {type DidString} from '@atproto/syntax'
import {
type InfiniteData,
useMutation,
useQueryClient,
} from '@tanstack/react-query'
import {DM_SERVICE_HEADERS} from '#/lib/constants'
import {logger} from '#/logger'
import {useAgent} from '#/state/session'
import {useChatClient} from '#/state/session'
import {chat} from '#/lexicons'
import {RQKEY as CONVO_KEY} from './conversation'
import {RQKEY_ROOT as CONVO_LIST_KEY} from './list-conversations'
import {listConvoMembersQueryKey} from './list-convo-members'
@@ -28,16 +29,16 @@ export function useRemoveFromGroupChat(
},
) {
const queryClient = useQueryClient()
const agent = useAgent()
const client = useChatClient()
return useMutation({
mutationFn: async ({members}: {members: string[]}) => {
if (!convoId) throw new Error('No convoId provided')
const {data} = await agent.chat.bsky.group.removeMembers(
{convoId, members},
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
)
return data
return await client.call(chat.bsky.group.removeMembers, {
convoId,
// callers pass already-resolved actor dids
members: members as DidString[],
})
},
onMutate: ({members}) => {
if (!convoId) return