diff --git a/src/app/navigation.tsx b/src/app/navigation.tsx index 9075f155..c77d665c 100644 --- a/src/app/navigation.tsx +++ b/src/app/navigation.tsx @@ -11,26 +11,26 @@ import { export const rootNavItems: SidebarNavItem[] = [ { title: "Home", - href: "/" - // icon: + href: "/", + icon: } ]; export const orgNavItems: SidebarNavItem[] = [ { title: "Sites", - href: "/{orgId}/settings/sites" - // icon: + href: "/{orgId}/settings/sites", + icon: }, { title: "Resources", - href: "/{orgId}/settings/resources" - // icon: + href: "/{orgId}/settings/resources", + icon: }, { title: "Access Control", href: "/{orgId}/settings/access", - // icon: , + icon: , autoExpand: true, children: [ { @@ -51,20 +51,20 @@ export const orgNavItems: SidebarNavItem[] = [ }, { title: "Shareable Links", - href: "/{orgId}/settings/share-links" - // icon: + href: "/{orgId}/settings/share-links", + icon: }, { title: "Settings", - href: "/{orgId}/settings/general" - // icon: + href: "/{orgId}/settings/general", + icon: } ]; export const adminNavItems: SidebarNavItem[] = [ { title: "All Users", - href: "/admin/users" - // icon: + href: "/admin/users", + icon: } ]; diff --git a/src/components/SidebarNav.tsx b/src/components/SidebarNav.tsx index 39c9ac07..0d2c15af 100644 --- a/src/components/SidebarNav.tsx +++ b/src/components/SidebarNav.tsx @@ -35,26 +35,6 @@ export function SidebarNav({ const userId = params.userId as string; const [expandedItems, setExpandedItems] = useState>(new Set()); - // Initialize expanded items based on autoExpand property - useEffect(() => { - const autoExpanded = new Set(); - - function findAutoExpanded(items: SidebarNavItem[]) { - items.forEach(item => { - const hydratedHref = hydrateHref(item.href); - if (item.autoExpand) { - autoExpanded.add(hydratedHref); - } - if (item.children) { - findAutoExpanded(item.children); - } - }); - } - - findAutoExpanded(items); - setExpandedItems(autoExpanded); - }, [items]); - function hydrateHref(val: string): string { return val .replace("{orgId}", orgId) @@ -63,6 +43,34 @@ export function SidebarNav({ .replace("{userId}", userId); } + // Initialize expanded items based on autoExpand property and current path + useEffect(() => { + const autoExpanded = new Set(); + + function findAutoExpandedAndActivePath(items: SidebarNavItem[], parentHrefs: string[] = []) { + items.forEach(item => { + const hydratedHref = hydrateHref(item.href); + + // Add current item's href to the path + const currentPath = [...parentHrefs, hydratedHref]; + + // Auto expand if specified or if this item or any child is active + if (item.autoExpand || pathname.startsWith(hydratedHref)) { + // Expand all parent sections when a child is active + currentPath.forEach(href => autoExpanded.add(href)); + } + + // Recursively check children + if (item.children) { + findAutoExpandedAndActivePath(item.children, currentPath); + } + }); + } + + findAutoExpandedAndActivePath(items); + setExpandedItems(autoExpanded); + }, [items, pathname]); + function toggleItem(href: string) { setExpandedItems(prev => { const newSet = new Set(prev); @@ -86,41 +94,49 @@ export function SidebarNav({ return (
- { - if (disabled) { - e.preventDefault(); - } else if (onItemClick) { - onItemClick(); - } - }} - tabIndex={disabled ? -1 : undefined} - aria-disabled={disabled} > - {item.icon && {item.icon}} - {item.title} - - {hasChildren && ( - + )} +
{hasChildren && isExpanded && (
@@ -135,7 +151,7 @@ export function SidebarNav({ return (