From 8cc7f2f526f8c256ba8f955faa960be380d70f6b Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Fri, 23 Jan 2026 23:04:04 +0700 Subject: [PATCH] feat: add findMenuPath helper function --- src/lib/helper.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib/helper.ts b/src/lib/helper.ts index c69f610f..dcd56e16 100644 --- a/src/lib/helper.ts +++ b/src/lib/helper.ts @@ -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; +}