Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep focus on a message whose control alone went away
Yudhi Armyndharisf9f856580410
diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.tsindex 1e763ac34..d9d2a6089 100644--- a/dashboard/src/components/chats/ChatThread.test.ts+++ b/dashboard/src/components/chats/ChatThread.test.ts@@ -596,6 +596,47 @@ test('the stop that takes the place is found after an older page landed since thassert.equal(focusedMessage(), 'wamid.c');});+// A message that leaves the list takes its bubble out of the document with its controls: the stop after it+// takes the focus, and the one before it when it was the last.+test('focus goes to the next stop, then the previous one, when the focused message leaves the list', () => {+ 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 reply = bubblesOf(container)[1].querySelector<HTMLElement>('.action-btn');+ assert.ok(reply);+ reply.focus();++ rerenderWith([a, c]);+ assert.equal(focusedMessage(), 'wamid.3');++ rerenderWith([a]);+ assert.equal(focusedMessage(), 'wamid.1');+});++// Only the control went away: its message is still a stop, and it keeps the focus.+test('focus stays on the bubble when a link inside it is the control that went away', () => {+ const linked: ChatMessageView = {+ ...PROMPT,+ id: 'db-2',+ waMessageId: 'wamid.2',+ body: 'see https://example.org/a',+ metadata: undefined,+ };+ const next: ChatMessageView = { ...PROMPT, id: 'db-3', waMessageId: 'wamid.3', body: 'Next', metadata: undefined };+ const { container, rerenderWith } = renderThread('operator', [linked, next]);+ const link = container.querySelector<HTMLElement>('[data-wa-message-id="wamid.2"] a[href]');+ assert.ok(link, 'the body links its URL');+ link.focus();++ rerenderWith([{ ...linked, body: 'see no link now' }, next]);++ assert.equal(focusedMessage(), 'wamid.2');+});+// A refetch after a reconnect replaces the live copy of a message with its stored row, whose id differs,// so React remounts the bubble: the message is the same, and focus belongs on it.test('focus goes to the same message when its bubble is remounted under another key', () => {diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsxindex d3380cba2..4544b828b 100644--- a/dashboard/src/components/chats/ChatThread.tsx+++ b/dashboard/src/components/chats/ChatThread.tsx@@ -157,15 +157,24 @@ function ChatThread({// A bubble that left the document may be the same message under a new key (a refetch replaces the// live copy of a message with its stored row): the WhatsApp id on its wrapper says so.const byWa = (wa: string | null) => (wa ? stops.find(stop => waIdOf(stop) === wa) : undefined);- 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))- : (byWa(held.wa) ??- held.neighbors- .map(near =>- near.element.isConnected && near.element.hasAttribute('data-roving-message') ? near.element : byWa(near.wa),- )- .find(Boolean));+ // The control alone may have gone (a link an edit removed, a media control swapped for its marker): the+ // message it belonged to is still a stop, and keeps the focus.+ const target =+ bubble.isConnected && bubble.hasAttribute('data-roving-message')+ ? bubble+ : bubble.isConnected+ ? (stops.find(stop => bubble.compareDocumentPosition(stop) & bubble.DOCUMENT_POSITION_FOLLOWING) ??+ [...stops]+ .reverse()+ .find(stop => bubble.compareDocumentPosition(stop) & bubble.DOCUMENT_POSITION_PRECEDING))+ : (byWa(held.wa) ??+ held.neighbors+ .map(near =>+ near.element.isConnected && near.element.hasAttribute('data-roving-message')+ ? near.element+ : byWa(near.wa),+ )+ .find(Boolean));target?.focus({ preventScroll: true });}, [messages, hasActions, messagesContainerRef]);