Open History to see the last commit
1import { useLocation, useNavigate } from "@tanstack/react-router";
2import { useCallback, useEffect } from "react";
3
4// Settings, Usage, 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.
7export function isSidebarUtilityPage(pathname: string) {
8 return (
9 pathname === "/settings" ||
10 pathname.startsWith("/settings/") ||
11 pathname.startsWith("/projects/") ||
12 pathname === "/usage" ||
13 pathname === "/pull-requests"
14 );
15}
16
17let mainAppHref: string | null = null;
18
19// Mount once in the app shell. Records the latest main app URL so Back can
20// return there no matter how many utility pages were visited since.
21export function MainAppLocationTracker() {
22 const href = useLocation({
23 select: (location) => (isSidebarUtilityPage(location.pathname) ? null : location.href),
24 });
25 useEffect(() => {
26 if (href !== null) mainAppHref = href;
27 }, [href]);
28 return null;
29}
30
31// Leaves a utility page for the last main app URL, or the thread list when
32// the app was opened directly on a utility page.
33export function useNavigateToMainApp() {
34 const navigate = useNavigate();
35 return useCallback(() => navigate({ href: mainAppHref ?? "/" }), [navigate]);
36}