fosrl.pangolin/src/components/SidebarNav.tsx

112 lines
4.2 KiB
TypeScript
Raw Normal View History

"use client";
2025-04-12 15:04:32 -04:00
import React 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";
import { CornerDownRight } from "lucide-react";
2024-10-13 16:18:54 -04:00
interface SidebarNavItem {
href: string;
title: string;
icon?: React.ReactNode;
children?: SidebarNavItem[];
}
2024-10-13 16:18:54 -04:00
interface SidebarNavProps extends React.HTMLAttributes<HTMLElement> {
items: SidebarNavItem[];
disabled?: boolean;
2024-10-13 16:18:54 -04: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;
2024-10-13 16:18:54 -04: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);
}
function renderItems(items: SidebarNavItem[]) {
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 15:04:32 -04:00
return (
<div key={hydratedHref}>
<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>
{item.children && (
<div className="ml-4 space-y-1 mt-1">
{item.children.map((child) => {
const hydratedChildHref = hydrateHref(child.href);
const isChildActive = pathname.startsWith(hydratedChildHref) && !pathname.includes("create");
return (
<Link
key={hydratedChildHref}
href={hydratedChildHref}
className={cn(
"flex items-center text-sm py-2 px-3 w-full transition-colors",
isChildActive
? "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}
>
<CornerDownRight className="h-4 w-4 text-muted-foreground/70 mr-2" />
{child.icon && <span className="mr-2">{child.icon}</span>}
{child.title}
</Link>
);
})}
</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(
"flex flex-col space-y-1",
disabled && "pointer-events-none opacity-60",
className
)}
{...props}
>
{renderItems(items)}
</nav>
);
2024-10-17 22:12:02 -04:00
}