Skip to content

Commit 90fb9dd

Browse files
authored
fix(desktop): dismiss stale channel suggestions before edit submission (#27)
Signed-off-by: Tee8z <[email protected]>
1 parent 63e4d74 commit 90fb9dd

6 files changed

Lines changed: 138 additions & 0 deletions

File tree

‎desktop/src/features/forum/ui/ForumComposer.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function ForumComposerVisit({
132132
submitModeRef.current = onSecondarySubmit ? submitMode : "primary";
133133

134134
const isAutocompleteOpenRef = React.useRef(false);
135+
const isChannelAutocompleteOpenRef = React.useRef(false);
136+
React.useLayoutEffect(() => {
137+
isChannelAutocompleteOpenRef.current = channelLinks.isChannelOpen;
138+
}, [channelLinks.isChannelOpen]);
135139
isAutocompleteOpenRef.current =
136140
mentions.isMentionOpen || channelLinks.isChannelOpen;
137141

@@ -158,6 +162,7 @@ function ForumComposerVisit({
158162
getMentionIdentities: mentions.getMentionIdentities,
159163
onSubmit: () => submitMessageRef.current(),
160164
isAutocompleteOpen: isAutocompleteOpenRef,
165+
isChannelAutocompleteOpen: isChannelAutocompleteOpenRef,
161166
onEditLink: (info) => onEditLinkRef.current?.(info),
162167
onLinkSelectionChange: (info) => onLinkSelectionChangeRef.current?.(info),
163168
onLinkShortcut: () => onLinkShortcutRef.current?.() ?? false,
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ export type RichTextEditorOptions = {
109109
onEditLastOwnMessage?: () => boolean;
110110
/** When true, plain Enter is passed through (e.g. to select an autocomplete item). */
111111
isAutocompleteOpen?: React.RefObject<boolean>;
112+
/** Prevent paragraph splitting from dismissing a pending channel selection. */
113+
isChannelAutocompleteOpen?: React.RefObject<boolean>;
112114
/**
113115
* Called when the user clicks an existing link in the editor. The link
114116
* extension runs with `openOnClick: false` (a chat composer must not
@@ -155,6 +157,7 @@ export function useRichTextEditor({
155157
onSubmit,
156158
onEditLastOwnMessage,
157159
isAutocompleteOpen,
160+
isChannelAutocompleteOpen,
158161
onEditLink,
159162
onLinkSelectionChange,
160163
onLinkShortcut,
@@ -315,6 +318,9 @@ export function useRichTextEditor({
315318
addKeyboardShortcuts() {
316319
return {
317320
Enter: ({ editor: ed }) => {
321+
// Channel queries close immediately when invalidated. Let the
322+
// React wrapper select before splitBlock changes that query.
323+
if (isChannelAutocompleteOpen?.current) return true;
318324
if (isAutocompleteOpen?.current) return false;
319325
if (!onSubmitRef.current) return false;
320326

‎desktop/src/features/messages/ui/MessageComposer.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ function MessageComposerImpl({
250250
extractMentionPubkeysRef.current = mentions.extractMentionPubkeys;
251251
ownerPubkeyRef.current = ownerPubkey;
252252
const isAutocompleteOpenRef = React.useRef(false);
253+
const isChannelAutocompleteOpenRef = React.useRef(false);
254+
React.useLayoutEffect(() => {
255+
isChannelAutocompleteOpenRef.current = channelLinks.isChannelOpen;
256+
}, [channelLinks.isChannelOpen]);
253257
isAutocompleteOpenRef.current =
254258
mentions.isMentionOpen ||
255259
channelLinks.isChannelOpen ||
@@ -294,6 +298,7 @@ function MessageComposerImpl({
294298
return handler ? handler() : false;
295299
},
296300
isAutocompleteOpen: isAutocompleteOpenRef,
301+
isChannelAutocompleteOpen: isChannelAutocompleteOpenRef,
297302
onEditLink: (info) => onEditLinkRef.current?.(info),
298303
onLinkSelectionChange: (info) => onLinkSelectionChangeRef.current?.(info),
299304
onLinkShortcut: () => onLinkShortcutRef.current?.() ?? false,

‎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)