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