Open History to see the last commit
1import { useLocation, useNavigate } from "@tanstack/react-router";
2import { useEffect } from "react";
3
4/** Settings and Usage open as modals over the last main-app view. */
5export function isAppModalPage(pathname: string) {
6 return pathname === "/settings" || pathname.startsWith("/settings/") || pathname === "/usage";
7}
8
9export function isSidebarUtilityPage(pathname: string) {
10 return pathname.startsWith("/projects/") || pathname === "/pull-requests";
11}
12
13let mainAppHref: string | null = null;
14let appModalReturnHref: string | null = null;
15
16export function MainAppLocationTracker() {
17 const href = useLocation({ select: (location) => location.href });
18 const pathname = useLocation({ select: (location) => location.pathname });
19 useEffect(() => {
20 if (isAppModalPage(pathname)) return;
21 appModalReturnHref = href;
22 if (!isSidebarUtilityPage(pathname)) mainAppHref = href;
23 }, [href, pathname]);
24 return null;
25}
26
27export function useNavigateToMainApp() {
28 const navigate = useNavigate();
29 return () => navigate({ href: mainAppHref ?? "/" });
30}
31
32/** Leaves Settings or Usage for the page that opened it. */
33export function useCloseAppModal() {
34 const navigate = useNavigate();
35 return () => navigate({ href: appModalReturnHref ?? mainAppHref ?? "/" });
36}