Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mainAppLocation.ts

45 lines1.8 KB

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