fosrl.pangolin/src/components/SidebarNav.tsx

176 lines
6.4 KiB
TypeScript
Raw Normal View History

"use client";
2025-04-12 21:35:17 -04:00
import React, { useState, useEffect } from "react";
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";
2025-04-16 21:37:15 -04:00
import { useUserContext } from "@app/hooks/useUserContext";
2024-10-13 16:18:54 -04:00
2025-04-12 21:35:17 -04:00
export interface SidebarNavItem {
href: string;
title: string;
icon?: React.ReactNode;
children?: SidebarNavItem[];
2025-04-12 21:35:17 -04:00
autoExpand?: boolean;
}
2025-04-12 21:35:17 -04:00
export interface SidebarNavProps extends React.HTMLAttributes<HTMLElement> {
items: SidebarNavItem[];
disabled?: boolean;
2025-04-13 14:21:18 -04:00
onItemClick?: () => void;
2024-10-13 16:18:54 -04:00
}
export function SidebarNav({
className,
items,
disabled = false,
2025-04-13 14:21:18 -04:00
onItemClick,
...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());
2025-04-16 21:37:15 -04:00
const { user } = useUserContext();
2025-04-16 20:49:06 -04:00
function hydrateHref(val: string): string {
return val
.replace("{orgId}", orgId)
.replace("{niceId}", niceId)
.replace("{resourceId}", resourceId)
.replace("{userId}", userId);
}
// Initialize expanded items based on autoExpand property and current path
2025-04-12 21:35:17 -04:00
useEffect(() => {
const autoExpanded = new Set<string>();
2025-04-16 21:37:15 -04:00
function findAutoExpandedAndActivePath(
items: SidebarNavItem[],
parentHrefs: string[] = []
) {
items.forEach((item) => {
2025-04-12 21:35:17 -04:00
const hydratedHref = hydrateHref(item.href);
2025-04-16 21:37:15 -04:00
2025-04-16 20:49:06 -04:00
// Add current item's href to the path
const currentPath = [...parentHrefs, hydratedHref];
2025-04-16 21:37:15 -04:00
2025-04-16 20:49:06 -04:00
// Auto expand if specified or if this item or any child is active
if (item.autoExpand || pathname.startsWith(hydratedHref)) {
// Expand all parent sections when a child is active
2025-04-16 21:37:15 -04:00
currentPath.forEach((href) => autoExpanded.add(href));
2025-04-12 21:35:17 -04:00
}
2025-04-16 21:37:15 -04:00
2025-04-16 20:49:06 -04:00
// Recursively check children
2025-04-12 21:35:17 -04:00
if (item.children) {
2025-04-16 20:49:06 -04:00
findAutoExpandedAndActivePath(item.children, currentPath);
2025-04-12 21:35:17 -04:00
}
});
}
2025-04-16 20:49:06 -04:00
findAutoExpandedAndActivePath(items);
2025-04-12 21:35:17 -04:00
setExpandedItems(autoExpanded);
2025-04-16 20:49:06 -04:00
}, [items, pathname]);
2025-04-12 21:35:17 -04:00
function toggleItem(href: string) {
2025-04-16 21:37:15 -04:00
setExpandedItems((prev) => {
2025-04-12 21:35:17 -04:00
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-16 21:37:15 -04:00
<div
className="flex items-center group"
style={{ marginLeft: `${indent}px` }}
>
2025-04-16 20:49:06 -04:00
<div
2025-04-12 21:35:17 -04:00
className={cn(
2025-04-16 20:49:06 -04:00
"flex items-center w-full transition-colors rounded-md",
2025-04-16 22:39:24 -04:00
isActive && level === 0 && "bg-primary/10"
2025-04-12 21:35:17 -04:00
)}
>
2025-04-16 20:49:06 -04:00
<Link
href={hydratedHref}
className={cn(
"flex items-center w-full px-3 py-2",
isActive
? "text-primary font-medium"
: "text-muted-foreground group-hover:text-foreground",
disabled && "cursor-not-allowed opacity-60"
2025-04-12 21:35:17 -04:00
)}
2025-04-16 20:49:06 -04:00
onClick={(e) => {
if (disabled) {
e.preventDefault();
} else if (onItemClick) {
onItemClick();
}
}}
tabIndex={disabled ? -1 : undefined}
aria-disabled={disabled}
>
2025-04-16 21:37:15 -04:00
{item.icon && (
<span className="mr-3 opacity-70">
{item.icon}
</span>
)}
2025-04-16 20:49:06 -04:00
{item.title}
</Link>
{hasChildren && (
<button
onClick={() => toggleItem(hydratedHref)}
className="p-2 rounded-md opacity-70 hover:opacity-100"
disabled={disabled}
>
{isExpanded ? (
<ChevronDown className="h-4 w-4" />
) : (
<ChevronRight className="h-4 w-4" />
)}
</button>
)}
</div>
2025-04-12 21:35:17 -04:00
</div>
{hasChildren && isExpanded && (
<div className="space-y-1 mt-1">
{renderItems(item.children || [], level + 1)}
</div>
)}
2025-04-12 15:04:32 -04:00
</div>
);
});
}
2024-10-13 23:42:09 -04:00
return (
2025-04-12 15:04:32 -04:00
<nav
className={cn(
2025-04-16 20:49:06 -04:00
"flex flex-col space-y-2",
2025-04-12 15:04:32 -04:00
disabled && "pointer-events-none opacity-60",
className
)}
{...props}
>
{renderItems(items)}
</nav>
);
2024-10-17 22:12:02 -04:00
}