Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): list a contact once in the new chat panel
Yudhi Armyndharis0b829d31a460
diff --git a/dashboard/src/components/chats/NewChatPanel.tsx b/dashboard/src/components/chats/NewChatPanel.tsxindex 68617690a..091603970 100644--- a/dashboard/src/components/chats/NewChatPanel.tsx+++ b/dashboard/src/components/chats/NewChatPanel.tsx@@ -44,6 +44,8 @@ const toInternationalDigits = (input: string): string | null => {const contactLabel = (c: Contact): string => c.name || c.pushName || formatPhoneForDisplay(c.id) || c.number;+const numberMatchesId = (c: Contact): boolean => c.number === c.id.split('@')[0];+// A chat row for a conversation the engine may not list yet (no history). The first message, or the// next chat-list load, replaces it with the engine's own row.const draftChat = (id: string, name: string, kind: Chat['kind'], timestamp = 0): Chat => ({@@ -95,14 +97,18 @@ function NewChatPanel({ sessionId, onOpenChat, onClose }: NewChatPanelProps) {});// Saved, unblocked personal contacts, alphabetically. The address book also lists groups and- // broadcast lists, which can be neither a 1:1 chat nor a group participant.- const contacts = useMemo(- () =>- (contactsQuery.data ?? [])- .filter(c => c.isMyContact && !c.isBlocked && !c.id.endsWith('@g.us') && !c.id.endsWith('@broadcast'))- .sort((a, b) => contactLabel(a).localeCompare(contactLabel(b))),- [contactsQuery.data],- );+ // broadcast lists, which can be neither a 1:1 chat nor a group participant. whatsapp-web.js lists a+ // contact it holds as both a LID and a phone record twice under one id, and only the copy whose+ // `number` is the id's is found by its phone number below, so that is the copy kept.+ const contacts = useMemo(() => {+ const byId = new Map<string, Contact>();+ for (const c of contactsQuery.data ?? []) {+ if (!c.isMyContact || c.isBlocked || c.id.endsWith('@g.us') || c.id.endsWith('@broadcast')) continue;+ const kept = byId.get(c.id);+ if (!kept || (!numberMatchesId(kept) && numberMatchesId(c))) byId.set(c.id, c);+ }+ return [...byId.values()].sort((a, b) => contactLabel(a).localeCompare(contactLabel(b)));+ }, [contactsQuery.data]);const normalizedQuery = query.trim().toLowerCase();const queryDigits = query.replace(/\D/g, '');diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex a7350f9eb..318587cea 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -84,6 +84,10 @@ const PUSH_ONLY_CONTACT = {};const LID_CHAT: Chat = { ...CHAT, id: '262813461250071@lid', name: 'Lia', lastMessage: 'from a lid' };let listExtra = false;+// Served when a test turns `listDuplicateContact` on: Bob listed twice under one id, as whatsapp-web.js lists a+// contact it holds as both a LID and a phone record. The first copy carries another number than the id's.+const DUPLICATE_CONTACT = { ...CONTACT, number: '262813461250071' };+let listDuplicateContact = false;// The number the new-chat tests type, and the id the gateway resolves it to.const TYPED_NUMBER = '+1 555 000 9999';const RESOLVED_ID = '[email protected]';@@ -178,7 +182,14 @@ function installFetchStub(): void {return gated('check', () => jsonResponse({ exists: true, whatsappId: RESOLVED_ID }));}if (method === 'GET' && path.startsWith(`${base}/contacts?`)) {- return Promise.resolve(jsonResponse([ALICE_CONTACT, CONTACT, ...(listExtra ? [PUSH_ONLY_CONTACT] : [])]));+ return Promise.resolve(+ jsonResponse([+ ALICE_CONTACT,+ ...(listDuplicateContact ? [DUPLICATE_CONTACT] : []),+ CONTACT,+ ...(listExtra ? [PUSH_ONLY_CONTACT] : []),+ ]),+ );}if (method === 'GET' && path.startsWith(`${base}/messages?`)) {return Promise.resolve(jsonResponse({ messages: [], total: 0 }));@@ -227,6 +238,7 @@ afterEach(async () => {fetchCalls.length = 0;listSecond = false;listExtra = false;+ listDuplicateContact = false;declineWrites = false;gates.clear();listGroup = false;@@ -268,6 +280,21 @@ test('a quick filter that matches no chat says so instead of claiming the accounassert.ok(!screen.queryByText('Alice'), 'the filter left a 1:1 chat in the groups list');});+test('a contact the engine lists twice under one id is offered once, and found by its phone number', async () => {+ const { screen, fireEvent } = rtl;+ listDuplicateContact = true;+ renderPage();+ await screen.findByText('Alice');+ fireEvent.click(screen.getByRole('button', { name: 'New chat' }));+ await screen.findByRole('button', { name: /Bob/ });+ assert.equal(screen.getAllByRole('button', { name: /Bob/ }).length, 1, 'Bob was listed twice');++ fireEvent.change(screen.getByLabelText('Search name or number with country code'), {+ target: { value: CONTACT.number },+ });+ assert.equal(screen.getAllByRole('button', { name: /Bob/ }).length, 1, 'Bob was not found by his phone number');+});+for (const reactStrictMode of [false, true]) {test(`a typed number is checked with the gateway and opens a chat under the id it resolves to${reactStrictMode ? ' (StrictMode)' : ''}`, async () => {const { screen, fireEvent, waitFor, within } = rtl;