Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): read native-script digits in the New chat panel
Yudhi Armyndharis22208eef1d40
diff --git a/dashboard/src/components/NewChatPanel.test.ts b/dashboard/src/components/NewChatPanel.test.tsnew file mode 100644index 000000000..e30cd268e--- /dev/null+++ b/dashboard/src/components/NewChatPanel.test.ts@@ -0,0 +1,99 @@+// The New chat panel reads a number typed with its country code. The panel lives under components/chats, so this+// test sits beside the other component tests, on the harness of the page tests (providers and a fetch stub).+import '../test-helpers/register-hooks.ts';+import { test, before, afterEach } from 'node:test';+import assert from 'node:assert/strict';+import { createElement } from 'react';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';++type RTL = typeof import('@testing-library/react');++let rtl: RTL;+let NewChatPanel: (typeof import('./chats/NewChatPanel.tsx'))['default'];+let RoleProvider: (typeof import('./RoleProvider.tsx'))['RoleProvider'];+let ToastProvider: (typeof import('./Toast.tsx'))['ToastProvider'];+let realFetch: typeof fetch;++function jsonResponse(data: unknown, status = 200): Response {+ return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });+}++const CONTACTS = [+ { id: '[email protected]', name: 'Anna', number: '491701234567', isMyContact: true, isBlocked: false },+ { id: '[email protected]', name: 'Ben', number: '628123456789', isMyContact: true, isBlocked: false },+];++const native = (ascii: string, zero: number) => [...ascii].map(d => String.fromCharCode(zero + Number(d))).join('');++before(async () => {+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');+ await installJsdomGlobals();+ const { i18nReady } = await import('../i18n/index.ts');+ await i18nReady;+ rtl = await import('@testing-library/react');+ ({ default: NewChatPanel } = await import('./chats/NewChatPanel.tsx'));+ ({ RoleProvider } = await import('./RoleProvider.tsx'));+ ({ ToastProvider } = await import('./Toast.tsx'));+ realFetch = globalThis.fetch;+});++afterEach(() => {+ rtl.cleanup();+ globalThis.fetch = realFetch;+ sessionStorage.clear();+});++function renderPanel(): { lookups: string[] } {+ const lookups: string[] = [];+ globalThis.fetch = (async (input: RequestInfo | URL) => {+ const path = String(input);+ if (path.includes('/contacts/check/')) {+ const number = path.split('/contacts/check/')[1];+ lookups.push(number);+ return jsonResponse({ number, exists: true, whatsappId: `${number}@c.us` });+ }+ if (path.includes('/contacts?')) return jsonResponse(CONTACTS);+ return jsonResponse([]);+ }) as typeof fetch;+ sessionStorage.setItem('openwa_user_role', 'operator');+ const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 1_000 } } });+ rtl.render(+ createElement(+ QueryClientProvider,+ { client },+ createElement(+ RoleProvider,+ null,+ createElement(+ ToastProvider,+ null,+ createElement(NewChatPanel, { sessionId: 's1', onOpenChat: () => {}, onClose: () => {} }),+ ),+ ),+ ),+ );+ return { lookups };+}++const search = () => rtl.screen.getByPlaceholderText('Search name or number with country code');++for (const [script, zero] of [+ ['Arabic-Indic', 0x0660],+ ['full-width', 0xff10],+] as const) {+ test(`a number typed in ${script} digits is offered and looked up as ASCII digits`, async () => {+ const { lookups } = renderPanel();+ rtl.fireEvent.change(search(), { target: { value: `+${native('491701234567', zero)}` } });+ rtl.fireEvent.click(await rtl.screen.findByRole('button', { name: /Start chat with/ }));+ await rtl.waitFor(() => assert.equal(lookups.length, 1));+ assert.deepEqual(lookups, ['491701234567']);+ });++ test(`digits typed in ${script} digits find a contact by its number`, async () => {+ renderPanel();+ await rtl.screen.findByText('Anna');+ rtl.fireEvent.change(search(), { target: { value: native('1701234', zero) } });+ await rtl.waitFor(() => assert.equal(rtl.screen.queryByText('Ben') === null, true));+ assert.ok(rtl.screen.getByText('Anna'));+ });+}diff --git a/dashboard/src/components/chats/NewChatPanel.tsx b/dashboard/src/components/chats/NewChatPanel.tsxindex 8703e5a34..97cc1ef67 100644--- a/dashboard/src/components/chats/NewChatPanel.tsx+++ b/dashboard/src/components/chats/NewChatPanel.tsx@@ -7,6 +7,7 @@ import { formatPhoneForDisplay, isolateNumber } from '../../utils/formatPhone';import { useRole } from '../../hooks/useRole';import { useToast } from '../../hooks/useToast';import ChatAvatar from './ChatAvatar';+import { normalizePhoneText } from '../../utils/phone';type Mode = 'chat' | 'group';@@ -41,7 +42,7 @@ const GROUP_NAME_MAX_LENGTH = 100;const toInternationalDigits = (input: string): string | null => {const trimmed = input.trim();if (!trimmed || /[a-z]/i.test(trimmed)) return null;- let digits = trimmed.replace(/\D/g, '');+ let digits = normalizePhoneText(trimmed).replace(/\D/g, '');if (digits.startsWith('00')) digits = digits.slice(2);else if (!trimmed.startsWith('+') && digits.startsWith('0')) return null;return digits.length >= 7 && digits.length <= 15 ? digits : null;@@ -117,7 +118,7 @@ function NewChatPanel({ sessionId, onOpenChat, findListedChat, onClose }: NewCha}, [contactsQuery.data]);const normalizedQuery = query.trim().toLowerCase();- const queryDigits = query.replace(/\D/g, '');+ const queryDigits = normalizePhoneText(query).replace(/\D/g, '');const visibleContacts = normalizedQuery? contacts.filter(c =>