2024-11-09 00:08:17 -05:00
|
|
|
"use client";
|
|
|
|
|
2025-04-12 21:35:17 -04:00
|
|
|
import React, { useState, useEffect } from "react";
|
2024-11-09 00:08:17 -05:00
|
|
|
import Link from "next/link";
|
2025-04-12 15:04:32 -04:00
|
|
|
import { useParams, usePathname } from "next/navigation";
|
2025-01-01 21:41:31 -05:00
|
|
|
import { cn } from "@app/lib/cn";
|
2025-04-12 21:35:17 -04:00
|
|
|
import { ChevronDown, ChevronRight } from "lucide-react";
|
2024-10-13 16:18:54 -04:00
|
|
|
|
2025-04-12 21:35:17 -04:00
|
|
|
export interface SidebarNavItem {
|
2025-04-09 09:23:47 -07:00
|
|
|
href: string;
|
|
|
|
title: string;
|
|
|
|
icon?: React.ReactNode;
|
|
|
|
children?: SidebarNavItem[];
|
2025-04-12 21:35:17 -04:00
|
|
|
autoExpand?: boolean;
|
2025-04-09 09:23:47 -07:00
|
|
|
}
|
|
|
|
|
2025-04-12 21:35:17 -04:00
|
|
|
export interface SidebarNavProps extends React.HTMLAttributes<HTMLElement> {
|
2025-04-09 09:23:47 -07:00
|
|
|
items: SidebarNavItem[];
|
2024-11-09 00:08:17 -05:00
|
|
|
disabled?: boolean;
|
2024-10-13 16:18:54 -04:00
|
|
|
}
|
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
export function SidebarNav({
|
|
|
|
className,
|
|
|
|
items,
|
|
|
|
disabled = false,
|
|
|
|
...props
|
|
|
|
}: SidebarNavProps) {
|
2024-10-13 23:42:09 -04:00
|
|
|
const pathname = usePathname();
|
|
|
|
const params = useParams();
|
|
|
|
const orgId = params.orgId as string;
|
2024-10-14 22:26:32 -04:00
|
|
|
const niceId = params.niceId as string;
|
2024-10-13 23:42:09 -04:00
|
|
|
const resourceId = params.resourceId as string;
|
2024-11-09 23:59:19 -05:00
|
|
|
const userId = params.userId as string;
|
2025-04-12 21:35:17 -04:00
|
|
|
const [expandedItems, setExpandedItems] = useState<Set<string>>(new Set());
|
|
|
|
|
|
|
|
// Initialize expanded items based on autoExpand property
|
|
|
|
useEffect(() => {
|
|
|
|
const autoExpanded = new Set<string>();
|
|
|
|
|
|
|
|
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]);
|
2024-10-13 16:18:54 -04:00
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
function hydrateHref(val: string): string {
|
|
|
|
return val
|
|
|
|
.replace("{orgId}", orgId)
|
|
|
|
.replace("{niceId}", niceId)
|
2024-11-09 23:59:19 -05:00
|
|
|
.replace("{resourceId}", resourceId)
|
|
|
|
.replace("{userId}", userId);
|
2024-11-09 00:08:17 -05:00
|
|
|
}
|
|
|
|
|
2025-04-12 21:35:17 -04:00
|
|
|
function toggleItem(href: string) {
|
|
|
|
setExpandedItems(prev => {
|
|
|
|
const newSet = new Set(prev);
|
|
|
|
if (newSet.has(href)) {
|
|
|
|
newSet.delete(href);
|
|
|
|
} else {
|
|
|
|
newSet.add(href);
|
|
|
|
}
|
|
|
|
return newSet;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderItems(items: SidebarNavItem[], level = 0) {
|
2025-04-12 15:04:32 -04:00
|
|
|
return items.map((item) => {
|
|
|
|
const hydratedHref = hydrateHref(item.href);
|
2025-04-12 19:50:30 -04:00
|
|
|
const isActive = pathname.startsWith(hydratedHref);
|
2025-04-12 21:35:17 -04:00
|
|
|
const hasChildren = item.children && item.children.length > 0;
|
|
|
|
const isExpanded = expandedItems.has(hydratedHref);
|
|
|
|
const indent = level * 16; // Base indent for each level
|
2025-04-12 15:04:32 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={hydratedHref}>
|
2025-04-12 21:35:17 -04:00
|
|
|
<div className="flex items-center group" style={{ marginLeft: `${indent}px` }}>
|
|
|
|
<Link
|
|
|
|
href={hydratedHref}
|
|
|
|
className={cn(
|
|
|
|
"flex items-center py-2 px-3 w-full transition-colors",
|
|
|
|
isActive
|
|
|
|
? "text-primary font-medium"
|
|
|
|
: "text-muted-foreground hover:text-foreground",
|
|
|
|
disabled && "cursor-not-allowed opacity-60"
|
|
|
|
)}
|
|
|
|
onClick={disabled ? (e) => e.preventDefault() : undefined}
|
|
|
|
tabIndex={disabled ? -1 : undefined}
|
|
|
|
aria-disabled={disabled}
|
|
|
|
>
|
|
|
|
{item.icon && <span className="mr-2">{item.icon}</span>}
|
|
|
|
{item.title}
|
|
|
|
</Link>
|
|
|
|
{hasChildren && (
|
|
|
|
<button
|
|
|
|
onClick={() => toggleItem(hydratedHref)}
|
|
|
|
className="p-2 hover:bg-muted rounded-md ml-auto"
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
|
|
|
{isExpanded ? (
|
|
|
|
<ChevronDown className="h-4 w-4" />
|
|
|
|
) : (
|
|
|
|
<ChevronRight className="h-4 w-4" />
|
|
|
|
)}
|
|
|
|
</button>
|
2025-04-12 15:04:32 -04:00
|
|
|
)}
|
2025-04-12 21:35:17 -04:00
|
|
|
</div>
|
|
|
|
{hasChildren && isExpanded && (
|
|
|
|
<div className="space-y-1 mt-1">
|
|
|
|
{renderItems(item.children || [], level + 1)}
|
2025-04-09 09:23:47 -07:00
|
|
|
</div>
|
|
|
|
)}
|
2025-04-12 15:04:32 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
2025-04-09 09:23:47 -07:00
|
|
|
}
|
|
|
|
|
2024-10-13 23:42:09 -04:00
|
|
|
return (
|
2025-04-12 15:04:32 -04:00
|
|
|
<nav
|
|
|
|
className={cn(
|
|
|
|
"flex flex-col space-y-1",
|
|
|
|
disabled && "pointer-events-none opacity-60",
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{renderItems(items)}
|
|
|
|
</nav>
|
2024-11-09 00:08:17 -05:00
|
|
|
);
|
2024-10-17 22:12:02 -04:00
|
|
|
}
|