Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep a typed number readable in right-to-left text

Yudhi Armyndharis40495bc86949
diff --git a/dashboard/src/components/chats/GroupInfoDialog.tsx b/dashboard/src/components/chats/GroupInfoDialog.tsx
index 8c9224eda..bd35461cc 100644
--- a/dashboard/src/components/chats/GroupInfoDialog.tsx
+++ b/dashboard/src/components/chats/GroupInfoDialog.tsx
@@ -15,7 +15,7 @@ import {
import { useProfilePicture } from '../../hooks/useProfilePicture';
import { useRole } from '../../hooks/useRole';
import { useToast } from '../../hooks/useToast';
-import { formatPhoneForDisplay } from '../../utils/formatPhone';
+import { formatPhoneForDisplay, isolateNumber } from '../../utils/formatPhone';
import { MEDIA_UPLOAD_MAX_BYTES } from '../../utils/mediaUpload';
import ChatAvatar from './ChatAvatar';
@@ -166,7 +166,8 @@ function GroupInfoDialog({ session, group, onClose, onRenamed }: GroupInfoDialog
const digits = newMember.replace(/\D/g, '').replace(/^00/, '');
const check = await contactApi.checkNumber(session.id, digits);
if (!check.exists || !check.whatsappId) {
- throw new Error(t('chats.newChat.notOnWhatsApp', { number: formatPhoneForDisplay(digits) ?? `+${digits}` }));
+ const number = isolateNumber(formatPhoneForDisplay(digits) ?? `+${digits}`);
+ throw new Error(t('chats.newChat.notOnWhatsApp', { number }));
}
return sessionApi.addGroupParticipants(session.id, group.id, [check.whatsappId]);
});
diff --git a/dashboard/src/components/chats/NewChatPanel.tsx b/dashboard/src/components/chats/NewChatPanel.tsx
index b34c74654..6f39d7748 100644
--- a/dashboard/src/components/chats/NewChatPanel.tsx
+++ b/dashboard/src/components/chats/NewChatPanel.tsx
@@ -3,7 +3,7 @@ import { useQuery } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';
import { ArrowLeft, Check, Loader2, Phone, Search, Users, X } from 'lucide-react';
import { contactApi, sessionApi, type Chat, type Contact } from '../../services/api';
-import { formatPhoneForDisplay } from '../../utils/formatPhone';
+import { formatPhoneForDisplay, isolateNumber } from '../../utils/formatPhone';
import { useRole } from '../../hooks/useRole';
import { useToast } from '../../hooks/useToast';
import ChatAvatar from './ChatAvatar';
@@ -126,7 +126,8 @@ function NewChatPanel({ sessionId, onOpenChat, findListedChat, onClose }: NewCha
: contacts;
const typedNumber = toInternationalDigits(query);
- const typedNumberLabel = typedNumber ? (formatPhoneForDisplay(typedNumber) ?? `+${typedNumber}`) : '';
+ // Placed in sentences (the start and add buttons, the not-registered toast), so it goes in as an isolate.
+ const typedNumberLabel = typedNumber ? isolateNumber(formatPhoneForDisplay(typedNumber) ?? `+${typedNumber}`) : '';
const showNumberHint = Boolean(query.trim()) && !typedNumber && /^[\d\s+()-]+$/.test(query.trim());
// Resolve a typed number to its canonical WhatsApp id. The send routes accept an unregistered number
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index be47338b1..364d66734 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -113,6 +113,8 @@ const lidPhones: Record<string, string> = {};
let failLidPhones = false;
// When true, it answers a phone that is no string, which nothing in the page expects.
let garbleLidPhones = false;
+// When true, the number check answers that the number is not on WhatsApp.
+let numberNotRegistered = false;
// How many more privacy-id chats the list holds, none of which resolves to a phone.
let extraLidChats = 0;
const extraLidChat = (n: number): Chat => ({
@@ -292,7 +294,11 @@ function installFetchStub(): void {
return Promise.resolve(jsonResponse({ contactId: id, phone: lidPhones[id] ?? null }));
}
if (method === 'GET' && path.startsWith(`${base}/contacts/check/`)) {
- return gated('check', () => jsonResponse({ exists: true, whatsappId: RESOLVED_ID }));
+ return gated('check', () =>
+ numberNotRegistered
+ ? jsonResponse({ number: '15550009999', exists: false, whatsappId: null })
+ : jsonResponse({ exists: true, whatsappId: RESOLVED_ID }),
+ );
}
if (method === 'GET' && path.startsWith(`${base}/contacts?`)) {
return gated('contacts', () =>
@@ -358,6 +364,7 @@ afterEach(async () => {
failContacts = false;
failLidPhones = false;
garbleLidPhones = false;
+ numberNotRegistered = false;
extraLidChats = 0;
for (const id of Object.keys(lidPhones)) delete lidPhones[id];
for (const key of Object.keys(aliceNow)) delete aliceNow[key as keyof Chat];
@@ -523,6 +530,63 @@ test('an answer about a privacy id that the page cannot read does not stop a cha
assert.ok(!screen.queryByText('Could not check the number'), 'a failed lookup was shown as a failed number check');
});
+// ── A phone number inside a sentence is placed in a left-to-right isolate: its plus sign and spaces are neutral to
+// the bidi algorithm, so in a right-to-left sentence the digit groups would be shown in reverse order. ──
+
+const LTR_NUMBER = `${String.fromCodePoint(0x2066)}+1 555 000 9999${String.fromCodePoint(0x2069)}`;
+
+test('the typed number keeps its digit groups together in the start chat label', async () => {
+ const { screen, fireEvent } = rtl;
+ renderPage();
+ await screen.findByText('Alice');
+ fireEvent.click(screen.getByRole('button', { name: 'New chat' }));
+ fireEvent.change(screen.getByLabelText('Search name or number with country code'), {
+ target: { value: TYPED_NUMBER },
+ });
+ const start = await screen.findByRole('button', { name: /^Start chat with/ });
+ assert.equal(start.textContent?.includes(LTR_NUMBER), true, `the label is not isolated: "${start.textContent}"`);
+});
+
+test('the typed number keeps its digit groups together in the add to group label', async () => {
+ const { screen, fireEvent } = rtl;
+ renderPage();
+ await screen.findByText('Alice');
+ fireEvent.click(screen.getByRole('button', { name: 'New chat' }));
+ fireEvent.click(screen.getByRole('button', { name: 'New group' }));
+ fireEvent.change(screen.getByLabelText('Search participants or enter a number'), { target: { value: TYPED_NUMBER } });
+ const add = await screen.findByRole('button', { name: /^Add \S/ });
+ assert.equal(add.textContent?.includes(LTR_NUMBER), true, `the label is not isolated: "${add.textContent}"`);
+});
+
+test('the number a check finds unregistered is named inside an isolate in the panel toast', async () => {
+ const { screen, fireEvent } = rtl;
+ numberNotRegistered = true;
+ renderPage();
+ await screen.findByText('Alice');
+ fireEvent.click(screen.getByRole('button', { name: 'New chat' }));
+ fireEvent.change(screen.getByLabelText('Search name or number with country code'), {
+ target: { value: TYPED_NUMBER },
+ });
+ fireEvent.click(await screen.findByRole('button', { name: /^Start chat with/ }));
+ const toast = await screen.findByText(/is not registered on WhatsApp/);
+ assert.equal(toast.textContent?.includes(LTR_NUMBER), true, `the toast is not isolated: "${toast.textContent}"`);
+});
+
+test('the number a check finds unregistered is named inside an isolate when adding to a group', async () => {
+ const { screen, fireEvent, within } = rtl;
+ listGroup = true;
+ numberNotRegistered = true;
+ renderPage();
+ const dialog = await openGroupInfo();
+ fireEvent.click(within(dialog).getByRole('tab', { name: 'Participants' }));
+ fireEvent.change(within(dialog).getByLabelText('Add a number with country code'), {
+ target: { value: TYPED_NUMBER },
+ });
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Add' }));
+ const toast = await screen.findByText(/is not registered on WhatsApp/);
+ assert.equal(toast.textContent?.includes(LTR_NUMBER), true, `the toast is not isolated: "${toast.textContent}"`);
+});
+
test('only the newest privacy-id chats are asked about, not every one the list holds', async () => {
const { screen, waitFor } = rtl;
extraLidChats = 60;
diff --git a/dashboard/src/utils/formatPhone.test.ts b/dashboard/src/utils/formatPhone.test.ts
index f26f64a08..1609aef70 100644
--- a/dashboard/src/utils/formatPhone.test.ts
+++ b/dashboard/src/utils/formatPhone.test.ts
@@ -1,6 +1,6 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
-import { parsePhoneFromJid, formatPhoneForDisplay } from './formatPhone.ts';
+import { parsePhoneFromJid, formatPhoneForDisplay, isolateNumber } from './formatPhone.ts';
test('parsePhoneFromJid extracts digits from a personal @c.us JID', () => {
assert.equal(parsePhoneFromJid('[email protected]'), '628123456789');
@@ -65,3 +65,9 @@ test('formatPhoneForDisplay picks the country code by its ITU prefix: 3 digits,
assert.equal(formatPhoneForDisplay('79161234567'), '+7 916 123 4567');
assert.equal(formatPhoneForDisplay('447911123456'), '+44 791 112 3456');
});
+
+test('isolateNumber keeps a number one left-to-right run inside a right-to-left sentence', () => {
+ const lri = String.fromCodePoint(0x2066);
+ const pdi = String.fromCodePoint(0x2069);
+ assert.equal(isolateNumber('+49 170 123 4 5678'), `${lri}+49 170 123 4 5678${pdi}`);
+});
diff --git a/dashboard/src/utils/formatPhone.ts b/dashboard/src/utils/formatPhone.ts
index 3edfd525a..c1dd5e496 100644
--- a/dashboard/src/utils/formatPhone.ts
+++ b/dashboard/src/utils/formatPhone.ts
@@ -61,3 +61,12 @@ export function formatPhoneForDisplay(phoneOrJid: string): string | null {
const prefixGroups = prefix.match(/.{1,3}/g) ?? [prefix];
return `+${cc} ${[...prefixGroups, last4].join(' ')}`;
}
+
+/**
+ * A phone number to place inside a sentence. Its plus sign and the spaces between its digit groups are neutral to
+ * the bidi algorithm, so inside a right-to-left sentence the groups are shown in reverse order; a left-to-right
+ * isolate keeps the number one run, whatever the sentence around it does.
+ */
+export function isolateNumber(number: string): string {
+ return `${String.fromCodePoint(0x2066)}${number}${String.fromCodePoint(0x2069)}`;
+}