fosrl.pangolin/src/components/SidebarNav.tsx

189 lines
7.1 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";
2025-04-23 16:18:51 -04:00
import { Badge } from "@app/components/ui/badge";
2025-04-27 13:03:00 -04:00
import { useLicenseStatusContext } from "@app/hooks/useLicenseStatusContext";
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-25 17:13:20 -04:00
showProfessional?: 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-30 22:56:10 -04:00
const [expandedItems, setExpandedItems] = useState<Set<string>>(() => {
2025-04-12 21:35:17 -04:00
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 20:49:06 -04:00
const currentPath = [...parentHrefs, hydratedHref];
2025-04-16 21:37:15 -04:00
2025-04-16 20:49:06 -04:00
if (item.autoExpand || pathname.startsWith(hydratedHref)) {
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-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-30 22:56:10 -04:00
return autoExpanded;
});
const { licenseStatus, isUnlocked } = useLicenseStatusContext();
const { user } = useUserContext();
function hydrateHref(val: string): string {
return val
.replace("{orgId}", orgId)
.replace("{niceId}", niceId)
.replace("{resourceId}", resourceId)
.replace("{userId}", userId);
}
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);
2025-04-23 16:18:51 -04:00
const indent = level * 28; // Base indent for each level
2025-04-27 13:03:00 -04:00
const isProfessional = item.showProfessional && !isUnlocked();
2025-04-25 17:13:20 -04:00
const isDisabled = disabled || isProfessional;
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
2025-04-25 17:13:20 -04:00
href={isProfessional ? "#" : hydratedHref}
2025-04-16 20:49:06 -04:00
className={cn(
"flex items-center w-full px-3 py-2",
isActive
? "text-primary font-medium"
: "text-muted-foreground group-hover:text-foreground",
2025-04-23 16:18:51 -04:00
isDisabled && "cursor-not-allowed"
2025-04-12 21:35:17 -04:00
)}
2025-04-16 20:49:06 -04:00
onClick={(e) => {
2025-04-23 16:18:51 -04:00
if (isDisabled) {
2025-04-16 20:49:06 -04:00
e.preventDefault();
} else if (onItemClick) {
onItemClick();
}
}}
2025-04-23 16:18:51 -04:00
tabIndex={isDisabled ? -1 : undefined}
aria-disabled={isDisabled}
2025-04-16 20:49:06 -04:00
>
2025-04-25 17:13:20 -04:00
<div
className={cn(
"flex items-center",
isDisabled && "opacity-60"
)}
>
2025-04-23 16:18:51 -04:00
{item.icon && (
2025-04-25 17:13:20 -04:00
<span className="mr-3">
{item.icon}
</span>
2025-04-23 16:18:51 -04:00
)}
{item.title}
</div>
2025-04-25 17:13:20 -04:00
{isProfessional && (
<Badge
variant="outlinePrimary"
className="ml-2"
>
Professional
2025-04-23 16:18:51 -04:00
</Badge>
2025-04-16 21:37:15 -04:00
)}
2025-04-16 20:49:06 -04:00
</Link>
{hasChildren && (
<button
onClick={() => toggleItem(hydratedHref)}
2025-04-18 11:36:34 -04:00
className="p-2 rounded-md text-muted-foreground hover:text-foreground cursor-pointer"
2025-04-23 16:18:51 -04:00
disabled={isDisabled}
2025-04-16 20:49:06 -04:00
>
{isExpanded ? (
2025-04-18 11:36:34 -04:00
<ChevronDown className="h-5 w-5" />
2025-04-16 20:49:06 -04:00
) : (
2025-04-18 11:36:34 -04:00
<ChevronRight className="h-5 w-5" />
2025-04-16 20:49:06 -04:00
)}
</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
}