Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): stop the sessions effect from rewinding the ref
Yudhi Armyndharisf3bffb371771
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.tsindex 9c3104318..694cc5717 100644--- a/dashboard/src/pages/Sessions.test.ts+++ b/dashboard/src/pages/Sessions.test.ts@@ -1304,6 +1304,49 @@ test('a status push landing right after a create or a delete keeps what it wrote}});+// A push handled between a list render's commit and its passive effects must not have its write undone+// in the ref, or the double-signal that follows it is taken for a fresh transition.+test('a duplicate push right after a list render is still recognised as a duplicate', async () => {+ const { screen, waitFor, act } = rtl;+ resetFetchCalls();+ window.sessionStorage.setItem('openwa_api_key', 'test-key');+ const row: Session = { ...SESSION_QR, id: 'sess-dup-1', name: 'dup-probe', status: 'authenticating' };+ SESSIONS.push(row);+ // Emitted outside act, so React commits and runs effects on its own schedule, as in the browser.+ const emit = (status: string) =>+ lastSocket()!.receive('message', {+ type: 'event',+ timestamp: '2026-01-01T00:00:00.000Z',+ payload: { event: 'session.status', sessionId: row.id, data: { status } },+ });+ let phase = 0;+ const observer = new MutationObserver(() => {+ if (phase === 0 && screen.queryByText('dup-probe-renamed')) {+ // The list read has just committed; its passive effects have not run yet.+ phase = 1;+ Object.assign(row, { status: 'ready' });+ emit('ready');+ } else if (phase === 1) {+ // The engine double-signals the same transition once React has rendered the first one.+ phase = 2;+ emit('ready');+ }+ });+ try {+ renderSessions();+ await screen.findByText('dup-probe');+ observer.observe(document.body, { childList: true, subtree: true, characterData: true });+ Object.assign(row, { name: 'dup-probe-renamed' });+ pushSessionStatus(SESSION_TIMELOCKED.id, 'action_required');+ await waitFor(() => assert.equal(phase, 2));+ await act(() => new Promise<void>(resolve => setTimeout(resolve, 50)));+ assert.equal(screen.queryAllByText('Session Ready').length, 1, 'the duplicate ready push was handled twice');+ } finally {+ observer.disconnect();+ SESSIONS.pop();+ }+});+// The detail modal shows the row as it is now, not as it was when View was clicked.test('an open detail modal follows its session status and phone', async () => {const { screen, fireEvent, within, waitFor } = rtl;diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsxindex a4e9ed028..c53d0ec65 100644--- a/dashboard/src/pages/Sessions.tsx+++ b/dashboard/src/pages/Sessions.tsx@@ -128,12 +128,9 @@ export function Sessions() {// Mirror the latest sessions in a ref so the WS handler can compare against the current status without// depending on `sessions` (which would churn the callback identity and re-subscribe the socket). Every// writer moves the ref in the same tick as its setState (a list read directly, row writes through- // `updateSessions`), so a push handled before React re-renders sees what the last write produced. The- // effect below is only a backstop.+ // `updateSessions`), so a push handled before React re-renders sees what the last write produced. No+ // effect copies `sessions` back in: one flushing after such a push would move the ref to an older list.const sessionsRef = useRef<Session[]>([]);- useEffect(() => {- sessionsRef.current = sessions;- }, [sessions]);// A row write: applied to the ref now, and to state as a functional update, so it builds on every// write queued before it instead of replacing the list with an older copy.const updateSessions = useCallback((update: (list: Session[]) => Session[]) => {@@ -265,8 +262,8 @@ export function Sessions() {(event: { sessionId: string; status: string }) => {const prev = sessionsRef.current.find(s => s.id === event.sessionId);// Some engines double-signal one transition; only react to an ACTUAL status change so the toast- // and the failed-refresh don't fire on every redundant envelope. Update the ref synchronously so- // a duplicate arriving in the same tick (before the sync effect runs) is also caught.+ // and the failed-refresh don't fire on every redundant envelope. `updateSessions` moves the ref+ // synchronously, so a duplicate arriving before React re-renders is also caught.if (prev && prev.status === event.status) return;// A push for a row the page does not hold yet changes nothing, so it must not void the read// that is about to bring that row (the mount read, before any row is on screen).