import { useCallback, useEffect, useId, useState } from 'react'; export type MenuItemChildrenType = { id: string; name: string; menuUrl: string; }; export type MenuItemType = { id: string; name: string; menuUrl?: null; children: MenuItemChildrenType[]; }; export interface MenuProps { items: MenuItemType[]; onSelectionChange: (menu: MenuItemChildrenType) => void; currentPath: string; } export const Menu = (props: MenuProps) => { const { items, currentPath, onSelectionChange } = props; const id = useId(); const getCurrentPathItem = useCallback(() => { return items.find((i) => i.children.some((c) => currentPath.includes(c.menuUrl))); }, [items, currentPath]); const [expandedItemKeys, setExpandedItemKeys] = useState(() => { const item = getCurrentPathItem(); return item ? [item.id] : []; }); useEffect(() => { const item = getCurrentPathItem(); setExpandedItemKeys((prev) => { if (item && prev.length === 1 && prev[0] === item.id) { return prev; } return item ? [item.id] : []; }); }, [getCurrentPathItem]); return ( ); };