@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
29 lines (28 loc) • 1.3 kB
JavaScript
//#region src/dashboard/ConsoleNav/active.ts
function matchesNavPath(pathname, item) {
if (item.end || item.href === "/") return pathname === item.href;
return pathname === item.href || pathname.startsWith(`${item.href}/`);
}
/** Longest matching href wins, so `/resources/new` does not also light up `/`. */
function resolveActiveHref(pathname, items) {
return items.filter((item) => matchesNavPath(pathname, item)).sort((left, right) => right.href.length - left.href.length)[0]?.href;
}
function collectNavItems(groups) {
return groups.flatMap((group) => [...group.items ?? [], ...collectNavItems(group.groups ?? [])]);
}
/**
* Keys of the groups on the path to the active page, outermost first. A group
* overview page (`href`) counts as being inside that group.
*/
function findActiveBranch(pathname, groups, activeHref) {
for (const group of groups) {
if (group.href && pathname === group.href) return [group.key];
const nested = findActiveBranch(pathname, group.groups ?? [], activeHref);
if (nested.length > 0) return [group.key, ...nested];
if (activeHref && group.items?.some((item) => item.href === activeHref)) return [group.key];
}
return [];
}
//#endregion
export { collectNavItems, findActiveBranch, matchesNavPath, resolveActiveHref };
//# sourceMappingURL=active.mjs.map