mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 13:32:00 +00:00
feat: add findMenuPath helper function
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user