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

mainAppLocation.ts

55 lines2.3 KB

1import { useNavigate, 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 appModalReturnThreadTarget: ThreadRouteTarget | null = null;
24​
25/** Remembers where Settings, Usage, and the utility pages lead back to. */
26export function startMainAppLocationTracking(router: AnyRouter): () => void {
27 const record = () => {
28 const { pathname } = router.state.location;
29 if (isAppModalPage(pathname) || SHELL_LESS_PATHS.has(pathname)) return;
30 const href = withoutAssistantCitationHash(router.state.location.href);
31 appModalReturnHref = href;
32 appModalReturnThreadTarget = resolveThreadRouteTarget(
33 router.state.matches.at(-1)?.params ?? {},
34 );
35 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
36 };
37 record();
38 return router.subscribe("onResolved", record);
40​
41/** The thread Settings or Usage opened over, for views mounted while the modal is open. */
42export function getAppModalReturnThreadTarget() {
43 return appModalReturnThreadTarget;
45​
46export function useNavigateToMainApp() {
47 const navigate = useNavigate();
48 return () => navigate({ href: mainAppHref ?? "/" });
50​
51/** Leaves Settings or Usage for the page that opened it. */
52export function useCloseAppModal() {
53 const navigate = useNavigate();
54 return () => navigate({ href: appModalReturnHref ?? mainAppHref ?? "/" });