Add Chat service

This commit is contained in:
Eric Bailey
2024-04-30 14:46:00 -05:00
parent 80bcf81dc5
commit cd46f0f435
5 changed files with 608 additions and 135 deletions
+28 -17
View File
@@ -1,3 +1,5 @@
import React from 'react'
import {BskyAgent} from '@atproto/api'
import {
useInfiniteQuery,
useMutation,
@@ -11,7 +13,6 @@ import * as TempDmChatGetChat from '#/temp/dm/getChat'
import * as TempDmChatGetChatForMembers from '#/temp/dm/getChatForMembers'
import * as TempDmChatGetChatLog from '#/temp/dm/getChatLog'
import * as TempDmChatGetChatMessages from '#/temp/dm/getChatMessages'
import * as TempDmChatListChats from '#/temp/dm/listChats'
import {useDmServiceUrlStorage} from '../useDmServiceUrlStorage'
/**
@@ -229,19 +230,24 @@ export function useGetChatFromMembers({
const queryClient = useQueryClient()
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const [agent] = React.useState(() => {
return new BskyAgent({
service: serviceUrl,
})
})
return useMutation({
mutationFn: async (members: string[]) => {
const response = await fetch(
`${serviceUrl}/xrpc/temp.dm.getChatForMembers?members=${members.join(
',',
)}`,
{headers},
const {data} = await agent.api.temp.dm.getChatForMembers(
{
members,
},
{
headers,
},
)
if (!response.ok) throw new Error('Failed to fetch chat')
return (await response.json()) as TempDmChatGetChatForMembers.OutputSchema
return data
},
onSuccess: data => {
queryClient.setQueryData(['chat', data.chat.id], {
@@ -258,20 +264,25 @@ export function useGetChatFromMembers({
export function useListChats() {
const headers = useHeaders()
const {serviceUrl} = useDmServiceUrlStorage()
const [agent] = React.useState(() => {
return new BskyAgent({
service: serviceUrl,
})
})
return useInfiniteQuery({
queryKey: ['chats'],
queryFn: async ({pageParam}) => {
const response = await fetch(
`${serviceUrl}/xrpc/temp.dm.listChats${
pageParam ? `?cursor=${pageParam}` : ''
}`,
{headers},
const {data} = await agent.api.temp.dm.listChats(
{
cursor: pageParam,
},
{
headers,
},
)
if (!response.ok) throw new Error('Failed to fetch chats')
return (await response.json()) as TempDmChatListChats.OutputSchema
return data
},
initialPageParam: undefined as string | undefined,
getNextPageParam: lastPage => lastPage.cursor,