Open History to see the last commit
1import { useNavigate, type AnyRouter } from "@tanstack/react-router";
2
3import { GUEST_PAGE_PATH } from "../../guestPage";
4
5/** Settings and Usage open as modals over the last main-app view. */
6export function isAppModalPage(pathname: string) {
7 return pathname === "/settings" || pathname.startsWith("/settings/") || pathname === "/usage";
8}
9
10export function isSidebarUtilityPage(pathname: string) {
11 return pathname.startsWith("/projects/") || pathname === "/pull-requests";
12}
13
14// Pages the root route always renders without the app shell, so they are never
15// a page to return to. Other paths seen before auth or on a prompt-link landing
16// are still recorded; they are real app URLs, so returning to one is harmless.
17const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_PAGE_PATH]);
18
19let mainAppHref: string | null = null;
20let appModalReturnHref: string | null = null;
21
22/** Remembers where Settings, Usage, and the utility pages lead back to. */
23export function startMainAppLocationTracking(router: AnyRouter): () => void {
24 const record = () => {
25 const { pathname, href } = router.state.location;
26 if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
27 appModalReturnHref = href;
28 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
29 };
30 record();
31 return router.subscribe("onResolved", record);
32}
33
34export function useNavigateToMainApp() {
35 const navigate = useNavigate();
36 return () => navigate({ href: mainAppHref ?? "/" });
37}
38
39/** Leaves Settings or Usage for the page that opened it. */
40export function useCloseAppModal() {
41 const navigate = useNavigate();
42 return () => navigate({ href: appModalReturnHref ?? mainAppHref ?? "/" });
43}