29 lines
1.3 KiB
TypeScript
29 lines
1.3 KiB
TypeScript
import { useLocation, useMatches, useParams } from 'react-router';
|
|
import { ASSET_FILTER } from '~/data/assets/constants';
|
|
import { ROUTES } from '~/data/routes';
|
|
import type { ArAsset } from '~/types/asset';
|
|
|
|
export type NavSection = 'projects' | 'assets' | 'unassigned' | 'settings';
|
|
|
|
const ASSET_ROUTE_ID = 'routes/admin/assets/$id';
|
|
|
|
// Which sidebar item and client are highlighted for the current page
|
|
export function useActiveNav(): { section: NavSection; projectId?: string } {
|
|
const { pathname, search } = useLocation();
|
|
const params = useParams();
|
|
const matches = useMatches();
|
|
|
|
if (pathname.startsWith(ROUTES.settings)) return { section: 'settings' };
|
|
if (pathname.startsWith(ROUTES.projects)) return { section: 'projects', projectId: params.id };
|
|
|
|
const assetMatch = matches.find((match) => match.id === ASSET_ROUTE_ID);
|
|
if (assetMatch) {
|
|
const projectId = (assetMatch.data as { asset?: ArAsset } | undefined)?.asset?.project || undefined;
|
|
return { section: projectId ? 'assets' : 'unassigned', projectId };
|
|
}
|
|
|
|
const filter = new URLSearchParams(search).get(ASSET_FILTER.param) ?? ASSET_FILTER.all;
|
|
if (filter === ASSET_FILTER.unassigned) return { section: 'unassigned' };
|
|
return { section: 'assets', projectId: filter === ASSET_FILTER.all ? undefined : filter };
|
|
}
|