From 94ac0a1d025eb162a3cf94d6cf51242ae591229b Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:54 +0530 Subject: [PATCH] feat: add active nav hook --- app/hooks/admin/use-active-nav.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/hooks/admin/use-active-nav.ts diff --git a/app/hooks/admin/use-active-nav.ts b/app/hooks/admin/use-active-nav.ts new file mode 100644 index 0000000..196f2c2 --- /dev/null +++ b/app/hooks/admin/use-active-nav.ts @@ -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 }; +}