Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): blank a session's QR code when it disconnects
Yudhi Armyndharisab72ff21ae93
diff --git a/dashboard/src/hooks/useSessionPairing.ts b/dashboard/src/hooks/useSessionPairing.tsindex e0f2ae889..8102fb620 100644--- a/dashboard/src/hooks/useSessionPairing.ts+++ b/dashboard/src/hooks/useSessionPairing.ts@@ -31,6 +31,7 @@ export interface SessionPairing {handleCloseQRModal: () => void;applyQrPush: (event: { sessionId: string; qrCode: string }) => void;dismissQrForSession: (sessionId: string) => void;+ clearQrCodeForSession: (sessionId: string) => void;}/**@@ -191,6 +192,14 @@ export function useSessionPairing({ sessions, sessionsRef, reloadSessions }: UsesetQrData(current => (current?.sessionId === sessionId ? null : current));}, []);+ // Blank the displayed code while keeping the modal open, for a disconnect whose engine is still+ // registered (an engine-internal reconnect): the code on screen was minted by a connection that is+ // now gone, so scanning it cannot work. The modal falls back to its loading state, and the poll+ // fills it again once the session is back at `qr_ready` with a fresh code.+ const clearQrCodeForSession = useCallback((sessionId: string) => {+ setQrData(current => (current?.sessionId === sessionId ? { ...current, qrCode: '' } : current));+ }, []);+return {qrData,pairingMode,@@ -206,5 +215,6 @@ export function useSessionPairing({ sessions, sessionsRef, reloadSessions }: UsehandleCloseQRModal,applyQrPush,dismissQrForSession,+ clearQrCodeForSession,};}diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 31d844542..54b33ac79 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -775,6 +775,32 @@ test('a disconnected push keeps the QR modal while the engine is still registere}});+// The code on screen belongs to the connection that just dropped, so it is cleared even when the+// modal stays: the engine reconnects and pushes a fresh one, and a dead code must not be scannable+// in the meantime.+test('a disconnected push blanks the displayed QR code', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ resetFetchCalls();+ window.sessionStorage.setItem('openwa_api_key', 'test-key');+ const row: Session = { ...SESSION_QR, id: 'sess-blank-1', name: 'blanked', status: 'qr_ready', engineLoaded: true };+ SESSIONS.push(row);+ try {+ renderSessions();++ const card = (await screen.findByText('blanked')).closest('.session-card') as HTMLElement;+ fireEvent.click(within(card).getByRole('button', { name: 'Show QR' }));+ await screen.findByAltText('QR');++ row.status = 'disconnected';+ pushSessionStatus(row.id, 'disconnected');++ await waitFor(() => assert.ok(!screen.queryByAltText('QR'), 'the dead QR code stayed on screen'));+ assert.ok(screen.queryByRole('dialog'), 'the QR modal closed while the engine was still registered');+ } finally {+ SESSIONS.pop();+ }+});+// A re-read that fails says nothing about the engine, so the modal stays.test('a disconnected push keeps the QR modal when the re-read fails', async () => {const { screen, fireEvent, within, act } = rtl;diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex 6d604f3aa..55abbda9d 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -165,6 +165,7 @@ export function Sessions() {handleCloseQRModal,applyQrPush,dismissQrForSession,+ clearQrCodeForSession,} = useSessionPairing({ sessions, sessionsRef, reloadSessions: fetchSessions });const {@@ -250,7 +251,11 @@ export function Sessions() {// that means two different things — an engine still registered through its automatic// reconnect backoff, or a session stopped with no engine at all — and only the server can// say which, so the offered actions must not be guessed from the status here.- // The same answer decides whether an open QR modal for it can still show a code.+ // The same answer decides whether an open QR modal for it can be closed outright.+ // Either way the code on screen was minted by a connection that is now gone, so blank it+ // first: scanning it cannot work, and the modal shows its loading state until the session+ // is back at `qr_ready` with a fresh one.+ clearQrCodeForSession(event.sessionId);void fetchSessions().then(rows => {if (rows.find(s => s.id === event.sessionId)?.engineLoaded === false) {dismissQrForSession(event.sessionId);@@ -269,7 +274,7 @@ export function Sessions() {toast.error(t('sessions.toasts.failedTitle'), t('sessions.toasts.failedDesc'));}},- [toast, t, fetchSessions, queryClient, dismissQrForSession],+ [toast, t, fetchSessions, queryClient, dismissQrForSession, clearQrCodeForSession],),});