| 1 | import { useLocation, useNavigate } from "@tanstack/react-router"; |
| 2 | import { useCallback, useEffect } from "react"; |
| 3 | |
| 4 | // Settings, Usage, Deployments, and Pull Requests replace the sidebar utility row with a |
| 5 | // Back button. Everything else is the main app. Legacy `/projects/<key>` links |
| 6 | // redirect into settings, so they count too and are never remembered. |
| 7 | export function isSidebarUtilityPage(pathname: string) { |
| 8 | return ( |
| 9 | pathname === "/settings" || |
| 10 | pathname.startsWith("/settings/") || |
| 11 | pathname.startsWith("/projects/") || |
| 12 | pathname === "/usage" || |
| 13 | pathname === "/deployments" || |
| 14 | pathname === "/pull-requests" |
| 15 | ); |
| 16 | } |
| 17 | |
| 18 | // Mount once in the app shell. Records the latest main app URL so Back can |
| 19 | // return there no matter how many utility pages were visited since. |
| 20 | export function MainAppLocationTracker() { |
| 21 | const href = useLocation({ |
| 22 | select: (location) => (isSidebarUtilityPage(location.pathname) ? null : location.href), |
| 23 | }); |
| 24 | useEffect(() => { |
| 25 | if (href !== null) sessionStorage.setItem("t3code-main-app-href", href); |
| 26 | }, [href]); |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | // Leaves a utility page for the last main app URL, or the thread list when |
| 31 | // the app was opened directly on a utility page. |
| 32 | export function useNavigateToMainApp() { |
| 33 | const navigate = useNavigate(); |
| 34 | return useCallback( |
| 35 | () => navigate({ href: sessionStorage.getItem("t3code-main-app-href") ?? "/" }), |
| 36 | [navigate], |
| 37 | ); |
| 38 | } |