Open History to see the last commit
1import { useNavigate, useRouter, type AnyRouter } from "@tanstack/react-router";
2
3import { GUEST_PAGE_PATH } from "../../guestPage";
4import { withoutAssistantCitationHash } from "../../lib/assistantCitationNavigation";
5import { resolveThreadRouteTarget, type ThreadRouteTarget } from "../../threadRoutes";
6
7/** Settings and Usage open as modals over the last main-app view. */
8export function isAppModalPage(pathname: string) {
9 return pathname === "/settings" || pathname.startsWith("/settings/") || pathname === "/usage";
10}
11
12export function isSidebarUtilityPage(pathname: string) {
13 return (
14 pathname.startsWith("/projects/") ||
15 pathname === "/pull-requests" ||
16 pathname === "/issues" ||
17 pathname === "/workflows"
18 );
19}
20
21// Pages the root route always renders without the app shell, so they are never
22// a page to return to. Other paths seen before auth or on a prompt-link landing
23// are still recorded; they are real app URLs, so returning to one is harmless.
24const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_PAGE_PATH]);
25
26let mainAppHref: string | null = null;
27let appModalReturnHref: string | null = null;
28let appModalReturnHistoryIndex: number | null = null;
29let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
30let initialHistoryIndex = 0;
31
32/** Remembers where Settings, Usage, and the utility pages lead back to. */
33export function startMainAppLocationTracking(router: AnyRouter): () => void {
34 initialHistoryIndex = router.state.location.state.__TSR_index;
35 const record = () => {
36 const { pathname } = router.state.location;
37 if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
38 const href = withoutAssistantCitationHash(router.state.location.href);
39 appModalReturnHref = href;
40 // Only an entry that is exactly the return location can be stepped back to.
41 appModalReturnHistoryIndex =
42 href === router.state.location.href ? router.state.location.state.__TSR_index : null;
43 appModalReturnThreadTarget = resolveThreadRouteTarget(
44 router.state.matches.at(-1)?.params ?? {},
45 );
46 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
47 };
48 record();
49 return router.subscribe("onResolved", record);
50}
51
52/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
53export function getAppModalReturnThreadTarget() {
54 return appModalReturnThreadTarget;
55}
56
57export function useNavigateToMainApp() {
58 const navigate = useNavigate();
59 return () => navigate({ href: mainAppHref ?? "/" });
60}
61
62/** Leaves Settings or Usage for the page that opened it. */
63export function useCloseAppModal() {
64 const navigate = useNavigate();
65 const router = useRouter();
66 return () => {
67 const href = appModalReturnHref ?? mainAppHref ?? "/";
68 const returnIndex = appModalReturnHistoryIndex ?? 0;
69 const steps = router.state.location.state.__TSR_index - returnIndex;
70 // Stepping back past this document's first entry reloads the page. Written as a negated
71 // comparison so a missing (NaN) history index also falls back to client navigation.
72 if (!(returnIndex > initialHistoryIndex && steps > 0)) return navigate({ href, replace: true });
73 // Go back to the entry before the return page and push it again: only a push drops the
74 // modal's entries from forward history, and reusing the old entry avoids a duplicate stop.
75 const unsubscribe = router.history.subscribe(() => {
76 unsubscribe();
77 void navigate({ href });
78 });
79 router.history.go(-(steps + 1));
80 };
81}