Skip to content

Commit 933ec4a

Browse files
committed
fix(desktop): dismiss stale channel suggestions before edit submission
Signed-off-by: Tee8z <[email protected]>
1 parent 63e4d74 commit 933ec4a

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import assert from "node:assert/strict";
2+
import { test } from "node:test";
3+
import React, { act } from "react";
4+
import { createRoot } from "react-dom/client";
5+
6+
import { ChannelNavigationProvider } from "@/shared/context/ChannelNavigationContext";
7+
import { useChannelLinks } from "./useChannelLinks.ts";
8+
9+
const channels = [
10+
{
11+
id: "general",
12+
name: "general",
13+
channelType: "stream",
14+
archivedAt: null,
15+
},
16+
];
17+
18+
function mountChannelLinks(t) {
19+
t.mock.timers.enable({ apis: ["setTimeout"] });
20+
const root = createRoot(document.createElement("div"));
21+
let current;
22+
function Harness() {
23+
current = useChannelLinks();
24+
return null;
25+
}
26+
act(() => {
27+
root.render(
28+
React.createElement(
29+
ChannelNavigationProvider,
30+
{ channels },
31+
React.createElement(Harness),
32+
),
33+
);
34+
});
35+
t.after(() => act(() => root.unmount()));
36+
return () => current;
37+
}
38+
39+
for (const replacement of ["", "Edited message", "Welcome to #general "]) {
40+
test(`channel suggestions close immediately after replacing the query with ${JSON.stringify(replacement)}`, (t) => {
41+
const links = mountChannelLinks(t);
42+
act(() => links().updateChannelQuery("Welcome to #general", 19));
43+
act(() => t.mock.timers.tick(120));
44+
assert.equal(links().isChannelOpen, true);
45+
46+
act(() => links().updateChannelQuery(replacement, replacement.length));
47+
48+
// Enter must submit the current edit before the debounce window ends.
49+
assert.equal(links().isChannelOpen, false);
50+
for (const key of ["Enter", "Tab"]) {
51+
assert.deepEqual(
52+
links().handleChannelKeyDown({
53+
key,
54+
preventDefault: () =>
55+
assert.fail("stale suggestion consumed the key"),
56+
}),
57+
{ handled: false },
58+
);
59+
}
60+
act(() => t.mock.timers.tick(120));
61+
assert.equal(links().isChannelOpen, false);
62+
});
63+
}
64+
65+
test("clearing a pending query cancels it; a later valid query still opens", (t) => {
66+
const links = mountChannelLinks(t);
67+
act(() => links().updateChannelQuery("#gen", 4));
68+
assert.equal(links().isChannelOpen, false);
69+
act(() => links().updateChannelQuery("", 0));
70+
act(() => t.mock.timers.tick(120));
71+
assert.equal(links().isChannelOpen, false);
72+
73+
act(() => links().updateChannelQuery("#gen", 4));
74+
act(() => t.mock.timers.tick(120));
75+
assert.equal(links().isChannelOpen, true);
76+
const event = { key: "Enter", preventDefault: t.mock.fn() };
77+
assert.deepEqual(links().handleChannelKeyDown(event), {
78+
handled: true,
79+
suggestion: { id: "general", name: "general", channelType: "stream" },
80+
});
81+
assert.equal(event.preventDefault.mock.callCount(), 1);
82+
});

‎desktop/src/features/messages/lib/useChannelLinks.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ export function useChannelLinks() {
122122

123123
if (debounceTimerRef.current !== null) {
124124
clearTimeout(debounceTimerRef.current);
125+
debounceTimerRef.current = null;
126+
}
127+
128+
// Closing must not wait for the debounce: Enter on a cleared or
129+
// replaced edit must submit it, not insert the previous suggestion.
130+
if (
131+
!detectPrefixQuery(
132+
"#",
133+
value,
134+
cursorPosition,
135+
knownNamesLowerRef.current,
136+
)
137+
) {
138+
setChannelQuery(null);
139+
setChannelSelectedIndex(0);
140+
return;
125141
}
126142

127143
debounceTimerRef.current = setTimeout(() => {

‎desktop/src/features/messages/lib/useRichTextEditor.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ export function useRichTextEditor({
315315
addKeyboardShortcuts() {
316316
return {
317317
Enter: ({ editor: ed }) => {
318-
if (isAutocompleteOpen?.current) return false;
318+
// The React wrapper selects the suggestion after this event
319+
// bubbles. Prevent splitBlock from changing the query first.
320+
if (isAutocompleteOpen?.current) return true;
319321
if (!onSubmitRef.current) return false;
320322

321323
const fenceResult = handleCodeFenceEnter(ed);

‎desktop/tests/e2e/empty-edit-delete.spec.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ test.beforeEach(async ({ page }) => {
4848
await expect(page.getByTestId("chat-title")).toHaveText("general");
4949
});
5050

51+
for (const selection of ["Enter", "Tab", "click"]) {
52+
test(`channel selection by ${selection} preserves subsequent typing`, async ({
53+
page,
54+
}) => {
55+
const input = page.getByTestId("message-input");
56+
// This prefix matches only general; "#gen" also matches agents.
57+
await input.fill("#gener");
58+
const option = page.getByRole("button", {
59+
name: "#general stream",
60+
exact: true,
61+
});
62+
await expect(option).toBeVisible();
63+
if (selection === "click") {
64+
await option.click();
65+
} else {
66+
await input.press(selection);
67+
}
68+
await expect(input).toHaveText("#general ");
69+
await expect(input.locator("p")).toHaveCount(1);
70+
await page.keyboard.type("hello");
71+
await expect(input).toHaveText("#general hello");
72+
});
73+
}
74+
5175
test("clearing an edit to empty prompts to delete, then deletes on confirm", async ({
5276
page,
5377
}) => {

0 commit comments

Comments
 (0)