Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop a remembered unread mark when a chat is opened

Yudhi Armyndharis76203b95edad
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 96df9b88a..15c5e9097 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -1743,6 +1743,58 @@ for (const race of RELOAD_RACES) {
});
}
+test('a chat list asked for before Mark as unread does not mark a chat that was opened and read since', async () => {
+ const { screen, fireEvent, waitFor, within } = rtl;
+ const { container } = renderPage();
+ await screen.findByText('Alice');
+ const release = hold('chats');
+ receiveEvent('message.edited', {
+ messageId: 'wamid.edit.unread',
+ chatId: CHAT.id,
+ body: 'edited',
+ timestamp: 1_700_001_000,
+ });
+ await waitFor(() => assert.equal(chatListReads(), 2, 'the edit did not ask for the list'));
+
+ pickRowAction('Alice', 'Mark as unread');
+ await screen.findByLabelText('Marked as unread');
+ // Opening the chat is what reads it again, and clears the mark.
+ fireEvent.click(screen.getByText('Alice'));
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the room did not open'));
+ await waitFor(() => assert.ok(!screen.queryByLabelText('Marked as unread'), 'opening the chat kept its mark'));
+
+ release();
+ await settle();
+ await settle();
+ assert.ok(!screen.queryByLabelText('Marked as unread'), 'the earlier list put the unread mark back on the open chat');
+ fireEvent.click(container.querySelector('.room-menu-trigger') as HTMLElement);
+ const menu = container.querySelector('.room-menu-popover') as HTMLElement;
+ assert.ok(within(menu).getByRole('button', { name: 'Mark as unread' }), 'the room menu lost Mark as unread');
+});
+
+test('a chat list asked for before a chat was opened does not bring its old unread count back', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ aliceNow.unreadCount = 2;
+ const { container } = renderPage();
+ await screen.findByLabelText('2 unread messages');
+ const release = hold('chats');
+ receiveEvent('message.edited', {
+ messageId: 'wamid.edit.count',
+ chatId: '[email protected]',
+ body: 'edited',
+ timestamp: 1_700_001_000,
+ });
+ await waitFor(() => assert.equal(chatListReads(), 2, 'the edit did not ask for the list'));
+
+ fireEvent.click(screen.getByText('Alice'));
+ await waitFor(() => assert.ok(container.querySelector('.room-header'), 'the room did not open'));
+ await waitFor(() => assert.ok(!screen.queryByLabelText('2 unread messages'), 'opening the chat kept its badge'));
+ release();
+ await settle();
+ await settle();
+ assert.ok(!screen.queryByLabelText('2 unread messages'), 'the earlier list put the old unread count back');
+});
+
test('a chat list asked for after a change is taken as it comes, so a later change elsewhere shows', async () => {
const { screen, waitFor } = rtl;
renderPage();
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 4c05d2cd5..47f57fcdc 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -428,6 +428,17 @@ export function Chats() {
const held = patchedRowsRef.current.get(chatId);
patchedRowsRef.current.set(chatId, { request: chatsRequestRef.current, patch: { ...held?.patch, ...patch } });
}, []);
+ // A chat this page has read shows nothing unread, and a reload asked for before the read keeps it so. That also
+ // replaces a Mark as unread still remembered for the chat, which the read has undone. A session switch leaves the
+ // open chat the previous account's for one render, and that account's row must not be remembered for this one.
+ const markRowRead = useCallback(
+ (chatId: string) => {
+ if (listedSessionRef.current !== selectedSessionId) return;
+ rememberPatch(chatId, { unreadCount: 0 });
+ setChats(prev => prev.map(c => (c.id === chatId ? { ...c, unreadCount: 0 } : c)));
+ },
+ [rememberPatch, selectedSessionId],
+ );
const loadChats = useCallback(
async (
sessionId: string,
@@ -1070,7 +1081,7 @@ export function Chats() {
markChatRead(activeChat.id);
// A read-only key sends no mark-as-read, so the chat stays unread on the gateway; clearing the
// badge here would only have the next chat-list load bring it back.
- if (canWrite) setChats(prev => prev.map(c => (c.id === activeChat.id ? { ...c, unreadCount: 0 } : c)));
+ if (canWrite) markRowRead(activeChat.id);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [activeChat?.id, markChatRead, canWrite]);