The best way to control Claude Code, Codex and more from any computer or phone
fix(web): closing Settings no longer leaves it as a back/forward stop (#36437)
Julius Marminge committed
Nov 13, 2026 at 15:18 UTC
ab9e6a2792c5e65d3ef75d29327e0c4f7288adc4
2 files changed
+40
-2
apps/web/src/components/sidebar/mainAppLocation.browser.test.tsx
+21
@@ -74,4 +74,25 @@ describe("main app return location", () => {
74
sidebarBack: THREAD_PATH,
75
});
76
});
77
+
78
+ it("leaves no stop on Settings for history back after it closes", async () => {
79
+ const otherThreadPath = "/environment-one/thread-two";
80
+ await using screen = await render(<ReturnControls />, { router: THREAD_PATH });
81
+ const router = screen.router!;
82
+ using _tracking = { [Symbol.dispose]: startMainAppLocationTracking(router) };
83
+
84
+ await settled(router, router.navigate({ href: otherThreadPath }));
85
+ await settled(router, router.navigate({ href: "/settings/general" }));
86
+ await userEvent.click(screen.getByRole("button", { name: "Close modal" }));
87
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
88
+
89
+ router.history.forward();
90
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
91
+ router.history.back();
92
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(THREAD_PATH));
93
+ router.history.forward();
94
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
95
+ router.history.forward();
96
+ await vi.waitFor(() => expect(router.state.location.pathname).toBe(otherThreadPath));
97
+ });
98
});
apps/web/src/components/sidebar/mainAppLocation.ts
+19
-2
@@ -1,4 +1,4 @@
1
-import { useNavigate, type AnyRouter } from "@tanstack/react-router";
1
+import { useNavigate, useRouter, type AnyRouter } from "@tanstack/react-router";
2
3
import { GUEST_PAGE_PATH } from "../../guestPage";
4
import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
@@ -20,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 appModalReturnHistoryIndex: number | null = null;
24
let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
25
26
/** Remembers where Settings, Usage, and the utility pages lead back to. */
@@ -29,6 +30,9 @@ export function startMainAppLocationTracking(router: AnyRouter): () => void {
30
if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
31
const href = withoutAssistantCitationHash(router.state.location.href);
32
appModalReturnHref = href;
33
+ // Only an entry that is exactly the return location can be stepped back to.
34
+ appModalReturnHistoryIndex =
35
+ href === router.state.location.href ? router.state.location.state.__TSR_index : null;
36
appModalReturnThreadTarget = resolveThreadRouteTarget(
37
router.state.matches.at(-1)?.params ?? {},
38
);
@@ -51,5 +55,18 @@ export function useNavigateToMainApp() {
55
/** Leaves Settings or Usage for the page that opened it. */
56
export function useCloseAppModal() {
57
const navigate = useNavigate();
54
- return () => navigate({ href: appModalReturnHref ?? mainAppHref ?? "/" });
58
+ const router = useRouter();
59
+ return () => {
60
+ const href = appModalReturnHref ?? mainAppHref ?? "/";
61
+ const returnIndex = appModalReturnHistoryIndex ?? 0;
62
+ const steps = router.state.location.state.__TSR_index - returnIndex;
63
+ if (returnIndex === 0 || steps <= 0) return navigate({ href });
64
+ // Go back to the entry before the return page and push it again: only a push drops the
65
+ // modal's entries from forward history, and reusing the old entry avoids a duplicate stop.
66
+ const unsubscribe = router.history.subscribe(() => {
67
+ unsubscribe();
68
+ void navigate({ href });
69
+ });
70
+ router.history.go(-(steps + 1));
71
+ };
72
}