Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): leave Home and End to the browser at the ends

Yudhi Armyndharisc3c9a3703455
diff --git a/dashboard/src/components/chats/ChatThread.test.ts b/dashboard/src/components/chats/ChatThread.test.ts
index 4a21f30fc..a79fb3520 100644
--- a/dashboard/src/components/chats/ChatThread.test.ts
+++ b/dashboard/src/components/chats/ChatThread.test.ts
@@ -552,6 +552,34 @@ test('arrow keys, Home and End are consumed and reach the stops from the middle
assert.equal(rtl.fireEvent.keyDown(bubbles[1], { key: 'a' }), true);
});
+// On the first message there is nowhere further for Home to go, and on the last none for End: the browser
+// scrolls the thread then, as it does for ArrowUp and ArrowDown past the ends, so Home asks for an older page
+// as the other keys do. Focus stays where it is.
+test('Home on the first message and End on the last are left to the browser, and keep focus', () => {
+ const messages = ['a', 'b', 'c'].map((id): ChatMessageView => ({
+ ...PROMPT,
+ id: `db-${id}`,
+ waMessageId: `wamid.${id}`,
+ metadata: undefined,
+ }));
+ const { container } = renderThread('operator', messages);
+ const bubbles = bubblesOf(container);
+
+ bubbles[0].focus();
+ assert.equal(rtl.fireEvent.keyDown(bubbles[0], { key: 'Home' }), true);
+ assert.equal(focusedMessage(), 'wamid.a');
+ assert.equal(rtl.fireEvent.keyDown(bubbles[0], { key: 'ArrowUp' }), true);
+
+ bubbles[2].focus();
+ assert.equal(rtl.fireEvent.keyDown(bubbles[2], { key: 'End' }), true);
+ assert.equal(focusedMessage(), 'wamid.c');
+ assert.equal(rtl.fireEvent.keyDown(bubbles[2], { key: 'ArrowDown' }), true);
+
+ // From any other message the key still moves to the end it names.
+ assert.equal(rtl.fireEvent.keyDown(bubbles[2], { key: 'Home' }), false);
+ assert.equal(focusedMessage(), 'wamid.a');
+});
+
// Deleting removes the buttons that hold focus, so focus would fall to the page and the next Tab would
// land in the composer.
test('keyboard focus stays in the thread when the focused message is deleted', () => {
diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsx
index 2c45ad98c..f7171acb1 100644
--- a/dashboard/src/components/chats/ChatThread.tsx
+++ b/dashboard/src/components/chats/ChatThread.tsx
@@ -129,7 +129,10 @@ function ChatThread({
: event.key === 'End'
? stops[stops.length - 1]
: undefined;
- if (!target) return;
+ // No stop to move to, or the bubble that already holds focus (Home on the first message, End on the last):
+ // the key is the browser's, which scrolls the thread, as ArrowUp and ArrowDown past the ends do. Taking it
+ // would leave a reader on the first message no way to ask for an older page with Home.
+ if (!target || target === event.currentTarget) return;
event.preventDefault();
target.focus();
};