Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): badge a chat that is marked unread

Yudhi Armyndharis42fa210e2371
diff --git a/dashboard/src/components/chats/ChatSidebar.test.ts b/dashboard/src/components/chats/ChatSidebar.test.ts
new file mode 100644
index 000000000..48ab17cf5
--- /dev/null
+++ b/dashboard/src/components/chats/ChatSidebar.test.ts
@@ -0,0 +1,99 @@
+// Render tests for the chat sidebar's list states.
+import '../../test-helpers/register-hooks.ts';
+import { test, before, afterEach } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+import type { UseQueryResult } from '@tanstack/react-query';
+import type { Channel, Chat } from '../../services/api.ts';
+import type { ChatsTab } from './ChatSidebar.tsx';
+
+let rtl: typeof import('@testing-library/react');
+let ChatSidebar: (typeof import('./ChatSidebar.tsx'))['default'];
+let RoleContext: (typeof import('../../hooks/useRole.tsx'))['RoleContext'];
+let t: (key: string, options?: Record<string, unknown>) => string;
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ const { i18nReady, default: i18n } = await import('../../i18n/index.ts');
+ await i18nReady;
+ t = (key, options) => i18n.t(key, options ?? {});
+ rtl = await import('@testing-library/react');
+ ({ RoleContext } = await import('../../hooks/useRole.tsx'));
+ ({ default: ChatSidebar } = await import('./ChatSidebar.tsx'));
+});
+
+afterEach(() => rtl.cleanup());
+
+const chat = (id: string, unreadCount: number): Chat => ({
+ id,
+ name: id.split('@')[0],
+ isGroup: false,
+ kind: 'individual',
+ unreadCount,
+ timestamp: 0,
+ archived: false,
+ pinned: false,
+ muted: false,
+});
+
+function renderSidebar(activeTab: ChatsTab, chats: Chat[], channels: { all: Channel[]; shown: Channel[] }) {
+ const role = {
+ role: 'operator' as const,
+ setRole: () => undefined,
+ isAdmin: false,
+ isOperator: true,
+ isViewer: false,
+ canWrite: true,
+ engineType: 'whatsapp-web.js',
+ setEngineType: () => undefined,
+ };
+ const query = { isLoading: false, error: null, data: channels.all } as unknown as UseQueryResult<Channel[], Error>;
+ return rtl.render(
+ createElement(
+ RoleContext.Provider,
+ { value: role },
+ createElement(ChatSidebar, {
+ sessions: [],
+ selectedSessionId: '',
+ onSelectSession: () => undefined,
+ activeTab,
+ onSwitchTab: () => undefined,
+ searchQuery: '',
+ onSearchQueryChange: () => undefined,
+ onComposeStatus: () => undefined,
+ formatChatTime: () => '',
+ chatsTab: { loading: false, chats, onSelectChat: () => undefined },
+ channelsTab: {
+ engineLoading: false,
+ supported: true,
+ query,
+ channels: channels.shown,
+ onSelectChannel: () => undefined,
+ },
+ statusTab: {
+ loading: false,
+ error: false,
+ groups: [],
+ activeContactId: null,
+ onSelectContact: () => undefined,
+ },
+ }),
+ ),
+ );
+}
+
+test('a chat marked unread (count -1) shows an unread badge without a number', () => {
+ const { container } = renderSidebar('chats', [chat('[email protected]', -1), chat('[email protected]', 0), chat('[email protected]', 3)], {
+ all: [],
+ shown: [],
+ });
+ const badges = Array.from(container.querySelectorAll('.chat-unread-badge'));
+ assert.deepEqual(
+ badges.map(badge => [badge.textContent, badge.getAttribute('aria-label')]),
+ [
+ ['', t('chats.markedUnread')],
+ ['3', t('chats.unreadBadge', { count: 3 })],
+ ],
+ );
+});
diff --git a/dashboard/src/components/chats/ChatSidebar.tsx b/dashboard/src/components/chats/ChatSidebar.tsx
index cb210a78d..9a6d825ad 100644
--- a/dashboard/src/components/chats/ChatSidebar.tsx
+++ b/dashboard/src/components/chats/ChatSidebar.tsx
@@ -105,7 +105,14 @@ function ChatSidebar({
{formatLastMessageSnippet(chat) ||
(!chat.timestamp && <span className="no-message">{t('chats.noMessageYet')}</span>)}
</span>
- {chat.unreadCount > 0 && (
+ {/* A negative count is a chat marked unread (Baileys reports -1): a badge with no number. */}
+ {chat.unreadCount < 0 ? (
+ <span
+ className="chat-unread-badge"
+ title={t('chats.markedUnread')}
+ aria-label={t('chats.markedUnread')}
+ />
+ ) : chat.unreadCount > 0 ? (
<span
className="chat-unread-badge"
title={t('chats.unreadBadge', { count: chat.unreadCount })}
@@ -113,7 +120,7 @@ function ChatSidebar({
>
{chat.unreadCount > 99 ? '99+' : chat.unreadCount}
</span>
- )}
+ ) : null}
</div>
</div>
</div>