feat: add findMenuPath helper function

This commit is contained in:
ValdiANS
2026-01-23 23:04:04 +07:00
parent 25074edaa1
commit 8cc7f2f526
+31
View File
@@ -2,6 +2,7 @@ import moment from 'moment';
import 'moment/locale/id';
import { twMerge } from 'tailwind-merge';
import clsx, { ClassValue } from 'clsx';
import { SidebarMenuItem } from '@/components/molecules/SidebarMenu';
// set locale globally
moment.locale('id');
@@ -142,3 +143,33 @@ export const isPathActive = (pathname: string, link?: string) => {
return pathname.startsWith(link) && isActiveLinkValid;
};
export function findMenuPath(
menus: readonly SidebarMenuItem[],
pathname: string,
parents: SidebarMenuItem[] = []
): SidebarMenuItem[] | null {
for (const menu of menus) {
const currentPath = [...parents, menu];
// Exact match
if (menu.link === pathname) {
return currentPath;
}
// Prefix match (useful for pages like /add, /edit, etc.)
if (pathname.startsWith(menu.link + '/')) {
if (!menu.submenu) {
return currentPath;
}
}
// Search children
if (menu.submenu) {
const found = findMenuPath(menu.submenu, pathname, currentPath);
if (found) return found;
}
}
return null;
}