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

mainAppLocation.ts

76 lines3.4 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;
25let initialHistoryIndex = 0;
26​
27/** Remembers where Settings, Usage, and the utility pages lead back to. */
28export function startMainAppLocationTracking(router: AnyRouter): () => void {
29 initialHistoryIndex = router.state.location.state.__TSR_index;
30 const record = () => {
31 const { pathname } = router.state.location;
32 if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
33 const href = withoutAssistantCitationHash(router.state.location.href);
34 appModalReturnHref = href;
35 // Only an entry that is exactly the return location can be stepped back to.
36 appModalReturnHistoryIndex =
37 href === router.state.location.href ? router.state.location.state.__TSR_index : null;
38 appModalReturnThreadTarget = resolveThreadRouteTarget(
39 router.state.matches.at(-1)?.params ?? {},
40 );
41 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
42 };
43 record();
44 return router.subscribe("onResolved", record);
46​
47/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
48export function getAppModalReturnThreadTarget() {
49 return appModalReturnThreadTarget;
51​
52export function useNavigateToMainApp() {
53 const navigate = useNavigate();
54 return () => navigate({ href: mainAppHref ?? "/" });
56​
57/** Leaves Settings or Usage for the page that opened it. */
58export function useCloseAppModal() {
59 const navigate = useNavigate();
60 const router = useRouter();
61 return () => {
62 const href = appModalReturnHref ?? mainAppHref ?? "/";
63 const returnIndex = appModalReturnHistoryIndex ?? 0;
64 const steps = router.state.location.state.__TSR_index - returnIndex;
65 // Stepping back past this document's first entry reloads the page. Written as a negated
66 // comparison so a missing (NaN) history index also falls back to client navigation.
67 if (!(returnIndex > initialHistoryIndex && steps > 0)) return navigate({ href, replace: true });
68 // Go back to the entry before the return page and push it again: only a push drops the
69 // modal's entries from forward history, and reusing the old entry avoids a duplicate stop.
70 const unsubscribe = router.history.subscribe(() => {
71 unsubscribe();
72 void navigate({ href });
73 });
74 router.history.go(-(steps + 1));
75 };