fix(web): keep delivery status monotonic after reconnect
All checks were successful
CI / test (push) Successful in 20s
All checks were successful
CI / test (push) Successful in 20s
This commit is contained in:
@@ -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 {
|
interface ChatState {
|
||||||
chats: Chat[];
|
chats: Chat[];
|
||||||
activeChatId: number | null;
|
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 unreadCount = get().chats.find((c) => c.id === chatId)?.unread_count ?? 0;
|
||||||
const messages = await getMessages(chatId);
|
const messages = await getMessages(chatId);
|
||||||
const sorted = [...messages].reverse();
|
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) => ({
|
set((state) => ({
|
||||||
messagesByChat: {
|
messagesByChat: {
|
||||||
...state.messagesByChat,
|
...state.messagesByChat,
|
||||||
[chatId]: sorted
|
[chatId]: normalized
|
||||||
},
|
},
|
||||||
unreadBoundaryByChat: {
|
unreadBoundaryByChat: {
|
||||||
...state.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
|
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) {
|
if (lastMessage) {
|
||||||
void updateMessageStatus(chatId, lastMessage.id, "message_read");
|
void updateMessageStatus(chatId, lastMessage.id, "message_read");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user