2024-11-09 00:08:17 -05:00
|
|
|
"use client";
|
|
|
|
|
2024-12-29 22:07:12 -05:00
|
|
|
import React, { useEffect } from "react";
|
2024-11-09 00:08:17 -05:00
|
|
|
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";
|
2024-11-09 00:08:17 -05:00
|
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
SelectContent,
|
|
|
|
SelectItem,
|
|
|
|
SelectTrigger,
|
2025-04-09 09:23:47 -07:00
|
|
|
SelectValue
|
2024-11-09 00:08:17 -05:00
|
|
|
} from "@/components/ui/select";
|
2025-04-09 20:32:21 -07:00
|
|
|
import { CornerDownRight } from "lucide-react";
|
2024-10-13 16:18:54 -04:00
|
|
|
|
2025-04-09 09:23:47 -07: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> {
|
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;
|
2024-10-13 16:18:54 -04:00
|
|
|
|
2025-04-09 09:23:47 -07:00
|
|
|
const [selectedValue, setSelectedValue] =
|
|
|
|
React.useState<string>(getSelectedValue());
|
2024-12-29 22:07:12 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSelectedValue(getSelectedValue());
|
|
|
|
}, [usePathname()]);
|
|
|
|
|
2024-11-09 00:08:17 -05:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const handleSelectChange = (value: string) => {
|
|
|
|
if (!disabled) {
|
|
|
|
router.push(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function getSelectedValue() {
|
2025-04-10 07:43:51 -07:00
|
|
|
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;
|
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-09 09:23:47 -07:00
|
|
|
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",
|
2025-04-09 09:23:47 -07:00
|
|
|
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">
|
2025-04-09 20:32:21 -07:00
|
|
|
{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",
|
2025-04-09 20:32:21 -07:00
|
|
|
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" />
|
2025-04-09 20:32:21 -07:00
|
|
|
{child.icon ? (
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
{child.icon}
|
|
|
|
<span>{child.title}</span>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
child.title
|
|
|
|
)}
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
))}
|
2025-04-09 09:23:47 -07:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-10-13 23:42:09 -04:00
|
|
|
return (
|
2024-11-09 00:08:17 -05:00
|
|
|
<div>
|
2024-12-29 22:07:12 -05:00
|
|
|
<div className="block lg:hidden">
|
2024-11-09 00:08:17 -05:00
|
|
|
<Select
|
2024-12-29 22:07:12 -05:00
|
|
|
defaultValue={selectedValue}
|
|
|
|
value={selectedValue}
|
2024-11-09 00:08:17 -05:00
|
|
|
onValueChange={handleSelectChange}
|
|
|
|
disabled={disabled}
|
2024-10-13 23:42:09 -04:00
|
|
|
>
|
2024-11-09 00:08:17 -05:00
|
|
|
<SelectTrigger>
|
|
|
|
<SelectValue placeholder="Select an option" />
|
|
|
|
</SelectTrigger>
|
|
|
|
<SelectContent>
|
2025-04-10 07:43:51 -07:00
|
|
|
{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];
|
|
|
|
})}
|
2024-11-09 00:08:17 -05:00
|
|
|
</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",
|
2024-11-09 00:08:17 -05:00
|
|
|
disabled && "opacity-50 pointer-events-none",
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
>
|
2025-04-09 09:23:47 -07:00
|
|
|
{renderItems(items)}
|
2024-11-09 00:08:17 -05:00
|
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
);
|
2024-10-17 22:12:02 -04:00
|
|
|
}
|