"use client";
import { ArrowUpRight, ChevronDown } from "lucide-react";
import Link from "next/link";
import { useState } from "react";
import { Badge } from "@/components/shadcn/badge/badge";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/shadcn/collapsible";
import { NavigationButton } from "@/components/shadcn/navigation-button";
import { ScrollArea } from "@/components/shadcn/scroll-area/scroll-area";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/shadcn/tooltip";
import { cn } from "@/lib/utils";
import { useCloudUpgradeStore } from "@/store";
import {
type AppSidebarSelectionHandler,
NAVIGATION_ITEM_KIND,
type NavigationChild,
type NavigationChildLink,
type NavigationCloudUpgrade,
type NavigationCollapsible,
type NavigationLink,
type NavigationSection,
} from "./types";
interface SidebarNavigationProps {
sections: NavigationSection[];
onSelect?: AppSidebarSelectionHandler;
}
interface NavigationLinkProps {
item: NavigationLink;
onSelect?: AppSidebarSelectionHandler;
}
interface NavigationCollapsibleProps {
item: NavigationCollapsible;
onSelect?: AppSidebarSelectionHandler;
}
interface NavigationChildProps {
item: NavigationChild;
onSelect?: AppSidebarSelectionHandler;
}
function getCollapsibleActivationKey(item: NavigationCollapsible) {
const activeChild = item.children.find(
(child) =>
child.kind === NAVIGATION_ITEM_KIND.LINK && child.active === true,
);
return `${item.label}-${activeChild?.label ?? "inactive"}`;
}
function TopLevelLink({ item, onSelect }: NavigationLinkProps) {
const Icon = item.icon;
const isExternal = item.target === "_blank";
const link = (
{item.active && (
)}
{item.label}
{item.highlight && (
New
)}
{isExternal && }
);
if (!item.tooltip) return link;
return (
{link}
{item.tooltip}
);
}
function CloudUpgradeChild({
item,
onSelect,
}: {
item: NavigationCloudUpgrade;
onSelect?: AppSidebarSelectionHandler;
}) {
const openCloudUpgrade = useCloudUpgradeStore(
(state) => state.openCloudUpgrade,
);
return (
{
openCloudUpgrade(item.cloudUpgradeFeature, onSelect?.() ?? undefined);
}}
>
{item.label}
Cloud
);
}
function LinkChild({
item,
onSelect,
}: {
item: NavigationChildLink;
onSelect?: AppSidebarSelectionHandler;
}) {
const isExternal = item.target === "_blank";
if (item.disabled) {
return (
{item.label}
);
}
return (
{item.label}
{item.highlight && (
New
)}
{isExternal && }
);
}
function NavigationChildItem({ item, onSelect }: NavigationChildProps) {
if (item.kind === NAVIGATION_ITEM_KIND.CLOUD_UPGRADE) {
return ;
}
return ;
}
function CollapsibleNavigationItem({
item,
onSelect,
}: NavigationCollapsibleProps) {
const hasActiveChild = item.children.some(
(child) =>
child.kind === NAVIGATION_ITEM_KIND.LINK && child.active === true,
);
const [expanded, setExpanded] = useState(item.defaultOpen || hasActiveChild);
const isOpen = expanded;
const Icon = item.icon;
return (
{hasActiveChild && (
)}
{item.label}
{item.children.map((child) => (
-
))}
);
}
function NavigationSectionList({
section,
onSelect,
}: {
section: NavigationSection;
onSelect?: AppSidebarSelectionHandler;
}) {
return (
{section.label && (
)}
{section.items.map((item) => (
-
{item.kind === NAVIGATION_ITEM_KIND.COLLAPSIBLE ? (
) : (
)}
))}
);
}
export function SidebarNavigation({
sections,
onSelect,
}: SidebarNavigationProps) {
return (
);
}