bigbuffet-rw/app/soapbox/queries/chats.ts

127 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-08-16 10:38:17 -07:00
import { useInfiniteQuery, useMutation, useQuery } from '@tanstack/react-query';
2022-08-10 05:38:49 -07:00
2022-08-16 10:38:17 -07:00
import { useChatContext } from 'soapbox/contexts/chat-context';
2022-08-10 05:38:49 -07:00
import { useApi } from 'soapbox/hooks';
2022-08-16 10:38:17 -07:00
import { queryClient } from './client';
2022-08-10 05:38:49 -07:00
export interface IChat {
id: string
unread: number
2022-08-16 10:38:17 -07:00
created_by_account: string
2022-08-10 05:38:49 -07:00
last_message: null | string
created_at: Date
updated_at: Date
accepted: boolean
discarded_at: null | string
account: any
}
export interface IChatMessage {
account_id: string
chat_id: string
content: string
created_at: Date
id: string
unread: boolean
}
const reverseOrder = (a: IChat, b: IChat): number => {
if (Number(a.id) < Number(b.id)) return -1;
if (Number(a.id) > Number(b.id)) return 1;
return 0;
};
const useChatMessages = (chatId: string) => {
const api = useApi();
2022-08-16 05:39:58 -07:00
const getChatMessages = async(chatId: string, pageParam?: any): Promise<{ result: IChatMessage[], maxId: string, hasMore: boolean }> => {
2022-08-10 05:38:49 -07:00
const { data, headers } = await api.get(`/api/v1/pleroma/chats/${chatId}/messages`, {
params: {
max_id: pageParam?.maxId,
},
});
const hasMore = !!headers.link;
const result = data.sort(reverseOrder);
const nextMaxId = result[0]?.id;
return {
result,
maxId: nextMaxId,
hasMore,
};
};
const queryInfo = useInfiniteQuery(['chats', 'messages', chatId], ({ pageParam }) => getChatMessages(chatId, pageParam), {
keepPreviousData: true,
getNextPageParam: (config) => {
if (config.hasMore) {
return { maxId: config.maxId };
}
return undefined;
},
});
const data = queryInfo.data?.pages.reduce<IChatMessage[]>(
(prev: IChatMessage[], curr) => [...curr.result, ...prev],
[],
);
return {
...queryInfo,
data,
};
};
const useChats = () => {
const api = useApi();
2022-08-16 05:39:58 -07:00
const getChats = async() => {
2022-08-10 05:38:49 -07:00
const { data } = await api.get('/api/v1/pleroma/chats');
return data;
};
const chatsQuery = useQuery<IChat[]>(['chats'], getChats, {
placeholderData: [],
});
const getOrCreateChatByAccountId = (accountId: string) => api.post<IChat>(`/api/v1/pleroma/chats/by-account-id/${accountId}`);
return { chatsQuery, getOrCreateChatByAccountId };
};
const useChat = (chatId: string) => {
const api = useApi();
2022-08-17 12:48:04 -07:00
const { setChat, setEditing } = useChatContext();
2022-08-10 05:38:49 -07:00
const markChatAsRead = () => api.post<IChat>(`/api/v1/pleroma/chats/${chatId}/read`);
const createChatMessage = (chatId: string, content: string) => {
return api.post<IChat>(`/api/v1/pleroma/chats/${chatId}/messages`, { content });
};
2022-08-16 05:39:58 -07:00
const deleteChatMessage = (chatMessageId: string) => api.delete<IChat>(`/api/v1/pleroma/chats/${chatId}/messages/${chatMessageId}`);
2022-08-16 10:38:17 -07:00
const acceptChat = useMutation(() => api.post<IChat>(`/api/v1/pleroma/chats/${chatId}/accept`), {
onSuccess(response) {
setChat(response.data);
queryClient.invalidateQueries(['chats', 'messages', chatId]);
queryClient.invalidateQueries(['chats']);
},
});
2022-08-16 10:54:36 -07:00
const deleteChat = useMutation(() => api.delete<IChat>(`/api/v1/pleroma/chats/${chatId}`), {
onSuccess(response) {
setChat(null);
2022-08-17 12:48:04 -07:00
setEditing(false);
2022-08-16 10:54:36 -07:00
queryClient.invalidateQueries(['chats', 'messages', chatId]);
queryClient.invalidateQueries(['chats']);
},
});
return { createChatMessage, markChatAsRead, deleteChatMessage, acceptChat, deleteChat };
2022-08-10 05:38:49 -07:00
};
export { useChat, useChats, useChatMessages };