Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not close a QR modal over a code that just arrived
Yudhi Armyndharis49ece8a2b697
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ffcb63152..3a84d1d68 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- A whatsapp-web.js pairing-code retry cancels the attempt it abandoned before starting the next one. The per-attempt timeout stops waiting but does not stop the library's in-page linking flow, which keeps re-requesting a code on its own interval, so retries used to stack competing flows over one device slot.- A Baileys session whose credentials carry no LID of their own no longer reports a `group.join` for a group it created itself: every LID comparison answered false, so the creator was never recognised. The session's own LID mapping settles it instead.- Starting a session that turns out to be linked already no longer opens a QR modal over it, which then polled for a code that could never arrive. The decision now reads the list the dashboard re-reads after the start, not the state it held before it.+- A QR code that arrives while the dashboard is asking the server about a disconnect no longer has its modal closed underneath it. The handler blanks the displayed code, re-reads the session list, and closed the modal when the answer said no engine was left; a reconnect completing inside that window had already pushed a fresh, scannable code.- Escape dismisses the Chats emoji picker instead of closing the whole conversation behind it.- A click on a WhatsApp Business prompt quotes the prompt in the stored reply, so the dashboard renders the question above the answer instead of an empty quote box.- A template prompt that numbers only some of its buttons no longer answers the bot with an index belonging to a different button: a template that carries its own numbering is answered with it, and one that carries none is answered by position.diff --git a/dashboard/src/hooks/useSessionPairing.ts b/dashboard/src/hooks/useSessionPairing.tsindex 8102fb620..428a37ae1 100644--- a/dashboard/src/hooks/useSessionPairing.ts+++ b/dashboard/src/hooks/useSessionPairing.ts@@ -30,7 +30,7 @@ export interface SessionPairing {handleShowQR: (id: string) => Promise<void>;handleCloseQRModal: () => void;applyQrPush: (event: { sessionId: string; qrCode: string }) => void;- dismissQrForSession: (sessionId: string) => void;+ dismissQrForSession: (sessionId: string, onlyIfBlank?: boolean) => void;clearQrCodeForSession: (sessionId: string) => void;}@@ -188,8 +188,17 @@ export function useSessionPairing({ sessions, sessionsRef, reloadSessions }: Use// Clear the modal when the session that owned it stops, so it never hangs on a disconnected// session's stale code. Functional form deliberately: reading `qrData` here would make it a// dependency everywhere this is held (the page's stop/force-kill/unlink handlers).- const dismissQrForSession = useCallback((sessionId: string) => {- setQrData(current => (current?.sessionId === sessionId ? null : current));+ //+ // `onlyIfBlank` is for the one caller that decides asynchronously: the disconnect handler blanks+ // the code, asks the server whether an engine is still registered, and closes the modal on the+ // answer. A reconnect can complete inside that window and push a fresh code, and closing then+ // would throw away a code that works. A still-blank modal means nothing has arrived since.+ const dismissQrForSession = useCallback((sessionId: string, onlyIfBlank = false) => {+ setQrData(current => {+ if (current?.sessionId !== sessionId) return current;+ if (onlyIfBlank && current.qrCode) return current;+ return null;+ });}, []);// Blank the displayed code while keeping the modal open, for a disconnect whose engine is stilldiff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 62aa368f1..ee1538e09 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -796,6 +796,48 @@ test('a disconnected push closes the QR modal once the re-read shows no engine',}});+// The disconnect handler blanks the code, then asks the server whether an engine is still+// registered, and closes the modal on the answer. A reconnect can finish inside that window and push+// a fresh, scannable code; closing then would throw it away.+test('a QR pushed while the disconnect re-read is in flight keeps the modal open', async () => {+ const { screen, fireEvent, within, waitFor, act } = rtl;+ resetFetchCalls();+ window.sessionStorage.setItem('openwa_api_key', 'test-key');+ const row: Session = { ...SESSION_QR, id: 'sess-raced-1', name: 'raced', status: 'qr_ready', engineLoaded: true };+ SESSIONS.push(row);+ try {+ renderSessions();++ const card = (await screen.findByText('raced')).closest('.session-card') as HTMLElement;+ fireEvent.click(within(card).getByRole('button', { name: 'Show QR' }));+ await screen.findByAltText('QR');++ // The server answer will say the engine is gone, which is what used to close the modal outright.+ Object.assign(row, { status: 'disconnected', engineLoaded: false });+ pushSessionStatus(row.id, 'disconnected');++ // A fresh code lands before that answer is applied.+ const socket = lastSocket();+ assert.ok(socket, 'expected the page to have opened a socket');+ act(() => {+ socket.receive('message', {+ type: 'event',+ timestamp: '2026-01-01T00:00:00.000Z',+ payload: {+ event: 'session.qr',+ sessionId: row.id,+ data: { qrCode: 'data:image/png;base64,FRESH' },+ },+ });+ });++ await waitFor(() => assert.ok(findFetchCall('GET', '/api/sessions')));+ assert.ok(screen.queryByRole('dialog'), 'the modal closed over a QR code that had just arrived');+ } finally {+ SESSIONS.pop();+ }+});+test('a disconnected push keeps the QR modal while the engine is still registered', async () => {const { screen, fireEvent, within } = rtl;resetFetchCalls();diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex dcb8a4709..1c135a3d0 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -258,7 +258,10 @@ export function Sessions() {clearQrCodeForSession(event.sessionId);void fetchSessions().then(rows => {if (rows.find(s => s.id === event.sessionId)?.engineLoaded === false) {- dismissQrForSession(event.sessionId);+ // Only if nothing arrived while the answer was in flight: a reconnect that completed+ // in that window has already pushed a fresh code into the modal blanked above, and+ // that code is scannable.+ dismissQrForSession(event.sessionId, true);}});toast.warning(t('sessions.toasts.disconnectedTitle'), t('sessions.toasts.disconnectedDesc'));