Update 'last_message' if the user deletes it
This commit is contained in:
parent
15cd5f9104
commit
b33b32d9dc
2 changed files with 19 additions and 3 deletions
|
@ -2,7 +2,7 @@ import { InfiniteData } from '@tanstack/react-query';
|
|||
|
||||
import { getSettings } from 'soapbox/actions/settings';
|
||||
import messages from 'soapbox/locales/messages';
|
||||
import { ChatKeys } from 'soapbox/queries/chats';
|
||||
import { ChatKeys, isLastMessage } from 'soapbox/queries/chats';
|
||||
import { queryClient } from 'soapbox/queries/client';
|
||||
import { updatePageItem, appendPageItem, removePageItem, flattenPages, PaginatedResult } from 'soapbox/utils/queries';
|
||||
import { play, soundCache } from 'soapbox/utils/sounds';
|
||||
|
@ -79,9 +79,16 @@ const updateChat = (payload: ChatPayload) => {
|
|||
|
||||
const removeChatMessage = (payload: string) => {
|
||||
const data = JSON.parse(payload);
|
||||
const chatId = data.chat_id;
|
||||
const chatMessageId = data.deleted_message_id;
|
||||
|
||||
removePageItem(ChatKeys.chatMessages(data.chat_id), chatMessageId, (o: any, n: any) => String(o.id) === String(n));
|
||||
// If the user just deleted the "last_message", then let's invalidate
|
||||
// the Chat Search query so the Chat List will show the new "last_message".
|
||||
if (isLastMessage(chatMessageId)) {
|
||||
queryClient.invalidateQueries(ChatKeys.chatSearch());
|
||||
}
|
||||
|
||||
removePageItem(ChatKeys.chatMessages(chatId), chatMessageId, (o: any, n: any) => String(o.id) === String(n));
|
||||
};
|
||||
|
||||
const connectTimelineStream = (
|
||||
|
|
|
@ -68,6 +68,15 @@ const ChatKeys = {
|
|||
chatSearch: (searchQuery?: string) => searchQuery ? ['chats', 'search', searchQuery] : ['chats', 'search'] as const,
|
||||
};
|
||||
|
||||
/** Check if item is most recent */
|
||||
const isLastMessage = (chatMessageId: string): boolean => {
|
||||
const queryData = queryClient.getQueryData<InfiniteData<PaginatedResult<IChat>>>(ChatKeys.chatSearch());
|
||||
const items = flattenPages(queryData);
|
||||
const chat = items?.find((item) => item.last_message?.id === chatMessageId);
|
||||
|
||||
return !!chat;
|
||||
};
|
||||
|
||||
const reverseOrder = (a: IChat, b: IChat): number => compareId(a.id, b.id);
|
||||
|
||||
const useChatMessages = (chat: IChat) => {
|
||||
|
@ -271,4 +280,4 @@ const useChatActions = (chatId: string) => {
|
|||
return { createChatMessage, markChatAsRead, deleteChatMessage, updateChat, acceptChat, deleteChat };
|
||||
};
|
||||
|
||||
export { ChatKeys, useChat, useChatActions, useChats, useChatMessages };
|
||||
export { ChatKeys, useChat, useChatActions, useChats, useChatMessages, isLastMessage };
|
||||
|
|
Loading…
Reference in a new issue