"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"; import { CornerDownRight } from "lucide-react"; 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() { let foundHref = ""; for (const item of items) { const hydratedHref = hydrateHref(item.href); if (hydratedHref === pathname) { foundHref = hydratedHref; break; } if (item.children) { for (const child of item.children) { const hydratedChildHref = hydrateHref(child.href); if (hydratedChildHref === pathname) { foundHref = hydratedChildHref; break; } } } if (foundHref) break; } return foundHref; } 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 && (
{item.children.map((child) => (
e.preventDefault() : undefined } tabIndex={disabled ? -1 : undefined} aria-disabled={disabled} > {child.icon ? (
{child.icon} {child.title}
) : ( child.title )}
))}
)}
)); } return (
); }