"use client"; import Link from "next/link"; import { ChevronDown, ChevronRight, CircleHelp, KeyRound, LogOut } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { AppLogo } from "@/components/branding/app-logo"; import { LogoutButton } from "@/components/auth/logout-button"; import { useLocale } from "@/components/providers/locale-provider"; import { getNavigationForRole, isNavGroup } from "@/config/navigation"; import type { SessionUser } from "@/lib/auth"; import { cn } from "@/lib/utils"; type SidebarProps = { pathname: string; user: SessionUser; }; function matchesPath(currentPath: string, href: string) { return currentPath === href || currentPath.startsWith(`${href}/`); } function findActiveHref(currentPath: string, items: Array<{ href: string }>) { return items .filter((item) => matchesPath(currentPath, item.href)) .sort((a, b) => b.href.length - a.href.length)[0]?.href; } export function Sidebar({ pathname, user }: SidebarProps) { const { dict } = useLocale(); const navigation = useMemo(() => getNavigationForRole(user.role as never), [user.role]); const buildOpenGroups = () => { const grouped = navigation.filter(isNavGroup); return grouped.reduce>((acc, item) => { const isGroupActive = Boolean(findActiveHref(pathname, item.children)); acc[item.key] = isGroupActive; return acc; }, {}); }; const [openGroups, setOpenGroups] = useState>(buildOpenGroups); useEffect(() => { setOpenGroups((current) => { const next = { ...current }; navigation.filter(isNavGroup).forEach((item) => { const isGroupActive = Boolean(findActiveHref(pathname, item.children)); if (isGroupActive) { next[item.key] = true; } }); return next; }); }, [pathname, navigation]); return ( ); }