"use client"; import React, { useEffect } from "react"; import Link from "next/link"; import { useParams, usePathname, useRouter } from "next/navigation"; import { cn } from "@app/lib/cn"; import { buttonVariants } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; interface SidebarNavItem { href: string; title: string; icon?: React.ReactNode; children?: SidebarNavItem[]; } interface SidebarNavProps extends React.HTMLAttributes { items: SidebarNavItem[]; disabled?: boolean; } export function SidebarNav({ className, items, disabled = false, ...props }: SidebarNavProps) { const pathname = usePathname(); const params = useParams(); const orgId = params.orgId as string; const niceId = params.niceId as string; const resourceId = params.resourceId as string; const userId = params.userId as string; const [selectedValue, setSelectedValue] = React.useState(getSelectedValue()); useEffect(() => { setSelectedValue(getSelectedValue()); }, [usePathname()]); const router = useRouter(); const handleSelectChange = (value: string) => { if (!disabled) { router.push(value); } }; function getSelectedValue() { const item = items.find((item) => hydrateHref(item.href) === pathname); return hydrateHref(item?.href || ""); } function hydrateHref(val: string): string { return val .replace("{orgId}", orgId) .replace("{niceId}", niceId) .replace("{resourceId}", resourceId) .replace("{userId}", userId); } function renderItems(items: SidebarNavItem[]) { return items.map((item) => (
e.preventDefault() : undefined} tabIndex={disabled ? -1 : undefined} aria-disabled={disabled} > {item.icon ? (
{item.icon} {item.title}
) : ( item.title )} {item.children && (
{renderItems(item.children)}{" "} {/* Recursively render children */}
)}
)); } return (
); }