Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): find a lost control's replacement stop by position
Yudhi Armyndharis378a4eac1edc
diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.tsindex 260f20510..fe74288a3 100644--- a/dashboard/src/components/chats/ChatThread.test.ts+++ b/dashboard/src/components/chats/ChatThread.test.ts@@ -547,13 +547,13 @@ test('keyboard focus stays in the thread when the focused message is 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 => ({+ const [a, b, c, d]: ChatMessageView[] = ['1', '2', '3', '4'].map(n => ({...PROMPT,id: `db-${n}`,waMessageId: `wamid.${n}`,metadata: undefined,}));- const { container, rerenderWith } = renderThread('operator', [a, b, c]);+ const { container, rerenderWith } = renderThread('operator', [a, b, c, d]);const del = bubblesOf(container)[1].querySelector<HTMLElement>('.action-btn:last-of-type');assert.ok(del);del.focus();@@ -564,18 +564,60 @@ test('keyboard focus returns without scrolling, and to the stop that took the ploriginal.call(this, options);};try {- rerenderWith([a, { ...b, type: 'revoked', body: '' }, c]);+ rerenderWith([a, { ...b, type: 'revoked', body: '' }, c, d]);} finally {HTMLElement.prototype.focus = original;}- // The message after the deleted one now holds its place among the stops, and nothing scrolls.+ // The message after the deleted one now holds its place among the stops (not the newest one), 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.+// The place is the bubble's position in the document when it is lost, not an index taken at focus time: an+// older page that landed in between would have made that index name a message far from it.+test('the stop that takes the place is found after an older page landed since the focus', () => {+ const [o1, o2, a, b, c]: ChatMessageView[] = ['o1', 'o2', 'a', 'b', 'c'].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();++ rerenderWith([o1, o2, a, b, c]);+ assert.equal(document.activeElement, del);+ rerenderWith([o1, o2, a, { ...b, type: 'revoked', body: '' }, c]);++ assert.equal(focusedMessage(), 'wamid.c');+});++// Firefox and WebKit leave the focused element active for a frame after it stopped being focusable, so+// the check after the commit finds focus still on the revoked bubble (jsdom never clears it): it has to be+// moved to a stop then, not left for the focusout that follows, which only forgets it.+test('a revoked bubble that still holds focus gives it to the stop that takes its place', () => {+ 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 bubble = bubblesOf(container)[1];+ bubble.focus();+ assert.equal(focusedMessage(), 'wamid.2');++ rerenderWith([a, { ...b, type: 'revoked', body: '' }, c]);++ assert.equal(focusedMessage(), 'wamid.3');+});++// What was remembered is used once. Here the only stop is gone and nothing in the thread can take the+// focus; a message that arrives later, after the place of the old bubble, 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,@@ -588,9 +630,9 @@ test('focus is given back once, not at every later change of the list', () => {assert.ok(del);del.focus();- rerenderWith([]);+ rerenderWith([{ ...a, type: 'revoked', body: '' }]);assert.equal(focusedMessage(), 'body');- rerenderWith([b]);+ rerenderWith([{ ...a, type: 'revoked', body: '' }, b]);assert.equal(focusedMessage(), 'body');});diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsxindex c187c6f6f..9cabde452 100644--- a/dashboard/src/components/chats/ChatThread.tsx+++ b/dashboard/src/components/chats/ChatThread.tsx@@ -121,23 +121,35 @@ function ChatThread({target.focus();};- // 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);+ // The message control that holds keyboard focus, with its bubble and the stops on either side of it.+ // 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. The place is found from the bubble in the+ // document, not from an index taken at focus time, which an older page landing since would have moved.+ const keyboardFocusRef = useRef<{ element: HTMLElement; bubble: HTMLElement; neighbors: HTMLElement[] } | null>(null);useEffect(() => {const held = keyboardFocusRef.current;- if (!held || document.activeElement !== document.body) return;+ if (!held) return;+ const stopOf = (element: HTMLElement) => element.isConnected && element.closest('[data-roving-message]') !== null;+ const stillStop = stopOf(held.element);+ // Lost: focus fell to the page, or it still sits on an element that has just stopped being a stop,+ // which Firefox and WebKit only clear a frame later, in a focusout that would reset what is held here.+ const active = document.activeElement;+ if (active !== document.body && !(active === held.element && !stillStop)) return;keyboardFocusRef.current = null;- if (held.element.isConnected && held.element.tabIndex >= 0) return;+ if (stillStop) return;+ const { bubble } = held;const stops = Array.from(messagesContainerRef.current?.querySelectorAll<HTMLElement>('[data-roving-message]') ?? [],);- stops[Math.min(held.at, stops.length - 1)]?.focus({ preventScroll: true });+ const target = bubble.isConnected+ ? (stops.find(stop => bubble.compareDocumentPosition(stop) & bubble.DOCUMENT_POSITION_FOLLOWING) ??+ [...stops].reverse().find(stop => bubble.compareDocumentPosition(stop) & bubble.DOCUMENT_POSITION_PRECEDING))+ : held.neighbors.find(stop => stop.isConnected && stop.hasAttribute('data-roving-message'));+ target?.focus({ preventScroll: true });}, [messages, hasActions, messagesContainerRef]);// "@<digits>" in a message body only ever means something once resolved against a participant@@ -293,13 +305,18 @@ function ChatThread({onKeyDown={event => event.currentTarget.removeAttribute('data-pointer')}onFocus={event => {const target = event.target;- const bubble = target.closest<HTMLElement>('.message-bubble-container');+ const bubble = target.closest<HTMLElement>('[data-roving-message]');const keyboard = target.matches(':focus-visible');if (keyboard) event.currentTarget.removeAttribute('data-pointer');else event.currentTarget.setAttribute('data-pointer', '');+ if (!keyboard || !bubble) {+ keyboardFocusRef.current = null;+ return;+ }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;+ const at = stops.indexOf(bubble);+ const neighbors = [stops[at + 1], stops[at - 1]].filter((stop): stop is HTMLElement => Boolean(stop));+ keyboardFocusRef.current = { element: target, bubble, neighbors };}}onBlur={event => {if (!event.currentTarget.contains(event.relatedTarget as Node | null)) keyboardFocusRef.current = null;