forked from rajun/ar-test
feat: add active nav hook
This commit is contained in:
parent
b4d6f976dd
commit
94ac0a1d02
28
app/hooks/admin/use-active-nav.ts
Normal file
28
app/hooks/admin/use-active-nav.ts
Normal file
@ -0,0 +1,28 @@
|
||||
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 };
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user