Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not move a reader when a thread control goes away
Yudhi Armyndharis8eefc986b803
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 43ec67275..475cd1dc1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Bound each webhook and ingress queue command at 2 s once Redis has connected, so a Redis that stops answering falls back to direct delivery and inline dispatch instead of holding every dispatch and ingress request.- Keep the dashboard chat's scroll-to-bottom button in view while the reader is scrolled up, where it moved with the thread and was off screen.+- Keep the dashboard chat reader's own scroll while an older page loads, where a scroll made in the meantime was undone and the scroll-to-bottom button could end far above the bottom.- Refuse to save a local `storage.localPath` the app cannot create or write, where the next start refused to boot and the dashboard that could undo the save was down.- Answer `400` for a send route's or a product send's `chatId`, or a bulk `batchId`, over 255 characters, where PostgreSQL answered `500` once the value passed its index entry limit.- Answer `400` for a `chatId` over 255 characters in the request body of a chat action, as the send routes do, instead of handing it to the engine.diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.tsindex 52082667d..260f20510 100644--- a/dashboard/src/components/chats/ChatThread.test.ts+++ b/dashboard/src/components/chats/ChatThread.test.ts@@ -2,7 +2,7 @@// prompt-button tap only for an operator key, so a read-only key must not be offered them. It also// checks the thread, not just the helpers, resolves an @mention in a body and in a quote.import '../../test-helpers/register-hooks.ts';-import { test, before, afterEach } from 'node:test';+import { test, before, after, afterEach } from 'node:test';import assert from 'node:assert/strict';import { readFileSync } from 'node:fs';import { createElement, createRef } from 'react';@@ -12,6 +12,7 @@ import type { ChatMessageView } from '../../utils/chatMessages.ts';type RTL = typeof import('@testing-library/react');let rtl: RTL;+let matchesOriginal: typeof Element.prototype.matches;let ChatThread: typeof import('./ChatThread.tsx').default;let RoleProvider: typeof import('../RoleProvider.tsx').RoleProvider;@@ -23,6 +24,18 @@ before(async () => {rtl = await import('@testing-library/react');({ RoleProvider } = await import('../RoleProvider.tsx'));({ default: ChatThread } = await import('./ChatThread.tsx'));+ // jsdom answers :focus-visible from the last key or pointer event that any test fired before, so the same+ // call matched in one run and not in the next. Here it holds for the element that has focus, as for the+ // focus a key press brings, and not for a focus event on an element that is not the focused one, as for+ // the focus of a click.+ matchesOriginal = window.Element.prototype.matches;+ window.Element.prototype.matches = function (this: Element, selector: string): boolean {+ return selector === ':focus-visible' ? this === document.activeElement : matchesOriginal.call(this, selector);+ } as typeof Element.prototype.matches;+});++after(() => {+ window.Element.prototype.matches = matchesOriginal;});afterEach(() => {@@ -455,6 +468,16 @@ const bubblesOf = (container: HTMLElement): HTMLElement[] => [...container.querySelectorAll<HTMLElement>('[data-roving-message]'),];+/**+ * Where focus is, as text: the id of the message whose bubble or control holds it, 'body' when it fell to+ * the page, 'elsewhere' for any other element. A failed assert.equal on two DOM nodes spends minutes+ * printing them, so focus is compared by name.+ */+const focusedMessage = (): string =>+ document.activeElement === document.body+ ? 'body'+ : (document.activeElement?.closest('[data-wa-message-id]')?.getAttribute('data-wa-message-id') ?? 'elsewhere');+// A bubble is the one tab stop of the thread; what is inside it (a link, a preview, a button) is a stop// of its own, and landing on it must not make its bubble the stop as well, or Shift+Tab visits both.test('focus on a link inside a message does not move the thread tab stop', () => {@@ -523,7 +546,94 @@ test('keyboard focus stays in the thread when the focused message is deleted', (assert.ok(container.contains(active), 'focus left the thread');});-test('focus does not return to the thread when it was moved elsewhere before a message was deleted', () => {+test('keyboard focus returns without scrolling, and to the stop that took the place of the deleted message', () => {+ const [a, b, c]: ChatMessageView[] = ['1', '2', '3'].map(n => ({+ ...PROMPT,+ id: `db-${n}`,+ waMessageId: `wamid.${n}`,+ metadata: undefined,+ }));+ const { container, rerenderWith } = renderThread('operator', [a, b, c]);+ const del = bubblesOf(container)[1].querySelector<HTMLElement>('.action-btn:last-of-type');+ assert.ok(del);+ del.focus();+ const original = HTMLElement.prototype.focus;+ const calls: Array<{ id: string | null; options?: FocusOptions }> = [];+ HTMLElement.prototype.focus = function (this: HTMLElement, options?: FocusOptions) {+ calls.push({ id: this.closest('[data-wa-message-id]')?.getAttribute('data-wa-message-id') ?? null, options });+ original.call(this, options);+ };+ try {+ rerenderWith([a, { ...b, type: 'revoked', body: '' }, c]);+ } finally {+ HTMLElement.prototype.focus = original;+ }++ // The message after the deleted one now holds its place among the stops, and nothing scrolls.+ assert.deepEqual(calls, [{ id: 'wamid.3', options: { preventScroll: true } }]);+ assert.equal(focusedMessage(), 'wamid.3');+});++// What was remembered is used once. Here the stop is gone and nothing in the thread can take the focus, so+// it stays on the page; a message that arrives later does not draw it in.+test('focus is given back once, not at every later change of the list', () => {+ const [a, b]: ChatMessageView[] = ['1', '2'].map(n => ({+ ...PROMPT,+ id: `db-${n}`,+ waMessageId: `wamid.${n}`,+ metadata: undefined,+ }));+ const { container, rerenderWith } = renderThread('operator', [a]);+ const del = bubblesOf(container)[0].querySelector<HTMLElement>('.action-btn:last-of-type');+ assert.ok(del);+ del.focus();++ rerenderWith([]);+ assert.equal(focusedMessage(), 'body');+ rerenderWith([b]);++ assert.equal(focusedMessage(), 'body');+});++// A control that is not a message holds no memory: when it goes away (the jump button once the reader is at+// the bottom) React delivers no blur, and a later change of the list used to take focus to the newest+// bubble, which scrolled a reader who had gone back up to read history down to it.+test('a thread control that is removed does not make a later change of the list take focus', () => {+ const first: ChatMessageView = { ...PROMPT, id: 'db-1', waMessageId: 'wamid.1', metadata: undefined };+ const second: ChatMessageView = { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2', metadata: undefined };+ const { container, rerenderWith } = renderThread('operator', [first]);+ const thread = container.querySelector<HTMLElement>('.room-messages');+ assert.ok(thread);+ const control = document.createElement('button');+ thread.appendChild(control);+ control.focus();+ control.remove();+ assert.equal(document.activeElement, document.body);++ rerenderWith([first, second]);+ rerenderWith([first, { ...second, status: 'read' }]);++ assert.equal(focusedMessage(), 'body');+});++test('a focus a pointer brought to an action is not given back, and marks pointer use', () => {+ const first: ChatMessageView = { ...PROMPT, id: 'db-1', waMessageId: 'wamid.1', metadata: undefined };+ const second: ChatMessageView = { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2', metadata: undefined };+ const { container, rerenderWith } = renderThread('operator', [first, second]);+ const thread = container.querySelector<HTMLElement>('.room-messages');+ assert.ok(thread);+ const del = bubblesOf(container)[1].querySelector<HTMLElement>('.action-btn:last-of-type');+ assert.ok(del);+ // The focusin of a mouse click: the element is not the focused one, so it is not :focus-visible.+ rtl.fireEvent.focusIn(del);+ assert.equal(thread.hasAttribute('data-pointer'), true);++ rerenderWith([first, { ...second, type: 'revoked', body: '' }]);++ assert.equal(focusedMessage(), 'body');+});++test('focus that is moved elsewhere before a message was deleted stays there', () => {const first: ChatMessageView = { ...PROMPT, id: 'db-1', waMessageId: 'wamid.1', metadata: undefined };const second: ChatMessageView = { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2', metadata: undefined };const { container, rerenderWith } = renderThread('operator', [first, second]);@@ -533,7 +643,31 @@ test('focus does not return to the thread when it was moved elsewhere before a mbubblesOf(container)[1].focus();outside.focus();rerenderWith([first, { ...second, type: 'revoked', body: '' }]);- assert.equal(document.activeElement, outside);+ assert.ok(document.activeElement === outside, 'focus was taken from the element it was moved to');+ } finally {+ outside.remove();+ }+});++// Focus that went to another control and was lost from there (the control closed, as a dialog does) is not+// the thread's to take back: what the thread remembered ended when focus left it.+test('focus that left the thread and was then lost is not taken back into it', () => {+ const first: ChatMessageView = { ...PROMPT, id: 'db-1', waMessageId: 'wamid.1', metadata: undefined };+ const second: ChatMessageView = { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2', metadata: undefined };+ const { container, rerenderWith } = renderThread('operator', [first, second]);+ const del = bubblesOf(container)[1].querySelector<HTMLElement>('.action-btn:last-of-type');+ assert.ok(del);+ const outside = document.createElement('input');+ document.body.appendChild(outside);+ try {+ del.focus();+ outside.focus();+ outside.remove();+ assert.equal(focusedMessage(), 'body');++ rerenderWith([first, { ...second, type: 'revoked', body: '' }]);++ assert.equal(focusedMessage(), 'body');} finally {outside.remove();}@@ -565,13 +699,47 @@ test('a click marks pointer use, a key press clears it, and the stylesheet revea// The button is a child of the scroller. Absolutely positioned there it moves with the content, which put it// at the bottom of the first screen of the thread and off screen whenever it was shown. Sticky, as the last-// child, it holds to the bottom of the visible pane. jsdom has no layout, so the declaration is pinned.-test('the scroll-to-bottom button is sticky inside the scroller, not absolute', () => {+// child, it holds to the bottom of the visible pane. jsdom has no layout, so the declarations are pinned:+// the negative margin takes back the room an in-flow button would add to the scroll height, align-self puts+// it at the logical end, and it sits below the actions menu (z-index 5) it would otherwise cover.+test('the scroll-to-bottom button is sticky inside the scroller, not absolute, and below the actions menu', () => {const css = readFileSync(new URL('../../pages/Chats.css', import.meta.url), 'utf8');const rule = /\.chats-page \.scroll-to-bottom-btn \{([^}]*)\}/.exec(css)?.[1] ?? '';assert.match(rule, /position:\s*sticky;/);assert.match(rule, /bottom:\s*1rem;/);+ assert.match(rule, /align-self:\s*flex-end;/);+ assert.match(rule, /margin-top:\s*calc\(-40px - 0\.75rem\);/);assert.doesNotMatch(rule, /position:\s*absolute/);+ const menu = /\.chats-page \.message-actions-menu \{([^}]*)\}/.exec(css)?.[1] ?? '';+ const level = (text: string): number => Number(/z-index:\s*(\d+);/.exec(text)?.[1]);+ assert.ok(level(rule) < level(menu), 'the jump button paints over the actions menu');+});++test('the scroll-to-bottom button is the last child of the scroller and jumps without animating', async () => {+ const messages: ChatMessageView[] = ['1', '2'].map(n => ({+ ...PROMPT,+ id: `db-${n}`,+ waMessageId: `wamid.${n}`,+ metadata: undefined,+ }));+ const { container } = renderThread('operator', messages);+ const thread = container.querySelector<HTMLElement>('.room-messages');+ assert.ok(thread);+ Object.defineProperty(thread, 'scrollHeight', { value: 2000, configurable: true });+ Object.defineProperty(thread, 'clientHeight', { value: 400, configurable: true });+ const jumps: ScrollToOptions[] = [];+ thread.scrollTo = ((options: ScrollToOptions) => void jumps.push(options)) as typeof thread.scrollTo;+ rtl.fireEvent.scroll(thread);++ const button = await rtl.waitFor(() => {+ const found = thread.querySelector<HTMLElement>('.scroll-to-bottom-btn');+ assert.ok(found, 'the jump button did not show');+ return found;+ });+ assert.equal(thread.lastElementChild, button);+ rtl.fireEvent.click(button);+ // A smooth jump is cancelled by an older page that lands during the animation.+ assert.deepEqual(jumps, [{ top: 2000 }]);});// A Tab from the sidebar into the thread is a key press the thread never sees, so a click made in the threaddiff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsxindex 8c8375aec..c187c6f6f 100644--- a/dashboard/src/components/chats/ChatThread.tsx+++ b/dashboard/src/components/chats/ChatThread.tsx@@ -121,13 +121,23 @@ function ChatThread({target.focus();};- // Whether focus was last inside the thread. When the focused message turns revoked its bubble loses its- // tab stop and its action buttons unmount, which drops focus to the page; no blur event is fired for a- // removed element, so this stays true and tells that case from a click elsewhere.- const focusInThreadRef = useRef(false);+ // The message control that holds keyboard focus, with the place of its bubble among the stops. When that+ // control is the one that goes away (a deleted message's actions unmount, or its bubble loses its tab+ // stop) focus falls to the page, and the next Tab would land in the composer: it moves to the stop that+ // took the place of the bubble instead. React delivers no blur for an element it removes, so the element+ // is checked after the commit, and only once: a later change of the list never moves focus, and the+ // position of a reader who scrolled away is never touched (preventScroll). Focus a pointer brought in+ // is not recorded, so a mouse click on Delete moves nothing.+ const keyboardFocusRef = useRef<{ element: HTMLElement; at: number } | null>(null);useEffect(() => {- if (!focusInThreadRef.current || document.activeElement !== document.body) return;- messagesContainerRef.current?.querySelector<HTMLElement>('[data-roving-message][tabindex="0"]')?.focus();+ const held = keyboardFocusRef.current;+ if (!held || document.activeElement !== document.body) return;+ keyboardFocusRef.current = null;+ if (held.element.isConnected && held.element.tabIndex >= 0) return;+ const stops = Array.from(+ messagesContainerRef.current?.querySelectorAll<HTMLElement>('[data-roving-message]') ?? [],+ );+ stops[Math.min(held.at, stops.length - 1)]?.focus({ preventScroll: true });}, [messages, hasActions, messagesContainerRef]);// "@<digits>" in a message body only ever means something once resolved against a participant@@ -249,7 +259,10 @@ function ChatThread({const handleJumpToBottom = useCallback(() => {const el = messagesContainerRef.current;if (!el) return;- el.scrollTo({ top: el.scrollHeight, behavior: 'smooth' });+ // Instant, not smooth: an older page that lands during the animation writes the scroll position and+ // cancels it, which left the reader thousands of pixels above the bottom. The landing keeps an instant+ // jump in place (useChatScrollPosition).+ el.scrollTo({ top: el.scrollHeight });}, [messagesContainerRef]);// Reset the jump button whenever the active chat changes: the new chat's content is restored by@@ -273,17 +286,23 @@ function ChatThread({ref={messagesContainerRef}// The actions show while focus is inside a bubble, but only for keyboard focus: a click also focuses// the bubble, and its menu would stay drawn over the message after the pointer left. The marker is- // read by Chats.css. Focus that a key press brings in from outside the thread clears it as well: the- // key was pressed elsewhere, so no keydown reached this element, and the marker a click left would- // keep the menu of the focused bubble hidden until the next key.+ // read by Chats.css. It follows the focus itself as well as the pointer and the keys: a key pressed+ // outside the thread is never seen here, and a click on a native audio or video control sends the+ // page no pointer event at all, though it focuses the element.onPointerDown={event => event.currentTarget.setAttribute('data-pointer', '')}onKeyDown={event => event.currentTarget.removeAttribute('data-pointer')}onFocus={event => {- focusInThreadRef.current = true;- if (event.target.matches(':focus-visible')) event.currentTarget.removeAttribute('data-pointer');+ const target = event.target;+ const bubble = target.closest<HTMLElement>('.message-bubble-container');+ const keyboard = target.matches(':focus-visible');+ if (keyboard) event.currentTarget.removeAttribute('data-pointer');+ else event.currentTarget.setAttribute('data-pointer', '');+ const stops = Array.from(event.currentTarget.querySelectorAll<HTMLElement>('[data-roving-message]'));+ const at = bubble ? stops.indexOf(bubble) : -1;+ keyboardFocusRef.current = keyboard && at >= 0 ? { element: target, at } : null;}}onBlur={event => {- if (!event.currentTarget.contains(event.relatedTarget as Node | null)) focusInThreadRef.current = false;+ if (!event.currentTarget.contains(event.relatedTarget as Node | null)) keyboardFocusRef.current = null;}}>{/* Older-page spinner, or the failure in its place. An in-flow sibling before the thread, notdiff --git a/dashboard/src/pages/Chats.css b/dashboard/src/pages/Chats.cssindex cd40630cd..73e9f4ce6 100644--- a/dashboard/src/pages/Chats.css+++ b/dashboard/src/pages/Chats.css@@ -458,14 +458,16 @@sat at the bottom of the first screen of the thread, off screen whenever it was shown. In flow itwould add its own height and one flex gap to the thread, which the negative margin takes back, sothe scroll height is the same with and without it (the gap is the 0.75rem of .room-messages). The- right edge is the scroller's padding, clear of the scrollbar. */+ right edge is the scroller's padding, clear of the scrollbar. It sits below a bubble's actions menu+ (z-index 5), which it would otherwise cover: it is later in the DOM, and the Delete icon of a message+ near the bottom edge of the pane lies under it. */.chats-page .scroll-to-bottom-btn {position: sticky;bottom: 1rem;align-self: flex-end;flex: none;margin-top: calc(-40px - 0.75rem);- z-index: 5;+ z-index: 4;width: 40px;height: 40px;border-radius: 50%;