@devries-cameron20 / t3code-1 / commits / d9a97e7c49
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): sidebar keeps the open thread active while Settings or Usage is open (#28703)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Igor Makowski committed Nov 12, 2026 at 22:51 UTC d9a97e7c493932c03cbef3b0785c0cbfc7c05d73
7 files changed +157 -26
apps/web/src/components/Sidebar.tsx
+11 -3
@@ -257,7 +257,7 @@ import {
257 useComposerDraftStore,
258 usePromotingDraftThreadKeys,
259 } from "../composerDraftStore";
260 -import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
260 +import { useShownThreadRouteTarget } from "../hooks/useShownThreadRouteTarget";
261 import {
262 SidebarSearchResultRow,
263 SortableThreadRow,
@@ -1006,10 +1006,14 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1006 const toggleThreadSelection = useThreadSelectionStore((s) => s.toggleThread);
1007 const rangeSelectTo = useThreadSelectionStore((s) => s.rangeSelectTo);
1008 const acknowledgeWoke = useAcknowledgeThreadWoke();
1009 - const routeTarget = useThreadRouteTarget();
1009 + // A Settings or Usage modal keeps the thread behind it current.
1010 + const routeTarget = useShownThreadRouteTarget().shownTarget;
1011 const routeDraftThread = useComposerDraftStore((store) =>
1012 routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
1013 );
1014 + const routeDraftRedirect = useComposerDraftStore((store) =>
1015 + routeTarget?.kind === "draft" ? store.getPromotedDraftRedirect(routeTarget.draftId) : null,
1016 + );
1017 const environmentMachineById = useEnvironmentMachines();
1018 const projectGroupsRef = useRef(projectGroups);
1019 const persistedProjectScopeKeys = useUiStateStore((store) => store.sidebarProjectScopeKeys);
@@ -1070,7 +1074,11 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
1074 const snoozingThreadKeysRef = useRef(new Set<string>());
1075 const removeFromSelection = useThreadSelectionStore((s) => s.removeFromSelection);
1076 const openAddProjectCommandPalette = () => openCommandPalette({ open: "add-project" });
1073 - const routeThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread);
1077 + const routeThreadRef = resolveActiveThreadRouteRef(
1078 + routeTarget,
1079 + routeDraftThread,
1080 + routeDraftRedirect,
1081 + );
1082 const routeThreadKey = routeThreadRef ? scopedThreadKey(routeThreadRef) : null;
1083
1084 // A selection made under one scope can hold threads the next scope hides.
apps/web/src/components/sidebar/mainAppLocation.ts
+10
@@ -2,6 +2,7 @@ import { useNavigate, type AnyRouter } from "@tanstack/react-router";
2
3 import { GUEST_PAGE_PATH } from "../../guestPage";
4 import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
5 +import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../../threadRoutes";
6
7 /** Settings and Usage open as modals over the last main-app view. */
8 export function isAppModalPage(pathname: string) {
@@ -19,6 +20,7 @@ const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_P
20
21 let mainAppHref: string | null = null;
22 let appModalReturnHref: string | null = null;
23 +let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
24
25 /** Remembers where Settings, Usage, and the utility pages lead back to. */
26 export function startMainAppLocationTracking(router: AnyRouter): () => void {
@@ -27,12 +29,20 @@ export function startMainAppLocationTracking(router: AnyRouter): () => void {
29 if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
30 const href = withoutAssistantCitationHash(router.state.location.href);
31 appModalReturnHref = href;
32 + appModalReturnThreadTarget = resolveThreadRouteTarget(
33 + router.state.matches.at(-1)?.params ?? {},
34 + );
35 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
36 };
37 record();
38 return router.subscribe("onResolved", record);
39 }
40
41 +/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
42 +export function getAppModalReturnThreadTarget() {
43 + return appModalReturnThreadTarget;
44 +}
45 +
46 export function useNavigateToMainApp() {
47 const navigate = useNavigate();
48 return () => navigate({ href: mainAppHref ?? "/" });
apps/web/src/hooks/useShownThreadRouteTarget.browser.test.tsx new
+91
@@ -0,0 +1,91 @@
1 +import {
2 + createMemoryHistory,
3 + createRootRoute,
4 + createRoute,
5 + createRouter,
6 + Outlet,
7 + RouterProvider,
8 +} from "@tanstack/react-router";
9 +import { describe, expect, it } from "vite-plus/test";
10 +
11 +import { act, render } from "../../test/render";
12 +import { startMainAppLocationTracking } from "../components/sidebar/mainAppLocation";
13 +import { useShownThreadRouteTarget } from "./useShownThreadRouteTarget";
14 +
15 +function ShownThread() {
16 + const { shownTarget } = useShownThreadRouteTarget();
17 + const label =
18 + shownTarget === null
19 + ? "none"
20 + : shownTarget.kind === "server"
21 + ? shownTarget.threadRef.threadId
22 + : shownTarget.draftId;
23 + return (
24 + <>
25 + <output>{label}</output>
26 + <Outlet />
27 + </>
28 + );
29 +}
30 +
31 +function createAppRouter(initialPath: string) {
32 + const rootRoute = createRootRoute({ component: ShownThread });
33 + const threadRoute = createRoute({
34 + getParentRoute: () => rootRoute,
35 + path: "$environmentId/$threadId",
36 + });
37 + const settingsRoute = createRoute({ getParentRoute: () => rootRoute, path: "settings" });
38 + const settingsGeneralRoute = createRoute({
39 + getParentRoute: () => settingsRoute,
40 + path: "general",
41 + });
42 + const usageRoute = createRoute({ getParentRoute: () => rootRoute, path: "usage" });
43 + const newRoute = createRoute({ getParentRoute: () => rootRoute, path: "new" });
44 + return createRouter({
45 + routeTree: rootRoute.addChildren([
46 + threadRoute,
47 + settingsRoute.addChildren([settingsGeneralRoute]),
48 + usageRoute,
49 + newRoute,
50 + ]),
51 + history: createMemoryHistory({ initialEntries: [initialPath] }),
52 + });
53 +}
54 +
55 +describe("useShownThreadRouteTarget", () => {
56 + it("keeps the thread that opened Settings or Usage shown until the user leaves for another page", async () => {
57 + const router = createAppRouter("/env-1/thread-a");
58 + await router.load();
59 + // The hook reads thread params and modal paths from real routes, which the default router lacks.
60 + await using screen = await render(<RouterProvider router={router} />, { router: false });
61 + const shown = screen.getByRole("status");
62 + await expect.element(shown).toHaveTextContent("thread-a");
63 +
64 + await act(() => router.navigate({ to: "/settings/general" }));
65 + await expect.element(shown).toHaveTextContent("thread-a");
66 +
67 + await act(() => router.navigate({ to: "/usage" }));
68 + await expect.element(shown).toHaveTextContent("thread-a");
69 +
70 + await act(() => router.navigate({ href: "/env-1/thread-b" }));
71 + await expect.element(shown).toHaveTextContent("thread-b");
72 +
73 + await act(() => router.navigate({ to: "/new" }));
74 + await expect.element(shown).toHaveTextContent("none");
75 +
76 + await act(() => router.navigate({ to: "/settings/general" }));
77 + await expect.element(shown).toHaveTextContent("none");
78 + });
79 +
80 + it("shows the thread behind Settings when it mounts while Settings is already open", async () => {
81 + const router = createAppRouter("/env-1/thread-a");
82 + await router.load();
83 + using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
84 + await router.navigate({ to: "/settings/general" });
85 +
86 + // The hook reads thread params and modal paths from real routes, which the default router lacks.
87 + await using screen = await render(<RouterProvider router={router} />, { router: false });
88 +
89 + await expect.element(screen.getByRole("status")).toHaveTextContent("thread-a");
90 + });
91 +});
apps/web/src/hooks/useShownThreadRouteTarget.ts new
+31
@@ -0,0 +1,31 @@
1 +import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2 +import { useMatches } from "@tanstack/react-router";
3 +import { useState } from "react";
4 +
5 +import {
6 + getAppModalReturnThreadTarget,
7 + isAppModalPage,
8 +} from "../components/sidebar/mainAppLocation";
9 +import type { ThreadRouteTarget } from "../threadRoutes";
10 +import { useThreadRouteTarget } from "./useThreadRouteTarget";
11 +
12 +function threadRouteTargetKey(target: ThreadRouteTarget | null) {
13 + if (!target) return null;
14 + return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId;
15 +}
16 +
17 +/** Settings and Usage render over the thread that opened them; that thread stays the shown one. */
18 +export function useShownThreadRouteTarget() {
19 + const routeTarget = useThreadRouteTarget();
20 + // Read matches, not location, so this agrees with the params useThreadRouteTarget reads.
21 + const modalOpen = useMatches({
22 + select: (matches) => matches.some((match) => isAppModalPage(match.pathname)),
23 + });
24 + const [backgroundTarget, setBackgroundTarget] = useState(() =>
25 + modalOpen ? getAppModalReturnThreadTarget() : routeTarget,
26 + );
27 + if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(routeTarget)) {
28 + setBackgroundTarget(routeTarget);
29 + }
30 + return { shownTarget: modalOpen ? backgroundTarget : routeTarget, modalOpen };
31 +}
apps/web/src/routes/_chat.tsx
+4 -21
@@ -1,33 +1,16 @@
1 -import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2 -import { Outlet, createFileRoute, redirect, useMatches } from "@tanstack/react-router";
3 -import { useState } from "react";
1 +import { Outlet, createFileRoute, redirect } from "@tanstack/react-router";
2
3 import { ThreadRouteView } from "../components/ThreadRouteView";
4 import { isAppModalPage } from "../components/sidebar/mainAppLocation";
5 import { SidebarInset } from "../components/ui/sidebar";
8 -import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../threadRoutes";
6 +import { resolveThreadRouteTarget } from "../threadRoutes";
7 import { retainThreadFindForRoute } from "../components/chat/threadFindStore";
8 import { ThreadLinkPrompt } from "../components/promptLinks/ThreadLinkPrompt";
11 -import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
12 -
13 -function threadRouteTargetKey(target: ThreadRouteTarget | null) {
14 - if (!target) return null;
15 - return target.kind === "server" ? scopedThreadKey(target.threadRef) : target.draftId;
16 -}
9 +import { useShownThreadRouteTarget } from "../hooks/useShownThreadRouteTarget";
10
11 function ChatRouteLayout() {
12 // Render both routes here so draft promotion keeps the same ChatView mounted.
20 - const threadTarget = useThreadRouteTarget();
21 - // Read matches, not location, so this agrees with the params useThreadRouteTarget reads.
22 - const modalOpen = useMatches({
23 - select: (matches) => matches.some((match) => isAppModalPage(match.pathname)),
24 - });
25 - // Settings and Usage render over the thread that opened them, so ChatView never remounts.
26 - const [backgroundTarget, setBackgroundTarget] = useState(threadTarget);
27 - if (!modalOpen && threadRouteTargetKey(backgroundTarget) !== threadRouteTargetKey(threadTarget)) {
28 - setBackgroundTarget(threadTarget);
29 - }
30 - const shownTarget = modalOpen ? backgroundTarget : threadTarget;
13 + const { shownTarget, modalOpen } = useShownThreadRouteTarget();
14 return (
15 <>
16 {shownTarget?.kind === "server" && !modalOpen ? (
apps/web/src/threadRoutes.test.ts
+7
@@ -83,6 +83,13 @@ describe("threadRoutes", () => {
83 environmentId: "env-2",
84 threadId: "server-thread",
85 });
86 + expect(
87 + resolveActiveThreadRouteRef(
88 + target,
89 + null,
90 + scopeThreadRef("env-2" as never, ThreadId.make("server-thread")),
91 + ),
92 + ).toEqual({ environmentId: "env-2", threadId: "server-thread" });
93 });
94
95 it("does not treat a draft's reserved thread ref as an active sidebar thread", () => {
apps/web/src/threadRoutes.ts
+3 -2
@@ -118,12 +118,13 @@ export function resolveThreadRouteTarget(
118 export function resolveActiveThreadRouteRef(
119 target: ThreadRouteTarget | null,
120 draftThread: DraftThreadRouteState | null,
121 + promotedDraftRedirect: ScopedThreadRef | null = null,
122 ): ScopedThreadRef | null {
123 if (target?.kind === "server") {
124 return target.threadRef;
125 }
125 - if (target?.kind !== "draft" || !draftThread?.promotedTo) {
126 + if (target?.kind !== "draft") {
127 return null;
128 }
128 - return draftThread.promotedTo;
129 + return draftThread?.promotedTo ?? promotedDraftRedirect;
130 }