Files
baya-monorepo/client/src/layout/matchActivePath.ts
T
2026-07-17 19:05:17 +03:30

16 lines
828 B
TypeScript

/**
* Longest-prefix, winner-takes-all active-path matching shared by the sidebar and every bottom
* bar. Given the current (locale-stripped) pathname and a set of nav-item paths, returns the
* single path that should render as active — the longest candidate that prefixes the pathname —
* so e.g. `/nurse/requests` lights up "Requests" and not the "Dashboard" root (`/nurse`), and
* `/patients/123` still lights up the `/patients` tab.
*/
export function matchActivePath(pathname: string, paths: ReadonlyArray<string | undefined>): string | undefined {
const isMatch = (path: string) => (path === '/' ? pathname === path : pathname === path || pathname.startsWith(`${path}/`));
return paths
.filter((path): path is string => Boolean(path))
.filter(isMatch)
.sort((a, b) => b.length - a.length)[0];
}