realtime: emit and handle chat_deleted for full chat removals
All checks were successful
CI / test (push) Successful in 38s

This commit is contained in:
2026-03-08 19:41:49 +03:00
parent a896568c53
commit 744ded914d
7 changed files with 73 additions and 4 deletions

View File

@@ -140,6 +140,13 @@ export function useRealtime() {
}
}
}
if (event.event === "chat_deleted") {
const chatId = Number(event.payload.chat_id);
if (Number.isFinite(chatId)) {
chatStore.removeChat(chatId);
scheduleReloadChats();
}
}
if (event.event === "pong") {
lastPongAtRef.current = Date.now();
}

View File

@@ -97,6 +97,7 @@ interface ChatState {
setEditingMessage: (chatId: number, message: Message | null) => void;
updateChatPinnedMessage: (chatId: number, pinnedMessageId: number | null) => void;
applyPresenceEvent: (chatId: number, userId: number, isOnline: boolean, lastSeenAt?: string) => void;
removeChat: (chatId: number) => void;
setDraft: (chatId: number, text: string) => void;
clearDraft: (chatId: number) => void;
setFocusedMessage: (chatId: number, messageId: number | null) => void;
@@ -431,6 +432,42 @@ export const useChatStore = create<ChatState>((set, get) => ({
return chat;
})
})),
removeChat: (chatId) =>
set((state) => {
const nextMessagesByChat = { ...state.messagesByChat };
const nextHasMoreByChat = { ...state.hasMoreByChat };
const nextLoadingMoreByChat = { ...state.loadingMoreByChat };
const nextTypingByChat = { ...state.typingByChat };
const nextReplyToByChat = { ...state.replyToByChat };
const nextEditingByChat = { ...state.editingByChat };
const nextUnreadBoundaryByChat = { ...state.unreadBoundaryByChat };
const nextFocusedMessageByChat = { ...state.focusedMessageIdByChat };
const nextDraftsByChat = { ...state.draftsByChat };
delete nextMessagesByChat[chatId];
delete nextHasMoreByChat[chatId];
delete nextLoadingMoreByChat[chatId];
delete nextTypingByChat[chatId];
delete nextReplyToByChat[chatId];
delete nextEditingByChat[chatId];
delete nextUnreadBoundaryByChat[chatId];
delete nextFocusedMessageByChat[chatId];
delete nextDraftsByChat[chatId];
saveDraftsToStorage(nextDraftsByChat);
const nextChats = state.chats.filter((chat) => chat.id !== chatId);
return {
chats: nextChats,
activeChatId: state.activeChatId === chatId ? (nextChats[0]?.id ?? null) : state.activeChatId,
messagesByChat: nextMessagesByChat,
hasMoreByChat: nextHasMoreByChat,
loadingMoreByChat: nextLoadingMoreByChat,
typingByChat: nextTypingByChat,
replyToByChat: nextReplyToByChat,
editingByChat: nextEditingByChat,
unreadBoundaryByChat: nextUnreadBoundaryByChat,
focusedMessageIdByChat: nextFocusedMessageByChat,
draftsByChat: nextDraftsByChat,
};
}),
setDraft: (chatId, text) =>
set((state) => {
const nextDrafts = {