Chats: MAKE WEBSOCKET STREAMING WORK!!!

This commit is contained in:
Alex Gleason 2022-09-21 18:26:18 -05:00
parent c3edd71bf3
commit 624d139a5a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 22 additions and 20 deletions

View file

@ -1,6 +1,5 @@
import { getSettings } from 'soapbox/actions/settings';
import messages from 'soapbox/locales/messages';
import { normalizeChat } from 'soapbox/normalizers';
import { queryClient } from 'soapbox/queries/client';
import { play, soundCache } from 'soapbox/utils/sounds';
@ -25,7 +24,8 @@ import {
processTimelineUpdate,
} from './timelines';
import type { InfiniteData, UseInfiniteQueryResult } from '@tanstack/react-query';
import type { InfiniteData } from '@tanstack/react-query';
import type { PaginatedResult } from 'soapbox/queries/chats';
import type { AppDispatch, RootState } from 'soapbox/store';
import type { APIEntity, Chat, ChatMessage } from 'soapbox/types/entities';
@ -53,14 +53,10 @@ interface ChatPayload extends Omit<Chat, 'last_message'> {
last_message: ChatMessage | null,
}
interface ChatPage {
result: Chat[],
hasMore: boolean,
link?: string,
}
const updateChat = (payload: ChatPayload) => {
queryClient.setQueriesData<InfiniteData<ChatPage>>(['chats', 'search'], (data) => {
const { last_message: lastMessage } = payload;
queryClient.setQueriesData<InfiniteData<PaginatedResult<Chat>>>(['chats', 'search'], (data) => {
if (data) {
const pages = data.pages.map(page => {
const result = page.result.map(chat => chat.id === payload.id ? payload as any : chat);
@ -70,15 +66,15 @@ const updateChat = (payload: ChatPayload) => {
}
});
// if (payload.last_message) {
// queryClient.setQueryData<UseInfiniteQueryResult<ChatMessage[]>>(['chats', 'messages', payload.id], (data) => {
// if (data) {
// const pages = [...data.pages];
// pages[0]?.push(payload.last_message);
// return { ...result, pages };
// }
// });
// }
if (lastMessage) {
queryClient.setQueryData<InfiniteData<PaginatedResult<ChatMessage>>>(['chats', 'messages', payload.id], (data) => {
if (data) {
const pages = [...data.pages];
pages[0] = { ...pages[0], result: [...pages[0].result, lastMessage] };
return { ...data, pages };
}
});
}
};
const connectTimelineStream = (

View file

@ -49,12 +49,18 @@ export interface IChatSilence {
target_account_id: number
}
export interface PaginatedResult<T> {
result: T[],
hasMore: boolean,
link?: string,
}
const reverseOrder = (a: IChat, b: IChat): number => compareId(a.id, b.id);
const useChatMessages = (chatId: string) => {
const api = useApi();
const getChatMessages = async(chatId: string, pageParam?: any): Promise<{ result: IChatMessage[], hasMore: boolean, link?: string }> => {
const getChatMessages = async(chatId: string, pageParam?: any): Promise<PaginatedResult<IChatMessage>> => {
const nextPageLink = pageParam?.link;
const uri = nextPageLink || `/api/v1/pleroma/chats/${chatId}/messages`;
const response = await api.get(uri);
@ -97,7 +103,7 @@ const useChats = (search?: string) => {
const api = useApi();
const dispatch = useAppDispatch();
const getChats = async(pageParam?: any): Promise<{ result: IChat[], hasMore: boolean, link?: string }> => {
const getChats = async(pageParam?: any): Promise<PaginatedResult<IChat>> => {
const nextPageLink = pageParam?.link;
const uri = nextPageLink || '/api/v1/pleroma/chats';
const response = await api.get<IChat[]>(uri, {