migrate the chat convo lifecycle mutations to the chat client
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,9 +4,9 @@ import {
|
|||||||
} from '@atproto/api'
|
} 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 {
|
||||||
type ConvoRequestListQueryData,
|
type ConvoRequestListQueryData,
|
||||||
optimisticDelete as optimisticDeleteRequest,
|
optimisticDelete as optimisticDeleteRequest,
|
||||||
@@ -34,16 +34,11 @@ export function useAcceptConversation(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.convo.acceptConvo(
|
return await client.call(chat.bsky.convo.acceptConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS},
|
|
||||||
)
|
|
||||||
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
onMutate: () => {
|
onMutate: () => {
|
||||||
// snapshot every convo-list cache up front so onError can restore them
|
// snapshot every convo-list cache up front so onError can restore them
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import {
|
|||||||
useQueryClient,
|
useQueryClient,
|
||||||
} from '@tanstack/react-query'
|
} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS} from '#/lib/constants'
|
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {useOnMarkAsRead} from '#/state/queries/messages/list-conversations'
|
import {useOnMarkAsRead} from '#/state/queries/messages/list-conversations'
|
||||||
import {useAgent} from '#/state/session'
|
import {useChatClient} from '#/state/session'
|
||||||
|
import {chat} from '#/lexicons'
|
||||||
import {
|
import {
|
||||||
RQKEY_PARTIAL as UNREAD_COUNTS_PARTIAL_KEY,
|
RQKEY_PARTIAL as UNREAD_COUNTS_PARTIAL_KEY,
|
||||||
UNREAD_ACCEPTED_CAP,
|
UNREAD_ACCEPTED_CAP,
|
||||||
@@ -30,15 +30,12 @@ export const RQKEY_ROOT = 'convo'
|
|||||||
export const RQKEY = (convoId: string) => [RQKEY_ROOT, convoId]
|
export const RQKEY = (convoId: string) => [RQKEY_ROOT, convoId]
|
||||||
|
|
||||||
export function useConvoQuery({convoId}: {convoId: string}) {
|
export function useConvoQuery({convoId}: {convoId: string}) {
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: RQKEY(convoId),
|
queryKey: RQKEY(convoId),
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.convo.getConvo(
|
const data = await client.call(chat.bsky.convo.getConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS},
|
|
||||||
)
|
|
||||||
return data.convo
|
return data.convo
|
||||||
},
|
},
|
||||||
staleTime: STALE.INFINITY,
|
staleTime: STALE.INFINITY,
|
||||||
@@ -55,7 +52,7 @@ export function precacheConvoQuery(
|
|||||||
export function useMarkAsReadMutation() {
|
export function useMarkAsReadMutation() {
|
||||||
const optimisticUpdate = useOnMarkAsRead()
|
const optimisticUpdate = useOnMarkAsRead()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({
|
mutationFn: async ({
|
||||||
@@ -67,16 +64,10 @@ export function useMarkAsReadMutation() {
|
|||||||
}) => {
|
}) => {
|
||||||
if (!convoId) throw new Error('No convoId provided')
|
if (!convoId) throw new Error('No convoId provided')
|
||||||
|
|
||||||
await agent.chat.bsky.convo.updateRead(
|
await client.call(chat.bsky.convo.updateRead, {
|
||||||
{
|
convoId,
|
||||||
convoId,
|
messageId,
|
||||||
messageId,
|
})
|
||||||
},
|
|
||||||
{
|
|
||||||
encoding: 'application/json',
|
|
||||||
headers: DM_SERVICE_HEADERS,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
onMutate({convoId}) {
|
onMutate({convoId}) {
|
||||||
if (!convoId) throw new Error('No convoId provided')
|
if (!convoId) throw new Error('No convoId provided')
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {type ChatBskyConvoGetConvoForMembers} from '@atproto/api'
|
import {type ChatBskyConvoGetConvoForMembers} 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 {precacheConvoQuery} from './conversation'
|
import {precacheConvoQuery} from './conversation'
|
||||||
|
|
||||||
export function useGetConvoForMembers({
|
export function useGetConvoForMembers({
|
||||||
@@ -14,16 +14,15 @@ export function useGetConvoForMembers({
|
|||||||
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 (members: string[]) => {
|
mutationFn: async (members: string[]) => {
|
||||||
const {data} = await agent.chat.bsky.convo.getConvoForMembers(
|
return await client.call(chat.bsky.convo.getConvoForMembers, {
|
||||||
{members: members},
|
// callers pass already-resolved actor dids
|
||||||
{headers: DM_SERVICE_HEADERS},
|
members:
|
||||||
)
|
members as chat.bsky.convo.getConvoForMembers.$Params['members'],
|
||||||
|
})
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
onSuccess: data => {
|
onSuccess: data => {
|
||||||
precacheConvoQuery(queryClient, data.convo)
|
precacheConvoQuery(queryClient, data.convo)
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import {
|
|||||||
} from '@atproto/api'
|
} 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 {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links'
|
import {invalidateJoinLinkPreviewsForConvo} from '#/state/queries/join-links'
|
||||||
import {useAgent} from '#/state/session'
|
import {useChatClient} from '#/state/session'
|
||||||
|
import {chat} from '#/lexicons'
|
||||||
import {
|
import {
|
||||||
type ConvoRequestListQueryData,
|
type ConvoRequestListQueryData,
|
||||||
optimisticDelete as optimisticDeleteRequest,
|
optimisticDelete as optimisticDeleteRequest,
|
||||||
@@ -38,19 +38,14 @@ export function useLeaveConvo(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationKey: RQKEY(convoId),
|
mutationKey: RQKEY(convoId),
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
if (!convoId) throw new Error('No convoId provided')
|
if (!convoId) throw new Error('No convoId provided')
|
||||||
|
|
||||||
const {data} = await agent.chat.bsky.convo.leaveConvo(
|
return await client.call(chat.bsky.convo.leaveConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
onMutate: () => {
|
onMutate: () => {
|
||||||
const prevConvoListQueries =
|
const prevConvoListQueries =
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {ChatBskyConvoDefs, type ChatBskyConvoLockConvo} from '@atproto/api'
|
import {ChatBskyConvoDefs, type ChatBskyConvoLockConvo} 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 {useChatClient} from '#/state/session'
|
||||||
import {useAgent} from '#/state/session'
|
import {chat} from '#/lexicons'
|
||||||
import {
|
import {
|
||||||
rollbackConvoOptimistic,
|
rollbackConvoOptimistic,
|
||||||
updateConvoOptimistic,
|
updateConvoOptimistic,
|
||||||
@@ -25,23 +25,15 @@ export function useLockConvo(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({lock}: {lock: boolean; silent?: boolean}) => {
|
mutationFn: async ({lock}: {lock: boolean; silent?: boolean}) => {
|
||||||
if (!convoId) throw new Error('No convoId provided')
|
if (!convoId) throw new Error('No convoId provided')
|
||||||
if (lock) {
|
if (lock) {
|
||||||
const {data} = await agent.chat.bsky.convo.lockConvo(
|
return await client.call(chat.bsky.convo.lockConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
} else {
|
} else {
|
||||||
const {data} = await agent.chat.bsky.convo.unlockConvo(
|
return await client.call(chat.bsky.convo.unlockConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onMutate: ({lock}) => {
|
onMutate: ({lock}) => {
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {type ChatBskyConvoMuteConvo} from '@atproto/api'
|
import {type ChatBskyConvoMuteConvo} 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 {useChatClient} from '#/state/session'
|
||||||
import {useAgent} from '#/state/session'
|
import {chat} from '#/lexicons'
|
||||||
import {
|
import {
|
||||||
rollbackConvoOptimistic,
|
rollbackConvoOptimistic,
|
||||||
updateConvoOptimistic,
|
updateConvoOptimistic,
|
||||||
@@ -19,23 +19,15 @@ export function useMuteConvo(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({mute}: {mute: boolean}) => {
|
mutationFn: async ({mute}: {mute: boolean}) => {
|
||||||
if (!convoId) throw new Error('No convoId provided')
|
if (!convoId) throw new Error('No convoId provided')
|
||||||
if (mute) {
|
if (mute) {
|
||||||
const {data} = await agent.chat.bsky.convo.muteConvo(
|
return await client.call(chat.bsky.convo.muteConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
} else {
|
} else {
|
||||||
const {data} = await agent.chat.bsky.convo.unmuteConvo(
|
return await client.call(chat.bsky.convo.unmuteConvo, {convoId})
|
||||||
{convoId},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
return data
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onMutate: ({mute}) => {
|
onMutate: ({mute}) => {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import {type ChatBskyConvoGetUnreadCounts} from '@atproto/api'
|
import {type ChatBskyConvoGetUnreadCounts} 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 {RQKEY_PARTIAL as UNREAD_COUNTS_PARTIAL_KEY} from './get-unread-counts'
|
import {RQKEY_PARTIAL as UNREAD_COUNTS_PARTIAL_KEY} from './get-unread-counts'
|
||||||
import {
|
import {
|
||||||
type ConvoRequestListQueryData,
|
type ConvoRequestListQueryData,
|
||||||
@@ -29,16 +29,11 @@ export function useUpdateAllRead(
|
|||||||
},
|
},
|
||||||
) {
|
) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const client = useChatClient()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async () => {
|
mutationFn: async () => {
|
||||||
const {data} = await agent.chat.bsky.convo.updateAllRead(
|
return await client.call(chat.bsky.convo.updateAllRead, {status})
|
||||||
{status},
|
|
||||||
{headers: DM_SERVICE_HEADERS, encoding: 'application/json'},
|
|
||||||
)
|
|
||||||
|
|
||||||
return data
|
|
||||||
},
|
},
|
||||||
onMutate: () => {
|
onMutate: () => {
|
||||||
// snapshot every convo-list cache up front so onError can restore them
|
// snapshot every convo-list cache up front so onError can restore them
|
||||||
|
|||||||
Reference in New Issue
Block a user