Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep a pasted phone number whole
Yudhi Armyndharis58d97ef88e0b
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 9ff82edcd..a919b7af1 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -756,6 +756,27 @@ test('the pairing phone field leaves a composition in progress alone and reads iassert.equal(phoneInput.value, '628123456789');});+// A native maxLength counts the separators of a pasted number and cuts it before the page reads it: "+62 812-3456-7890"+// is 17 characters but 12 digits. The digits are capped instead, at the 15 a number can have.+test('the pairing phone field caps the digits it keeps, not the characters a paste may carry', async () => {+ const { screen, fireEvent, within } = rtl;+ resetFetchCalls();+ renderSessions();++ await screen.findByText('new-device');+ const qrCard = screen.getByText('new-device').closest('.session-card') as HTMLElement;+ fireEvent.click(within(qrCard).getByRole('button', { name: 'Show QR' }));+ await screen.findByAltText('QR');+ fireEvent.click(screen.getByRole('tab', { name: 'Link with Phone Number' }));+ const phoneInput = screen.getByLabelText('Phone Number') as HTMLInputElement;++ assert.equal(phoneInput.hasAttribute('maxlength'), false, 'a native maxLength cuts a pasted formatted number');+ fireEvent.change(phoneInput, { target: { value: '+62 812-3456-7890' } });+ assert.equal(phoneInput.value, '6281234567890');+ fireEvent.change(phoneInput, { target: { value: '1234567890123456789' } });+ assert.equal(phoneInput.value, '123456789012345');+});+// Nothing server-side refuses a pairing code for a number linked elsewhere, so the panel has to say// what it can cost before the operator types one.test('the phone pairing tab warns that a code can unlink an existing session', async () => {diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex 619d0a478..2a908e50e 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -68,6 +68,9 @@ function restrictionTitle(restriction: AccountRestriction, t: TFunction): stringreturn parts.join(' · ');}+const PAIRING_PHONE_MAX_DIGITS = 15;+const pairingDigits = (text: string) => normalizePhoneText(text).replace(/\D/g, '').slice(0, PAIRING_PHONE_MAX_DIGITS);+export function Sessions() {const { t } = useTranslation();useDocumentTitle(t('sessions.title'));@@ -890,21 +893,18 @@ export function Sessions() {className="pairing-input"type="tel"inputMode="numeric"- maxLength={15}placeholder={t('sessions.pairing.phonePlaceholder')}value={phoneNumber}// A composition in progress is left as typed, since rewriting it garbles what the IME commits;// it is read when the composition ends. Pasted and directly typed text is read at once.+ // The cap is on the digits, not a native maxLength: that counts the separators of a pasted+ // "+62 812-3456-7890" and cuts the number short.onChange={e =>setPhoneNumber(- (e.nativeEvent as InputEvent).isComposing- ? e.target.value- : normalizePhoneText(e.target.value).replace(/\D/g, ''),+ (e.nativeEvent as InputEvent).isComposing ? e.target.value : pairingDigits(e.target.value),)}- onCompositionEnd={e =>- setPhoneNumber(normalizePhoneText(e.currentTarget.value).replace(/\D/g, ''))- }+ onCompositionEnd={e => setPhoneNumber(pairingDigits(e.currentTarget.value))}onKeyDown={e => e.key === 'Enter' && handleGeneratePairingCode()}/><p className="input-hint" style={{ marginBottom: '1.5rem' }}>