Skip to content

Commit 0a410af

Browse files
fix(web): thread find stays open over Settings and Usage
The thread route's onLeave closed find whenever a modal page replaced it, though the thread stays drawn underneath. The _chat layout, which stays matched over Settings and Usage, now decides: a modal page keeps find, any other route keeps it only for the thread it shows. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent eff218f commit 0a410af

4 files changed

Lines changed: 41 additions & 19 deletions

File tree

‎apps/web/src/components/chat/threadFindStore.test.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
findForMessageMatch,
99
openThreadFind,
1010
openThreadFindOnQuery,
11-
retainThreadFindForThread,
11+
retainThreadFindForRoute,
1212
setThreadFindQuery,
1313
useThreadFindStore,
1414
} from "./threadFindStore";
@@ -59,13 +59,19 @@ describe("thread find store", () => {
5959
detach();
6060
});
6161

62-
it("closes find once the route leaves its thread", () => {
62+
it("keeps find over Settings and Usage, and closes it on any other route", () => {
6363
setup();
6464
openThreadFindOnQuery(threadKey, { query: "x" });
65-
retainThreadFindForThread(threadRef);
66-
expect(useThreadFindStore.getState().threadKey).toBe(threadKey);
67-
retainThreadFindForThread(otherRef);
65+
retainThreadFindForRoute({ modalPage: false, threadRef });
66+
retainThreadFindForRoute({ modalPage: true, threadRef: null });
67+
expect(useThreadFindStore.getState()).toMatchObject({ threadKey, query: "x" });
68+
69+
retainThreadFindForRoute({ modalPage: false, threadRef: otherRef });
6870
expect(useThreadFindStore.getState()).toMatchObject({ threadKey: null, query: "" });
71+
72+
openThreadFindOnQuery(threadKey, { query: "x" });
73+
retainThreadFindForRoute({ modalPage: false, threadRef: null });
74+
expect(useThreadFindStore.getState().threadKey).toBeNull();
6975
});
7076
});
7177

‎apps/web/src/components/chat/threadFindStore.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,18 @@ export function closeThreadFind(): void {
132132
useThreadFindStore.setState(CLOSED_THREAD_FIND);
133133
}
134134

135-
/** Find belongs to the thread it was opened in; any route away from it closes it. */
136-
export function retainThreadFindForThread(ref: ScopedThreadRef | null): void {
135+
/**
136+
* Find belongs to the thread it was opened in: a route showing anything else
137+
* closes it. Settings and Usage draw over the thread, so they keep it.
138+
*/
139+
export function retainThreadFindForRoute(route: {
140+
readonly modalPage: boolean;
141+
readonly threadRef: ScopedThreadRef | null;
142+
}): void {
143+
if (route.modalPage) return;
137144
const { threadKey } = useThreadFindStore.getState();
138-
if (threadKey !== null && threadKey !== (ref ? scopedThreadKey(ref) : null)) closeThreadFind();
145+
const shownKey = route.threadRef ? scopedThreadKey(route.threadRef) : null;
146+
if (threadKey !== null && threadKey !== shownKey) closeThreadFind();
139147
}
140148

141149
/** Whether find is open in `thread`, as long as find is available there. */
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { createFileRoute } from "@tanstack/react-router";
22

3-
import { retainThreadFindForThread } from "../components/chat/threadFindStore";
4-
import { resolveThreadRouteTarget } from "../threadRoutes";
5-
63
// The _chat layout owns ChatView so draft promotion keeps it mounted.
74
export const Route = createFileRoute("/_chat/$environmentId/$threadId")({
85
component: () => null,
9-
// Find belongs to the thread it was opened in: switching threads or leaving closes it.
10-
onStay: ({ params }) => {
11-
const target = resolveThreadRouteTarget(params);
12-
retainThreadFindForThread(target?.kind === "server" ? target.threadRef : null);
13-
},
14-
onLeave: () => retainThreadFindForThread(null),
156
});

‎apps/web/src/routes/_chat.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { isCommandPaletteOpen } from "../commandPaletteBus";
88
import { ThreadRouteView } from "../components/ThreadRouteView";
99
import { isAppModalPage } from "../components/sidebar/mainAppLocation";
1010
import { SidebarInset } from "../components/ui/sidebar";
11-
import type { ThreadRouteTarget } from "../threadRoutes";
11+
import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../threadRoutes";
12+
import { retainThreadFindForRoute } from "../components/chat/threadFindStore";
1213
import { ThreadLinkPrompt } from "../components/promptLinks/ThreadLinkPrompt";
1314
import { useClientSettings, useLegacySidebarEnabled } from "../hooks/useSettings";
1415
import { openCommandPalette } from "../commandPaletteBus";
@@ -234,13 +235,29 @@ function ChatRouteLayout() {
234235
}
235236

236237
export const Route = createFileRoute("/_chat")({
237-
beforeLoad: async ({ context }) => {
238+
beforeLoad: async ({ context, location }) => {
238239
if (
239240
context.authGateState.status !== "authenticated" &&
240241
context.authGateState.status !== "hosted-static"
241242
) {
242243
throw redirect({ to: "/pair", replace: true });
243244
}
245+
return { appModalPage: isAppModalPage(location.pathname) };
244246
},
245247
component: ChatRouteLayout,
248+
// Every match carries the whole location's params, so this layout sees the thread route's too.
249+
onEnter: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params),
250+
onStay: ({ context, params }) => retainThreadFindForRouteMatch(context.appModalPage, params),
251+
onLeave: () => retainThreadFindForRoute({ modalPage: false, threadRef: null }),
246252
});
253+
254+
function retainThreadFindForRouteMatch(
255+
modalPage: boolean,
256+
params: Partial<Record<"environmentId" | "threadId", string>>,
257+
) {
258+
const target = resolveThreadRouteTarget(params);
259+
retainThreadFindForRoute({
260+
modalPage,
261+
threadRef: target?.kind === "server" ? target.threadRef : null,
262+
});
263+
}

0 commit comments

Comments
 (0)