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

mainAppLocation.ts

72 lines3.1 KB

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";
11​
12export function isSidebarUtilityPage(pathname: string) {
13 return pathname.startsWith("/projects/") || pathname === "/pull-requests";
15​
16// Pages the root route always renders without the app shell, so they are never
17// a page to return to. Other paths seen before auth or on a prompt-link landing
18// are still recorded; they are real app URLs, so returning to one is harmless.
19const SHELL_LESS_PATHS = new Set(["/pair", "/connect", "/connect-agent", GUEST_PAGE_PATH]);
20​
21let mainAppHref: string | null = null;
22let appModalReturnHref: string | null = null;
23let appModalReturnHistoryIndex: number | null = null;
24let appModalReturnThreadTarget: ThreadRouteTarget | null = null;
25​
26/** Remembers where Settings, Usage, and the utility pages lead back to. */
27export function startMainAppLocationTracking(router: AnyRouter): () => void {
28 const record = () => {
29 const { pathname } = router.state.location;
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 );
39 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
40 };
41 record();
42 return router.subscribe("onResolved", record);
44​
45/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
46export function getAppModalReturnThreadTarget() {
47 return appModalReturnThreadTarget;
49​
50export function useNavigateToMainApp() {
51 const navigate = useNavigate();
52 return () => navigate({ href: mainAppHref ?? "/" });
54​
55/** Leaves Settings or Usage for the page that opened it. */
56export function useCloseAppModal() {
57 const navigate = useNavigate();
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 };