Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): count a pointer release toward its focus window
Yudhi Armyndharise821df63b0e4
diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.tsindex d405c2850..b68f6e223 100644--- a/dashboard/src/components/chats/ChatThread.test.ts+++ b/dashboard/src/components/chats/ChatThread.test.ts@@ -871,6 +871,81 @@ test('focus given back after a press outside the thread is pointer focus, until}});+// A click acts on release, so the lightbox gives focus back when the press is let go, not when it went down. A+// press held for longer than the window is still the pointer's, and the window runs from the last pointer event.+test('focus given back on the release of a long press is pointer focus, counted from the release', () => {+ const { container } = renderThread('operator', [PROMPT, { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2' }]);+ const thread = container.querySelector<HTMLElement>('.room-messages');+ assert.ok(thread);+ const close = document.createElement('button');+ document.body.appendChild(close);+ const clock = holdClock();+ try {+ rtl.fireEvent.pointerDown(close);+ clock.advance(1400);+ rtl.fireEvent.pointerUp(close);+ clock.advance(300);+ rtl.fireEvent.focusIn(bubblesOf(container)[0]);+ assert.equal(thread.hasAttribute('data-pointer'), true);++ rtl.fireEvent.pointerDown(close);+ clock.advance(1400);+ rtl.fireEvent.pointerUp(close);+ clock.advance(1500);+ rtl.fireEvent.focusIn(bubblesOf(container)[1]);+ assert.equal(thread.hasAttribute('data-pointer'), false);+ } finally {+ close.remove();+ }+});++// A lightbox or a menu may stop its pointer events from bubbling: the page still has to see them.+test('the pointer is noted before a handler that stops its events from reaching the document', () => {+ const { container } = renderThread('operator', [PROMPT, { ...PROMPT, id: 'db-2', waMessageId: 'wamid.2' }]);+ const thread = container.querySelector<HTMLElement>('.room-messages');+ assert.ok(thread);+ const close = document.createElement('button');+ for (const type of ['pointerdown', 'pointerup']) close.addEventListener(type, event => event.stopPropagation());+ document.body.appendChild(close);+ const clock = holdClock();+ try {+ rtl.fireEvent.pointerDown(close);+ clock.advance(1400);+ rtl.fireEvent.pointerUp(close);+ clock.advance(300);+ rtl.fireEvent.focusIn(bubblesOf(container)[0]);+ assert.equal(thread.hasAttribute('data-pointer'), true);+ } finally {+ close.remove();+ }+});++test('the thread stops listening for the pointer on the document when it unmounts', () => {+ const original = { add: document.addEventListener, remove: document.removeEventListener };+ const listening = new Set<string>();+ mock.method(+ document,+ 'addEventListener',+ function (this: Document, ...args: Parameters<Document['addEventListener']>) {+ if (args[0].startsWith('pointer') && args[2] === true) listening.add(args[0]);+ return original.add.apply(this, args);+ },+ );+ mock.method(+ document,+ 'removeEventListener',+ function (this: Document, ...args: Parameters<Document['removeEventListener']>) {+ if (args[2] === true) listening.delete(args[0]);+ return original.remove.apply(this, args);+ },+ );+ renderThread('operator');+ assert.deepEqual([...listening].sort(), ['pointerdown', 'pointerup']);++ rtl.cleanup();+ assert.deepEqual([...listening], []);+});+// A click on the controls of an audio or video element sends the page no pointer event at all.test('focus on a media control is pointer focus without any press seen', () => {const voice: ChatMessageView = {diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsxindex 0847c2fdd..2c45ad98c 100644--- a/dashboard/src/components/chats/ChatThread.tsx+++ b/dashboard/src/components/chats/ChatThread.tsx@@ -59,10 +59,10 @@ interface ChatThreadProps {}/**- * How long after a pointer press a focus that is not :focus-visible still counts as the press's own. A click- * focuses its target in the same task as its pointerdown, and the lightbox's close button gives focus back to- * the bubble that opened it as it goes away; a focus long after a press is a script's or an assistive- * technology's.+ * How long after the pointer last went down or came up a focus that is not :focus-visible still counts as the+ * pointer's. A click focuses its target in the same task as its pointerdown, and the lightbox's close button+ * gives focus back to the bubble that opened it as it goes away, on the release of the press however long it+ * was held; a focus long after the pointer's last event is a script's or an assistive technology's.*/const POINTER_FOCUS_WINDOW_MS = 1000;@@ -156,15 +156,20 @@ function ChatThread({// comes back. The blur arrives after confirm() has returned, so the handler that opened the dialog cannot put// the place back itself.const deactivatedRef = useRef<EventTarget | null>(null);- // When the pointer last went down anywhere in the page (performance.now()); see POINTER_FOCUS_WINDOW_MS. Not- // only in the thread: the press that closes the lightbox is outside it, and the focus it gives back is one.+ // When the pointer last went down or came up anywhere in the page (performance.now()); see+ // POINTER_FOCUS_WINDOW_MS. Not only in the thread: the press that closes the lightbox is outside it, and the+ // focus it gives back is one. The release counts as well as the press: a click acts on release, so a press+ // that was held for longer than the window would otherwise be over before its own focus arrived.const pointerAtRef = useRef<number>(Number.NEGATIVE_INFINITY);useEffect(() => {- const notePress = () => {+ const notePointer = () => {pointerAtRef.current = performance.now();};- document.addEventListener('pointerdown', notePress, true);- return () => document.removeEventListener('pointerdown', notePress, true);+ const types = ['pointerdown', 'pointerup'] as const;+ for (const type of types) document.addEventListener(type, notePointer, true);+ return () => {+ for (const type of types) document.removeEventListener(type, notePointer, true);+ };}, []);useEffect(() => {const held = keyboardFocusRef.current;@@ -393,8 +398,8 @@ function ChatThread({// is 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. A focus that is not :focus-visible- // is the pointer's only when a press came just before it or the element is a media control: one that- // a script or an assistive technology moves later is not, and its actions are shown.+ // is the pointer's only when the pointer went down or came up just before it or the element is a media+ // control: one that a script or an assistive technology moves later is not, and its actions are shown.onPointerDown={event => event.currentTarget.setAttribute('data-pointer', '')}onKeyDown={event => event.currentTarget.removeAttribute('data-pointer')}onFocus={event => {