Skip to content

Commit 4bf8097

Browse files
tellahoRizzCarl
authored
fix(messages): route edits to the owning composer (#6575)
**Category:** fix **User Impact:** Editing a channel message now opens in the main composer, while editing a thread reply stays in the thread composer, with focus ready for typing. **Problem:** When a thread was open, Buzz treated its root message as thread-owned and opened edits in the thread composer. Menu-driven edits also lacked regression coverage for immediate focus. **Solution:** Carry the message's semantic root/reply classification into the edit target, route only actual replies to the thread composer, and use the menu primitive's selection event for a reliable handoff. End-to-end tests cover placement and focus for both paths. <details> <summary>File changes</summary> **desktop/src/features/channels/ui/ChannelPane.tsx** Routes edit targets by semantic thread ownership rather than membership in the open thread panel. **desktop/src/features/channels/ui/ChannelPane.types.ts** Uses the shared composer edit-target type so routing metadata stays attached to the target. **desktop/src/features/messages/lib/draftMentionRefs.ts** Classifies each edit target as a root or true thread reply from its event tags. **desktop/src/features/messages/lib/draftMentionRefs.test.mjs** Covers semantic ownership for root and reply edit targets. **desktop/src/features/messages/ui/MessageActionBar.tsx** Handles Edit through the dropdown menu's selection event so focus restoration and edit startup share the intended lifecycle. **desktop/src/features/messages/ui/MessageComposer.types.ts** Adds semantic thread ownership to the edit-target contract. **desktop/tests/e2e/messaging.spec.ts** Verifies root edits use and focus the main composer, while reply edits use and focus the thread composer. </details> ### Reproduction Steps 1. Send a channel message and open its thread. 2. From the thread panel, edit the root message; confirm its content loads in the main composer and the editor is focused. 3. Send a reply in that thread. 4. Edit the reply; confirm its content loads in the thread composer and the editor is focused. ### Screenshots **Editing a channel-root message uses the main composer** ![Channel-root message editing in the main composer, dark theme with purple accent](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6575/message-edit-root-main-dark.png) **Editing an actual thread reply uses the thread composer** ![Thread reply editing in the thread composer, dark theme with purple accent](https://d24qwcpro867f5.cloudfront.net/repos/buzz/prs/6575/message-edit-reply-thread-dark.png) --------- Signed-off-by: Taylor Ho <[email protected]> Co-authored-by: Rizz <302abe414ca6e3134763d2539bfcf145aea2a63fe5f8455204ed602fd40cf381@buzz.block.builderlab.xyz> Co-authored-by: Carl <acda9e433d19dcd0e6b6840f7f4b98f3a56f1fab98049d444c087019e6d36560@buzz.block.builderlab.xyz>
1 parent 6eff84d commit 4bf8097

28 files changed

Lines changed: 1799 additions & 94 deletions
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
const target = {
5+
kind: "channel-message",
6+
channelId: "general",
7+
messageId: "message-a",
8+
threadRootId: "thread-a",
9+
};
10+
11+
const { allowNavigation, registerNavigationGuard, traverseHistory } =
12+
await import("./navigationGuard.ts");
13+
14+
test("all navigation consults the registered boundary guard", () => {
15+
let received;
16+
const unregister = registerNavigationGuard((nextTarget) => {
17+
received = nextTarget;
18+
return false;
19+
});
20+
21+
assert.equal(allowNavigation(target), false);
22+
assert.deepEqual(received, target);
23+
unregister();
24+
assert.equal(allowNavigation(target), true);
25+
});
26+
27+
test("guarded history traversal blocks before mutating history", () => {
28+
let received;
29+
let backCalls = 0;
30+
const unregister = registerNavigationGuard((nextTarget) => {
31+
received = nextTarget;
32+
return false;
33+
});
34+
35+
assert.equal(
36+
traverseHistory(
37+
{
38+
back: () => {
39+
backCalls += 1;
40+
},
41+
forward: () => {},
42+
},
43+
"back",
44+
),
45+
false,
46+
);
47+
assert.deepEqual(received, { kind: "history", direction: "back" });
48+
assert.equal(backCalls, 0);
49+
unregister();
50+
});
51+
52+
test("guarded history traversal invokes the selected direction when allowed", () => {
53+
let forwardCalls = 0;
54+
55+
assert.equal(
56+
traverseHistory(
57+
{
58+
back: () => {},
59+
forward: () => {
60+
forwardCalls += 1;
61+
},
62+
},
63+
"forward",
64+
),
65+
true,
66+
);
67+
assert.equal(forwardCalls, 1);
68+
});
69+
70+
test("unregistering the newer guard restores the prior live guard", () => {
71+
const unregisterFirst = registerNavigationGuard(() => false);
72+
const unregisterSecond = registerNavigationGuard(() => true);
73+
74+
assert.equal(allowNavigation(target), true);
75+
unregisterSecond();
76+
assert.equal(allowNavigation(target), false);
77+
unregisterFirst();
78+
assert.equal(allowNavigation(target), true);
79+
});
80+
81+
test("stale cleanup cannot unregister a newer guard", () => {
82+
const unregisterFirst = registerNavigationGuard(() => false);
83+
const unregisterSecond = registerNavigationGuard(() => true);
84+
85+
unregisterFirst();
86+
assert.equal(allowNavigation(target), true);
87+
unregisterSecond();
88+
assert.equal(allowNavigation(target), true);
89+
});
90+
91+
test("duplicate callback registrations clean up by registration identity", () => {
92+
const sharedGuard = () => false;
93+
const unregisterFirst = registerNavigationGuard(sharedGuard);
94+
const unregisterSecond = registerNavigationGuard(sharedGuard);
95+
96+
unregisterFirst();
97+
assert.equal(allowNavigation(target), false);
98+
unregisterSecond();
99+
assert.equal(allowNavigation(target), true);
100+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export type GuardedNavigation =
2+
| {
3+
kind: "history";
4+
direction: "back" | "forward";
5+
}
6+
| {
7+
kind: "route";
8+
href: string;
9+
}
10+
| {
11+
kind: "channel-message";
12+
channelId: string;
13+
messageId: string;
14+
threadRootId: string | null;
15+
}
16+
| {
17+
kind: "forum-post";
18+
channelId: string;
19+
postId: string;
20+
replyId: string | null;
21+
};
22+
23+
type NavigationGuard = (target: GuardedNavigation) => boolean;
24+
25+
type GuardRegistration = {
26+
guard: NavigationGuard;
27+
};
28+
29+
const activeGuards: GuardRegistration[] = [];
30+
31+
export function allowNavigation(target: GuardedNavigation): boolean {
32+
return activeGuards.at(-1)?.guard(target) ?? true;
33+
}
34+
35+
export function traverseHistory(
36+
history: Pick<History, "back" | "forward">,
37+
direction: "back" | "forward",
38+
): boolean {
39+
if (!allowNavigation({ kind: "history", direction })) {
40+
return false;
41+
}
42+
43+
history[direction]();
44+
return true;
45+
}
46+
47+
export function registerNavigationGuard(guard: NavigationGuard): () => void {
48+
const registration = { guard };
49+
activeGuards.push(registration);
50+
return () => {
51+
const index = activeGuards.lastIndexOf(registration);
52+
if (index >= 0) activeGuards.splice(index, 1);
53+
};
54+
}

‎desktop/src/app/navigation/useAppNavigation.ts‎

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import {
77
} from "@tanstack/react-router";
88

99
import { openSearchHitWithNavigation } from "@/app/navigation/searchHitNavigation";
10+
import {
11+
allowNavigation,
12+
type GuardedNavigation,
13+
traverseHistory,
14+
} from "@/app/navigation/navigationGuard";
1015
import type { SearchHit } from "@/shared/api/types";
1116

1217
type NavigationBehavior = {
@@ -30,13 +35,22 @@ export function useAppNavigation() {
3035
state?: Record<string, unknown>;
3136
},
3237
behavior: NavigationBehavior = {},
38+
guardedTarget?: GuardedNavigation,
3339
) => {
3440
const nextLocation = router.buildLocation(next as never);
3541

3642
if (location.href === nextLocation.href && !behavior.force) {
3743
return false;
3844
}
3945

46+
if (
47+
!allowNavigation(
48+
guardedTarget ?? { kind: "route", href: nextLocation.href },
49+
)
50+
) {
51+
return false;
52+
}
53+
4054
await navigate({
4155
...next,
4256
replace: behavior.replace,
@@ -256,8 +270,8 @@ export function useAppNavigation() {
256270
thread?: string;
257271
threadRootId?: string | null;
258272
},
259-
) =>
260-
commitNavigation(
273+
) => {
274+
return commitNavigation(
261275
{
262276
to: "/channels/$channelId",
263277
params: {
@@ -282,7 +296,16 @@ export function useAppNavigation() {
282296
replace: options?.replace,
283297
resetScroll: options?.messageId ? true : undefined,
284298
},
285-
),
299+
options?.messageId
300+
? {
301+
kind: "channel-message",
302+
channelId,
303+
messageId: options.messageId,
304+
threadRootId: options.threadRootId ?? null,
305+
}
306+
: undefined,
307+
);
308+
},
286309
[commitNavigation],
287310
);
288311

@@ -307,8 +330,8 @@ export function useAppNavigation() {
307330
replace?: boolean;
308331
replyId?: string;
309332
},
310-
) =>
311-
commitNavigation(
333+
) => {
334+
return commitNavigation(
312335
{
313336
to: "/channels/$channelId/posts/$postId",
314337
params: {
@@ -322,7 +345,14 @@ export function useAppNavigation() {
322345
replace: options?.replace,
323346
resetScroll: false,
324347
},
325-
),
348+
{
349+
kind: "forum-post",
350+
channelId,
351+
postId,
352+
replyId: options?.replyId ?? null,
353+
},
354+
);
355+
},
326356
[commitNavigation],
327357
);
328358

@@ -340,7 +370,7 @@ export function useAppNavigation() {
340370

341371
const closeSettings = React.useCallback(() => {
342372
if (canGoBack) {
343-
router.history.back();
373+
traverseHistory(router.history, "back");
344374
return;
345375
}
346376

@@ -349,7 +379,7 @@ export function useAppNavigation() {
349379

350380
const closeWorkflowDetail = React.useCallback(() => {
351381
if (canGoBack) {
352-
router.history.back();
382+
traverseHistory(router.history, "back");
353383
return;
354384
}
355385

@@ -359,7 +389,7 @@ export function useAppNavigation() {
359389
const closeForumPost = React.useCallback(
360390
(channelId: string) => {
361391
if (canGoBack) {
362-
router.history.back();
392+
traverseHistory(router.history, "back");
363393
return;
364394
}
365395

‎desktop/src/app/navigation/useBackForwardControls.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isTauri } from "@tauri-apps/api/core";
88
import { listen } from "@tauri-apps/api/event";
99

1010
import { matchBackForwardChord } from "@/app/navigation/backForwardChords";
11+
import { traverseHistory } from "@/app/navigation/navigationGuard";
1112
import { isMacPlatform } from "@/shared/lib/platform";
1213
import { trimMapToSize } from "@/shared/lib/trimMapToSize";
1314

@@ -59,15 +60,15 @@ export function useBackForwardControls() {
5960
return;
6061
}
6162

62-
router.history.back();
63+
traverseHistory(router.history, "back");
6364
}, [canGoBack, router.history]);
6465

6566
const goForward = React.useCallback(() => {
6667
if (!canGoForward) {
6768
return;
6869
}
6970

70-
router.history.forward();
71+
traverseHistory(router.history, "forward");
7172
}, [canGoForward, router.history]);
7273

7374
const handleKeyDown = React.useEffectEvent((event: KeyboardEvent) => {

‎desktop/src/app/routes/WorkflowsRouteScreen.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export function WorkflowsRouteScreen({
1818
onEditorPaneChange,
1919
}: WorkflowsRouteScreenProps) {
2020
const {
21+
closeWorkflowDetail,
2122
goDuplicateWorkflow,
2223
goEditWorkflow,
2324
goNewWorkflow,
@@ -26,11 +27,11 @@ export function WorkflowsRouteScreen({
2627
} = useAppNavigation();
2728
const closeEditor = React.useCallback(() => {
2829
if (editor?.hasOrigin) {
29-
window.history.back();
30+
closeWorkflowDetail();
3031
return;
3132
}
3233
void goWorkflows({ replace: true });
33-
}, [editor?.hasOrigin, goWorkflows]);
34+
}, [closeWorkflowDetail, editor?.hasOrigin, goWorkflows]);
3435
const channelsQuery = useChannelsQuery();
3536
const channels = channelsQuery.data ?? [];
3637
const memberChannels = channels.filter((channel) => channel.isMember);

0 commit comments

Comments
 (0)