fosrl.pangolin/src/components/SidebarNav.tsx

227 lines
8.8 KiB
TypeScript
Raw Normal View History

"use client";
import React, { useEffect } from "react";
import Link from "next/link";
import { useParams, usePathname, useRouter } from "next/navigation";
2025-01-01 21:41:31 -05:00
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";
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
const [selectedValue, setSelectedValue] =
React.useState<string>(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)
2024-11-09 23:59:19 -05:00
.replace("{resourceId}", resourceId)
.replace("{userId}", userId);
}
function renderItems(items: SidebarNavItem[]) {
return items.map((item) => (
<div key={hydrateHref(item.href)}>
<Link
href={hydrateHref(item.href)}
className={cn(
2025-04-12 12:24:16 -04:00
"w-full",
buttonVariants({ variant: "ghost" }),
pathname === hydrateHref(item.href) &&
!pathname.includes("create")
? "bg-accent hover:bg-accent dark:bg-border dark:hover:bg-border"
: "hover:bg-transparent hover:underline",
"justify-start",
disabled && "cursor-not-allowed"
)}
onClick={disabled ? (e) => e.preventDefault() : undefined}
tabIndex={disabled ? -1 : undefined}
aria-disabled={disabled}
>
{item.icon ? (
<div className="flex items-center space-x-2">
{item.icon}
<span>{item.title}</span>
</div>
) : (
item.title
)}
</Link>
{item.children && (
<div className="ml-4 space-y-2">
{item.children.map((child) => (
<div
key={hydrateHref(child.href)}
className="flex items-center space-x-2"
>
<Link
href={hydrateHref(child.href)}
className={cn(
2025-04-12 12:24:16 -04:00
"w-full",
buttonVariants({ variant: "ghost" }),
pathname === hydrateHref(child.href) &&
!pathname.includes("create")
? "bg-accent hover:bg-accent dark:bg-border dark:hover:bg-border"
: "hover:bg-transparent hover:underline",
"justify-start",
disabled && "cursor-not-allowed"
)}
onClick={
disabled
? (e) => e.preventDefault()
: undefined
}
tabIndex={disabled ? -1 : undefined}
aria-disabled={disabled}
>
2025-04-12 12:24:16 -04:00
<CornerDownRight className="h-4 w-4 text-gray-500 mr-2" />
{child.icon ? (
<div className="flex items-center space-x-2">
{child.icon}
<span>{child.title}</span>
</div>
) : (
child.title
)}
</Link>
</div>
))}
</div>
)}
</div>
));
}
2024-10-13 23:42:09 -04:00
return (
<div>
<div className="block lg:hidden">
<Select
defaultValue={selectedValue}
value={selectedValue}
onValueChange={handleSelectChange}
disabled={disabled}
2024-10-13 23:42:09 -04:00
>
<SelectTrigger>
<SelectValue placeholder="Select an option" />
</SelectTrigger>
<SelectContent>
{items.flatMap((item) => {
const topLevelItem = (
<SelectItem
key={hydrateHref(item.href)}
value={hydrateHref(item.href)}
>
{item.icon ? (
<div className="flex items-center space-x-2">
{item.icon}
<span>{item.title}</span>
</div>
) : (
item.title
)}
</SelectItem>
);
const childItems =
item.children?.map((child) => (
<SelectItem
key={hydrateHref(child.href)}
value={hydrateHref(child.href)}
className="pl-8"
>
<div className="flex items-center space-x-2">
<CornerDownRight className="h-4 w-4 text-gray-500" />
{child.icon ? (
<>
{child.icon}
<span>{child.title}</span>
</>
) : (
<span>{child.title}</span>
)}
</div>
</SelectItem>
)) || [];
return [topLevelItem, ...childItems];
})}
</SelectContent>
</Select>
</div>
<nav
className={cn(
2025-01-04 20:22:01 -05:00
"hidden lg:flex space-x-2 lg:flex-col lg:space-x-0 lg:space-y-3 pr-8",
disabled && "opacity-50 pointer-events-none",
className
)}
{...props}
>
{renderItems(items)}
</nav>
</div>
);
2024-10-17 22:12:02 -04:00
}