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