Files
t3code/apps/web/src/components/sidebar/mainAppLocation.ts
T
2026-09-24 16:38:53 -07:00

37 lines
1.3 KiB
TypeScript

import { useLocation, useNavigate } from "@tanstack/react-router";
import { useCallback, useEffect } from "react";
// Settings, Usage, and Pull Requests replace the sidebar utility row with a
// Back button. Everything else is the main app. Legacy `/projects/<key>` links
// redirect into settings, so they count too and are never remembered.
export function isSidebarUtilityPage(pathname: string) {
return (
pathname === "/settings" ||
pathname.startsWith("/settings/") ||
pathname.startsWith("/projects/") ||
pathname === "/usage" ||
pathname === "/pull-requests"
);
}
let mainAppHref: string | null = null;
// Mount once in the app shell. Records the latest main app URL so Back can
// return there no matter how many utility pages were visited since.
export function MainAppLocationTracker() {
const href = useLocation({
select: (location) => (isSidebarUtilityPage(location.pathname) ? null : location.href),
});
useEffect(() => {
if (href !== null) mainAppHref = href;
}, [href]);
return null;
}
// Leaves a utility page for the last main app URL, or the thread list when
// the app was opened directly on a utility page.
export function useNavigateToMainApp() {
const navigate = useNavigate();
return useCallback(() => navigate({ href: mainAppHref ?? "/" }), [navigate]);
}