import { forwardRef, useState } from 'react' import type { HTMLAttributes, ReactNode } from 'react' import { cn } from '@/lib/utils' import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from './dropdown-menu' import { MoreLine } from './icons-inline' export interface SidebarMenuItemAction { id: string label: string icon?: ReactNode onClick?: () => void destructive?: boolean } export interface SidebarMenuItem { id: string label: string icon?: ReactNode disabled?: boolean actions?: SidebarMenuItemAction[] } export interface SidebarMenuProps extends HTMLAttributes { items: SidebarMenuItem[] selectedId?: string onItemClick?: (item: SidebarMenuItem) => void width?: string moreIcon?: ReactNode } const moreClass = 'w-4 h-4' export const SidebarMenu = forwardRef( ( { items, selectedId, onItemClick, width = 'w-52', className, moreIcon, ...props }, ref ) => { const [openDropdownId, setOpenDropdownId] = useState(null) const baseStyles = 'flex flex-col gap-1.5 rounded-md bg-transparent' const defaultMore = moreIcon ?? return (
{items.map((item) => { const isSelected = selectedId === item.id const isDisabled = item.disabled const isOpen = openDropdownId === item.id return (
{item.actions && item.actions.length > 0 ? (
e.stopPropagation()}> { setOpenDropdownId(open ? item.id : null) }} > {item.actions.map((action) => ( { e.stopPropagation() setOpenDropdownId(null) action.onClick?.() }} > {action.icon && ( {action.icon} )} {action.label} ))}
) : ( e.stopPropagation()} > {item.icon ? ( {item.icon} ) : ( {defaultMore} )} )}
) })}
) } ) SidebarMenu.displayName = 'SidebarMenu'