f928e0a547
* mutate for mutes * mutate data for mutes * add initial data, `useConvoQuery` in `ConvoMenu` * `useInitialData` * don't use `identifier` for notifications, use `dates` instead * better implementation * simplify * simplify * fix types
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
import {
|
|
BskyAgent,
|
|
ChatBskyConvoLeaveConvo,
|
|
ChatBskyConvoListConvos,
|
|
} from '@atproto-labs/api'
|
|
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
|
|
|
import {logger} from '#/logger'
|
|
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
|
|
import {RQKEY as CONVO_LIST_KEY} from './list-converations'
|
|
import {useHeaders} from './temp-headers'
|
|
|
|
export function useLeaveConvo(
|
|
convoId: string | undefined,
|
|
{
|
|
onSuccess,
|
|
onError,
|
|
}: {
|
|
onSuccess?: (data: ChatBskyConvoLeaveConvo.OutputSchema) => void
|
|
onError?: (error: Error) => void
|
|
},
|
|
) {
|
|
const queryClient = useQueryClient()
|
|
const headers = useHeaders()
|
|
const {serviceUrl} = useDmServiceUrlStorage()
|
|
|
|
return useMutation({
|
|
mutationFn: async () => {
|
|
if (!convoId) throw new Error('No convoId provided')
|
|
|
|
const agent = new BskyAgent({service: serviceUrl})
|
|
const {data} = await agent.api.chat.bsky.convo.leaveConvo(
|
|
{convoId},
|
|
{headers, encoding: 'application/json'},
|
|
)
|
|
|
|
return data
|
|
},
|
|
onMutate: () => {
|
|
queryClient.setQueryData(
|
|
CONVO_LIST_KEY,
|
|
(old?: {
|
|
pageParams: Array<string | undefined>
|
|
pages: Array<ChatBskyConvoListConvos.OutputSchema>
|
|
}) => {
|
|
if (!old) return old
|
|
return {
|
|
...old,
|
|
pages: old.pages.map(page => {
|
|
return {
|
|
...page,
|
|
convos: page.convos.filter(convo => convo.id !== convoId),
|
|
}
|
|
}),
|
|
}
|
|
},
|
|
)
|
|
},
|
|
onSuccess: data => {
|
|
queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY})
|
|
onSuccess?.(data)
|
|
},
|
|
onError: error => {
|
|
logger.error(error)
|
|
queryClient.invalidateQueries({queryKey: CONVO_LIST_KEY})
|
|
onError?.(error)
|
|
},
|
|
})
|
|
}
|