Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): give the session switcher list one tab stop

Yudhi Armyndharis1c75eb7acc58
diff --git a/dashboard/src/components/chats/SessionPicker.test.ts b/dashboard/src/components/chats/SessionPicker.test.ts
new file mode 100644
index 000000000..acd746922
--- /dev/null
+++ b/dashboard/src/components/chats/SessionPicker.test.ts
@@ -0,0 +1,125 @@
+// The session switcher's list is a listbox: one option is in the tab order and the arrow keys move between them, and
+// the text that says nothing matches sits outside the list, which may hold options only.
+import '../../test-helpers/register-hooks.ts';
+import { test, before, afterEach } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+import type { Session } from '../../services/api.ts';
+
+let rtl: typeof import('@testing-library/react');
+let SessionPicker: (typeof import('./SessionPicker.tsx'))['default'];
+let QueryClientProvider: (typeof import('@tanstack/react-query'))['QueryClientProvider'];
+let QueryClient: (typeof import('@tanstack/react-query'))['QueryClient'];
+
+const session = (id: string, name: string, status: Session['status'] = 'ready'): Session => ({
+ id,
+ name,
+ status,
+ phone: null,
+ createdAt: '2026-01-01T00:00:00.000Z',
+ updatedAt: '2026-01-01T00:00:00.000Z',
+});
+
+// Ann is down, so of the four only Ben, Cleo and Dan can be picked; Cleo is the one selected.
+const SESSIONS = [session('a', 'Ann', 'disconnected'), session('b', 'Ben'), session('c', 'Cleo'), session('d', 'Dan')];
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ // The avatars ask for each ready account's picture; none has one.
+ globalThis.fetch = (() =>
+ Promise.resolve(
+ new Response(JSON.stringify({ url: null }), { headers: { 'Content-Type': 'application/json' } }),
+ )) as typeof fetch;
+ const { i18nReady } = await import('../../i18n/index.ts');
+ await i18nReady;
+ rtl = await import('@testing-library/react');
+ ({ QueryClient, QueryClientProvider } = await import('@tanstack/react-query'));
+ ({ default: SessionPicker } = await import('./SessionPicker.tsx'));
+});
+
+// Each client is cleared after its test: a picture read still out, or a cache entry's collection timer, keeps the
+// runner from exiting.
+const clients: InstanceType<typeof QueryClient>[] = [];
+afterEach(async () => {
+ rtl.cleanup();
+ for (const client of clients.splice(0)) {
+ await client.cancelQueries();
+ client.clear();
+ }
+});
+
+function renderPicker(selectedSessionId = 'c') {
+ const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } } });
+ clients.push(client);
+ return rtl.render(
+ createElement(
+ QueryClientProvider,
+ { client },
+ createElement(SessionPicker, {
+ sessions: SESSIONS,
+ selectedSessionId,
+ onSelectSession: () => undefined,
+ canAddSession: false,
+ }),
+ ),
+ );
+}
+
+const open = () => rtl.fireEvent.click(document.querySelector('.session-switcher-btn') as HTMLElement);
+const tabStops = () =>
+ rtl.screen
+ .getAllByRole('option')
+ .filter(option => option.tabIndex === 0)
+ .map(option => option.querySelector('.session-name')?.textContent);
+const focusedName = () =>
+ (document.activeElement as HTMLElement | null)?.querySelector('.session-name')?.textContent ??
+ document.activeElement?.getAttribute('aria-label');
+
+test('only the selected session is in the tab order, and the arrow keys move focus and the tab stop with it', () => {
+ renderPicker();
+ open();
+ assert.deepEqual(tabStops(), ['Cleo']);
+
+ const search = rtl.screen.getByLabelText('Search sessions...');
+ rtl.fireEvent.keyDown(search, { key: 'ArrowDown' });
+ assert.equal(focusedName(), 'Ben', 'the first option that can be picked did not take focus');
+ assert.deepEqual(tabStops(), ['Ben']);
+ rtl.fireEvent.keyDown(document.activeElement as HTMLElement, { key: 'ArrowDown' });
+ rtl.fireEvent.keyDown(document.activeElement as HTMLElement, { key: 'ArrowDown' });
+ assert.equal(focusedName(), 'Dan');
+ assert.deepEqual(tabStops(), ['Dan']);
+ rtl.fireEvent.keyDown(document.activeElement as HTMLElement, { key: 'ArrowUp' });
+ rtl.fireEvent.keyDown(document.activeElement as HTMLElement, { key: 'ArrowUp' });
+ rtl.fireEvent.keyDown(document.activeElement as HTMLElement, { key: 'ArrowUp' });
+ assert.equal(focusedName(), 'Search sessions...', 'ArrowUp from the first option did not return to the search');
+});
+
+test('the tab stop falls to the first session that can be picked when the selected one cannot be or is not listed', () => {
+ renderPicker('a');
+ open();
+ assert.deepEqual(tabStops(), ['Ben'], 'a session that cannot be picked holds the tab stop');
+ rtl.fireEvent.change(rtl.screen.getByLabelText('Search sessions...'), { target: { value: 'd' } });
+ assert.deepEqual(tabStops(), ['Dan']);
+});
+
+test('the tab stop starts at the selected session again each time the list opens', () => {
+ renderPicker();
+ open();
+ rtl.fireEvent.keyDown(rtl.screen.getByLabelText('Search sessions...'), { key: 'ArrowDown' });
+ assert.deepEqual(tabStops(), ['Ben']);
+ rtl.fireEvent.keyDown(document, { key: 'Escape' });
+ assert.equal(document.activeElement?.getAttribute('title'), 'Switch session', 'Escape did not return focus');
+ open();
+ assert.deepEqual(tabStops(), ['Cleo']);
+});
+
+test('a search with no match says so outside the listbox, which holds options only', () => {
+ renderPicker();
+ open();
+ rtl.fireEvent.change(rtl.screen.getByLabelText('Search sessions...'), { target: { value: 'zz' } });
+ const listbox = rtl.screen.getByRole('listbox');
+ assert.equal(listbox.children.length, 0, 'the listbox holds something that is no option');
+ assert.equal(listbox.contains(rtl.screen.getByText('No session matches')), false);
+ assert.equal(rtl.screen.getByRole('textbox').getAttribute('aria-controls'), listbox.id);
+});
diff --git a/dashboard/src/components/chats/SessionPicker.tsx b/dashboard/src/components/chats/SessionPicker.tsx
index e9030e412..fc97b319f 100644
--- a/dashboard/src/components/chats/SessionPicker.tsx
+++ b/dashboard/src/components/chats/SessionPicker.tsx
@@ -108,6 +108,8 @@ function SessionPicker({ sessions, selectedSessionId, onSelectSession, canAddSes
const inRouter = useInRouterContext();
const [open, setOpen] = useState(false);
const [query, setQuery] = useState('');
+ // The option focus last reached, which holds the list's one tab stop while it is listed.
+ const [focusedId, setFocusedId] = useState<string | null>(null);
const rootRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLButtonElement | null>(null);
const listRef = useRef<HTMLUListElement | null>(null);
@@ -123,6 +125,14 @@ function SessionPicker({ sessions, selectedSessionId, onSelectSession, canAddSes
(digits.length > 0 && (s.phone ?? '').includes(digits)),
)
: sessions;
+ // A roving tab stop: one option is in the tab order, the one focus last reached, else the selected session, else the
+ // first that can be picked. The arrow keys move between the options and take the tab stop along.
+ const pickable = shown.filter(s => s.status === 'ready');
+ const tabStop = (
+ pickable.find(s => s.id === focusedId) ??
+ pickable.find(s => s.id === selectedSessionId) ??
+ pickable[0]
+ )?.id;
const close = (refocus: boolean) => {
setOpen(false);
@@ -187,7 +197,14 @@ function SessionPicker({ sessions, selectedSessionId, onSelectSession, canAddSes
ref={triggerRef}
type="button"
className="session-switcher-btn"
- onClick={() => (open ? close(false) : setOpen(true))}
+ onClick={() => {
+ if (open) {
+ close(false);
+ } else {
+ setFocusedId(null);
+ setOpen(true);
+ }
+ }}
aria-haspopup="listbox"
aria-expanded={open}
aria-controls={open ? listId : undefined}
@@ -224,45 +241,45 @@ function SessionPicker({ sessions, selectedSessionId, onSelectSession, canAddSes
aria-label={t('chats.sessionLabel')}
ref={listRef}
>
- {shown.length === 0 ? (
- <li className="session-picker-empty">{t('chats.sessionPicker.noMatch')}</li>
- ) : (
- shown.map(session => {
- const isSelected = session.id === selectedSessionId;
- const ready = session.status === 'ready';
- return (
- <li key={session.id} role="presentation">
- <button
- type="button"
- role="option"
- aria-selected={isSelected}
- disabled={!ready}
- className={`session-picker-option ${isSelected ? 'selected' : ''}`}
- onClick={() => pick(session)}
- title={ready ? undefined : t(`sessionStatus.${session.status}`)}
- >
- <SessionAvatar session={session} size="sm" />
- <span className="session-text">
- <span className="session-name">{session.name}</span>
- <span className={`session-phone${session.pushName ? ' with-push-name' : ''}`}>
- {session.pushName && (
- <>
- <bdi>{session.pushName}</bdi>
- <span className="session-push-name-sep" aria-hidden="true">
- {' · '}
- </span>
- </>
- )}
- <PhoneLine phone={session.phone} />
- </span>
+ {shown.map(session => {
+ const isSelected = session.id === selectedSessionId;
+ const ready = session.status === 'ready';
+ return (
+ <li key={session.id} role="presentation">
+ <button
+ type="button"
+ role="option"
+ aria-selected={isSelected}
+ disabled={!ready}
+ tabIndex={session.id === tabStop ? 0 : -1}
+ className={`session-picker-option ${isSelected ? 'selected' : ''}`}
+ onFocus={() => setFocusedId(session.id)}
+ onClick={() => pick(session)}
+ title={ready ? undefined : t(`sessionStatus.${session.status}`)}
+ >
+ <SessionAvatar session={session} size="sm" />
+ <span className="session-text">
+ <span className="session-name">{session.name}</span>
+ <span className={`session-phone${session.pushName ? ' with-push-name' : ''}`}>
+ {session.pushName && (
+ <>
+ <bdi>{session.pushName}</bdi>
+ <span className="session-push-name-sep" aria-hidden="true">
+ {' · '}
+ </span>
+ </>
+ )}
+ <PhoneLine phone={session.phone} />
</span>
- {isSelected && <Check size={18} className="session-check" />}
- </button>
- </li>
- );
- })
- )}
+ </span>
+ {isSelected && <Check size={18} className="session-check" />}
+ </button>
+ </li>
+ );
+ })}
</ul>
+ {/* Outside the list, which may hold options only. */}
+ {shown.length === 0 && <div className="session-picker-empty">{t('chats.sessionPicker.noMatch')}</div>}
{canAddSession && inRouter && <AddSessionLink onFollow={() => close(false)} />}
</div>
)}