Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop the unreachable media-viewer branch from the Escape handler

Yudhi Armyndharis2b6c9aea9cf3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 365aa7b27..2a0e3a903 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Webhook and automation filters accept a `chatId` condition, so a webhook can be scoped to specific groups or chats instead of only to a sender ([#1634](https://github.com/rmyndharis/OpenWA/issues/1634)). Thanks @krishshah9944.
-- On the dashboard Chats page, Escape closes the open chat, channel or status viewer and returns to the list. It leaves the key alone while a dialog, a menu or the media viewer is open, since those handle Escape themselves.
+- On the dashboard Chats page, Escape closes the open chat, channel or status viewer and returns to the list. It leaves the key alone while a dialog, a menu or the media viewer is open, since those handle Escape themselves. Thanks @C24212.
### Changed
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index ad8d7796b..51d15eae3 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -504,6 +504,30 @@ test('a typed draft survives closing and reopening the room', async () => {
assert.equal(input.value, 'draft survives');
});
+test('Escape closes the open room, and is left alone while a dialog owns it', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ resetFetchCalls();
+ const { container } = renderChats();
+
+ await screen.findByText('Main (15551234567)');
+ fireEvent.click(await screen.findByText('Alice'));
+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
+
+ // A modal owns Escape while it is open: the room must survive it, or closing a dialog would also
+ // throw away the conversation behind it.
+ const dialog = document.createElement('div');
+ dialog.setAttribute('role', 'dialog');
+ document.body.appendChild(dialog);
+ fireEvent.keyDown(document, { key: 'Escape' });
+ assert.ok(screen.queryByRole('button', { name: 'Back' }), 'Escape closed the room while a dialog was open');
+
+ dialog.remove();
+ fireEvent.keyDown(document, { key: 'Escape' });
+ await waitFor(() =>
+ assert.equal(screen.queryByRole('button', { name: 'Back' }), null, 'Escape did not close the room'),
+ );
+});
+
// Stage a file in the open room and wait for the preview banner. A non-image type is used on
// purpose: the image branch calls URL.createObjectURL, which JSDOM does not implement.
//
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index b42f9ae47..03940ded1 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -790,20 +790,24 @@ export function Chats() {
if (el) el.scrollTop = el.scrollHeight;
}, [activeStatusGroup?.contact.id, activeStatusGroup?.items]);
+ // Escape closes the open view and returns to the list. Anything that owns the key already keeps
+ // it: a modal (Modal renders role="dialog" only while open) and the language menu (role="menu")
+ // are skipped here, and so is the media viewer, whose library renders its own role="dialog"
+ // portal and closes itself on Escape. A handler that called preventDefault, or a composition
+ // still being committed by an IME, is left alone for the same reason.
useEffect(() => {
- const closeTopmostViewOnEscape = (event: KeyboardEvent) => {
+ const closeOpenViewOnEscape = (event: KeyboardEvent) => {
if (event.key !== 'Escape' || event.defaultPrevented || event.isComposing) return;
if (document.querySelector('[role="dialog"], [role="menu"]')) return;
- if (lightboxIndex !== null) setLightboxIndex(null);
- else if (activeStatusContactId !== null) setActiveStatusContactId(null);
+ if (activeStatusContactId !== null) setActiveStatusContactId(null);
else if (activeChannel) setActiveChannel(null);
else if (activeChat) setActiveChat(null);
else return;
event.preventDefault();
};
- document.addEventListener('keydown', closeTopmostViewOnEscape);
- return () => document.removeEventListener('keydown', closeTopmostViewOnEscape);
- }, [lightboxIndex, activeStatusContactId, activeChannel, activeChat]);
+ document.addEventListener('keydown', closeOpenViewOnEscape);
+ return () => document.removeEventListener('keydown', closeOpenViewOnEscape);
+ }, [activeStatusContactId, activeChannel, activeChat]);
// Image media items for the lightbox, in render order. `getMediaSrc` reconstructs a usable src
// from either a base64 payload or a URL — the ChatMessageView shape stores both in `data`.