From 613edbecfec4c9cc7935a2d7ce328c1c77a56309 Mon Sep 17 00:00:00 2001 From: benya Date: Sun, 8 Mar 2026 12:48:41 +0300 Subject: [PATCH] fix(web): keep delivery status monotonic after reconnect --- web/src/store/chatStore.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/web/src/store/chatStore.ts b/web/src/store/chatStore.ts index 9be51fd..e4b59f7 100644 --- a/web/src/store/chatStore.ts +++ b/web/src/store/chatStore.ts @@ -38,6 +38,21 @@ function saveDraftsToStorage(drafts: Record): void { } } +function mergeDeliveryStatus( + incoming: DeliveryStatus | undefined, + existing: DeliveryStatus | undefined +): DeliveryStatus | undefined { + const rank: Record = { + sending: 1, + sent: 2, + delivered: 3, + read: 4 + }; + const incomingRank = incoming ? rank[incoming] : 0; + const existingRank = existing ? rank[existing] : 0; + return incomingRank >= existingRank ? incoming : existing; +} + interface ChatState { chats: Chat[]; activeChatId: number | null; @@ -106,10 +121,16 @@ export const useChatStore = create((set, get) => ({ const unreadCount = get().chats.find((c) => c.id === chatId)?.unread_count ?? 0; const messages = await getMessages(chatId); const sorted = [...messages].reverse(); + const existingById = new Map((get().messagesByChat[chatId] ?? []).map((message) => [message.id, message])); + const normalized = sorted.map((message) => { + const existing = existingById.get(message.id); + const deliveryStatus = mergeDeliveryStatus(message.delivery_status, existing?.delivery_status); + return deliveryStatus ? { ...message, delivery_status: deliveryStatus } : message; + }); set((state) => ({ messagesByChat: { ...state.messagesByChat, - [chatId]: sorted + [chatId]: normalized }, unreadBoundaryByChat: { ...state.unreadBoundaryByChat, @@ -127,7 +148,7 @@ export const useChatStore = create((set, get) => ({ chat.id === chatId ? { ...chat, unread_count: 0, unread_mentions_count: 0 } : chat ) })); - const lastMessage = sorted[sorted.length - 1]; + const lastMessage = normalized[normalized.length - 1]; if (lastMessage) { void updateMessageStatus(chatId, lastMessage.id, "message_read"); }