import { ChevronDown } from "lucide-react";
import {
type ComponentType,
type ReactNode,
useEffect,
useId,
useState,
} from "react";
import { NavLink, useLocation } from "react-router-dom";
import { cn } from "../../lib/utils";
export type SidebarNavChild = {
label: string;
to: string;
end?: boolean;
};
type SidebarNavGroupProps = {
label: string;
icon: ComponentType<{ className?: string }>;
basePath: string;
/** When set, any matching prefix marks the group active (e.g. `/content` and `/new-content`). */
activePaths?: string[];
children: SidebarNavChild[];
isCollapsed: boolean;
onNavigate?: () => void;
trailing?: ReactNode;
};
export function SidebarNavGroup({
label,
icon: Icon,
basePath,
activePaths,
children,
isCollapsed,
onNavigate,
trailing,
}: SidebarNavGroupProps) {
const location = useLocation();
const panelId = useId();
const paths = activePaths?.length ? activePaths : [basePath];
const isSectionActive = paths.some((path) =>
location.pathname.startsWith(path),
);
const [expanded, setExpanded] = useState(isSectionActive);
useEffect(() => {
if (isSectionActive) {
// eslint-disable-next-line react-hooks/set-state-in-effect
setExpanded(true);
}
}, [isSectionActive]);
if (isCollapsed) {
return (
cn(
"sidebar-nav-item relative group flex items-center justify-center rounded-xl p-2 text-sm font-medium text-grayScale-600 transition",
"hover:bg-grayScale-100 hover:text-brand-600 dark:text-grayScale-400 dark:hover:bg-white/5 dark:hover:text-brand-400",
isActive &&
"sidebar-nav-item-active bg-brand-500/10 text-brand-600 hover:bg-brand-500/10 hover:text-brand-600 dark:bg-brand-500/15 dark:text-brand-400 dark:hover:bg-brand-500/15 dark:hover:text-brand-400",
)
}
title={label}
>
{({ isActive }) => (
{trailing}
)}
);
}
return (
{children.map((child) => (
cn(
"sidebar-nav-item block rounded-lg px-3 py-1.5 text-sm font-medium transition",
isActive
? "sidebar-subnav-item-active bg-brand-500/10 text-black font-medium hover:bg-brand-500/10 hover:text-black dark:bg-brand-500/15 dark:text-white dark:hover:bg-brand-500/15 dark:hover:text-white"
: "text-grayScale-500 hover:bg-grayScale-100 hover:text-brand-600 dark:text-grayScale-400 dark:hover:bg-white/5 dark:hover:text-brand-400",
)
}
>
{child.label}
))}
);
}