diff --git a/app/soapbox/actions/streaming.ts b/app/soapbox/actions/streaming.ts index da5f52dc3..e4434d237 100644 --- a/app/soapbox/actions/streaming.ts +++ b/app/soapbox/actions/streaming.ts @@ -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 { last_message: ChatMessage | null, } -interface ChatPage { - result: Chat[], - hasMore: boolean, - link?: string, -} - const updateChat = (payload: ChatPayload) => { - queryClient.setQueriesData>(['chats', 'search'], (data) => { + const { last_message: lastMessage } = payload; + + queryClient.setQueriesData>>(['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>(['chats', 'messages', payload.id], (data) => { - // if (data) { - // const pages = [...data.pages]; - // pages[0]?.push(payload.last_message); - // return { ...result, pages }; - // } - // }); - // } + if (lastMessage) { + queryClient.setQueryData>>(['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 = ( diff --git a/app/soapbox/queries/chats.ts b/app/soapbox/queries/chats.ts index 46b79286d..0c472b36d 100644 --- a/app/soapbox/queries/chats.ts +++ b/app/soapbox/queries/chats.ts @@ -49,12 +49,18 @@ export interface IChatSilence { target_account_id: number } +export interface PaginatedResult { + 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> => { 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> => { const nextPageLink = pageParam?.link; const uri = nextPageLink || '/api/v1/pleroma/chats'; const response = await api.get(uri, {