Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): make the emoji picker actually answer Escape

Yudhi Armyndharis1d2e356fb711
diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsx
index b7487f80b..8e74916dd 100644
--- a/dashboard/src/components/chats/ChatComposer.tsx
+++ b/dashboard/src/components/chats/ChatComposer.tsx
@@ -89,6 +89,25 @@ function ChatComposer({
};
}, [activeChat.id]);
+ // Escape dismisses the emoji picker instead of closing the conversation behind it.
+ //
+ // On `document`, and in the capture phase, because neither alternative works: the picker is a div
+ // with no tabIndex, so an onKeyDown on it fires only when focus is already inside, and after the
+ // toggle button is clicked focus is on the button, outside. Capture also puts this ahead of the
+ // page's own Escape handler whatever order the two listeners were registered in, so the
+ // preventDefault below is what the page reads, and the room stays open without the picker having
+ // to advertise a role it does not implement.
+ useEffect(() => {
+ if (!showEmojiPicker) return;
+ const dismissOnEscape = (event: KeyboardEvent) => {
+ if (event.key !== 'Escape' || event.isComposing) return;
+ event.preventDefault();
+ setShowEmojiPicker(false);
+ };
+ document.addEventListener('keydown', dismissOnEscape, true);
+ return () => document.removeEventListener('keydown', dismissOnEscape, true);
+ }, [showEmojiPicker]);
+
// References
const fileInputRef = useRef<HTMLInputElement | null>(null);
@@ -277,17 +296,10 @@ function ChatComposer({
{/* Popular emojis panel */}
{showEmojiPicker && (
- // role="menu" is what the page's Escape handler skips, and it owes that handler the
- // behaviour the skip assumes: Escape dismisses the picker rather than the conversation.
- <div
- className="chats-emoji-picker"
- role="menu"
- onKeyDown={event => {
- if (event.key !== 'Escape') return;
- event.preventDefault();
- setShowEmojiPicker(false);
- }}
- >
+ // No role="menu" here: these are plain buttons with no menuitem roles, no roving focus and
+ // no arrow-key navigation, so claiming the role would promise a keyboard contract the panel
+ // does not keep. Escape is handled on document instead, see the effect above.
+ <div className="chats-emoji-picker">
<div className="emoji-grid">
{popularEmojis.map(emoji => (
<button key={emoji} type="button" className="emoji-btn" onClick={() => handleEmojiClick(emoji)}>
diff --git a/dashboard/src/hooks/useSessionPairing.ts b/dashboard/src/hooks/useSessionPairing.ts
index 428a37ae1..29c1b7667 100644
--- a/dashboard/src/hooks/useSessionPairing.ts
+++ b/dashboard/src/hooks/useSessionPairing.ts
@@ -192,7 +192,11 @@ export function useSessionPairing({ sessions, sessionsRef, reloadSessions }: Use
// `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.
+ // would throw away a code that works. The guard proves only that the modal is blank right now,
+ // not that it is still the same modal that was blanked. That is enough: the only other way to be
+ // blank is a modal still loading its first code, for a session the answer just said has no engine,
+ // and closing that one is right too. A code arriving AFTER the answer is not covered, and cannot
+ // be from here; the modal is gone by then and the operator reopens it from the card.
const dismissQrForSession = useCallback((sessionId: string, onlyIfBlank = false) => {
setQrData(current => {
if (current?.sessionId !== sessionId) return current;
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 5c54930bc..7856fe235 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -514,22 +514,18 @@ test('Escape dismisses the emoji picker instead of the conversation behind it',
await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
fireEvent.click(screen.getByTitle('Pick emoji'));
- const picker = await waitFor(() => {
- const found = container.querySelector('.chats-emoji-picker');
- assert.ok(found, 'the emoji picker did not open');
- return found as HTMLElement;
- });
+ await waitFor(() => assert.ok(container.querySelector('.chats-emoji-picker'), 'the emoji picker did not open'));
- // The page-level handler listens on document and skips anything with role="menu", so an Escape it
- // sees while the picker is open must leave the conversation alone.
+ // Driven from document, which is where a real Escape lands: focus is on the toggle button, not
+ // inside the picker, so a handler bound to the picker element would never see this event. One
+ // press must do both things, dismiss the picker and leave the conversation open.
fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() => assert.equal(container.querySelector('.chats-emoji-picker'), null, 'the picker stayed open'));
assert.ok(screen.queryByRole('button', { name: 'Back' }), 'Escape closed the room while the picker owned it');
- assert.ok(container.querySelector('.chats-emoji-picker'), 'the picker closed on a document-level Escape');
- // And the picker owes that skip the behaviour it assumes: Escape on the picker dismisses it.
- fireEvent.keyDown(picker, { key: 'Escape' });
- await waitFor(() => assert.equal(container.querySelector('.chats-emoji-picker'), null, 'the picker stayed open'));
- assert.ok(screen.queryByRole('button', { name: 'Back' }), 'dismissing the picker also closed the room');
+ // With the picker gone the key belongs to the room again, which is what it must not keep.
+ fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() => assert.equal(screen.queryByRole('button', { name: 'Back' }), null, 'the room stayed open'));
});
test('Escape closes the open room, and is left alone while a dialog owns it', async () => {
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.ts
index ee1538e09..419f910f2 100644
--- a/dashboard/src/pages/Sessions.test.ts
+++ b/dashboard/src/pages/Sessions.test.ts
@@ -814,6 +814,9 @@ test('a QR pushed while the disconnect re-read is in flight keeps the modal open
// 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 });
+ // Counted BEFORE the push: the mount already read the list once, so waiting for "a GET happened"
+ // would be satisfied by that one and would settle before the handler's own re-read resolves.
+ const readsBeforeDisconnect = fetchCalls.filter(c => c.method === 'GET' && c.path === '/api/sessions').length;
pushSessionStatus(row.id, 'disconnected');
// A fresh code lands before that answer is applied.
@@ -831,7 +834,16 @@ test('a QR pushed while the disconnect re-read is in flight keeps the modal open
});
});
- await waitFor(() => assert.ok(findFetchCall('GET', '/api/sessions')));
+ // The fresh code is on screen, so the push really landed in the modal that is being judged.
+ await waitFor(() =>
+ assert.equal((screen.getByAltText('QR') as HTMLImageElement).src, 'data:image/png;base64,FRESH'),
+ );
+ // And the handler's re-read has resolved, so the close decision has already been taken.
+ await waitFor(() =>
+ assert.ok(
+ fetchCalls.filter(c => c.method === 'GET' && c.path === '/api/sessions').length > readsBeforeDisconnect,
+ ),
+ );
assert.ok(screen.queryByRole('dialog'), 'the modal closed over a QR code that had just arrived');
} finally {
SESSIONS.pop();