fix(web): keep delivery status monotonic after reconnect
All checks were successful
CI / test (push) Successful in 20s

This commit is contained in:
2026-03-08 12:48:41 +03:00
parent dcc0f2abbc
commit 613edbecfe

View File

@@ -38,6 +38,21 @@ function saveDraftsToStorage(drafts: Record<number, string>): void {
}
}
function mergeDeliveryStatus(
incoming: DeliveryStatus | undefined,
existing: DeliveryStatus | undefined
): DeliveryStatus | undefined {
const rank: Record<DeliveryStatus, number> = {
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<ChatState>((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<ChatState>((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");
}