"use client"; import type { HTMLAttributes } from "react"; import { useDroppable } from "@dnd-kit/core"; import { SortableContext, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { IconChevronDown, IconChevronUp, IconGripVertical, IconPlus, IconTrash, IconX, } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Input } from "@kandev/ui/input"; import { Switch } from "@kandev/ui/switch"; import type { SidebarLayoutNode } from "@/lib/sidebar/layout-types"; import type { ProjectedSidebarNode, ProjectedShortcut } from "@/lib/sidebar/layout-projection"; import type { ShortcutCatalogEntry } from "@/lib/sidebar/shortcut-catalog"; import { SidebarShortcutPicker, ShortcutSectionMoveMenu, } from "./sidebar-layout-editor-shortcut-picker"; import { groupDropId, nodeDragId, shortcutDragId, type SidebarLayoutDragData, } from "./sidebar-layout-editor-dnd"; const SECTION_NAME_LABEL_KEY = "settings:sectionName"; const BUILTIN_LABEL_KEYS: Record = { home: "sidebar:home", new_task: "sidebar:newTask", automations: "common:automations", canvases: "canvases:canvases", integrations: "common:integrations", }; function nodeTitle( node: SidebarLayoutNode, projected: ProjectedSidebarNode | undefined, t: (key: string) => string, ): string { if (projected?.available === false) return projected.label; if (node.name) return node.name; return node.destinationId && BUILTIN_LABEL_KEYS[node.destinationId] ? t(BUILTIN_LABEL_KEYS[node.destinationId]) : node.id; } function handleProps( attributes: object, listeners: object | undefined, ): HTMLAttributes { return { ...attributes, ...(listeners ?? {}) } as HTMLAttributes; } type NodeEditorProps = { node: SidebarLayoutNode; projected?: ProjectedSidebarNode; sections: SidebarLayoutNode[]; index: number; total: number; catalog: ShortcutCatalogEntry[]; loading: boolean; catalogError: string | null; canvasError?: string | null; readOnly: boolean; pickerOpen: boolean; query: string; onQueryChange: (value: string) => void; onToggle: () => void; onMove: (position: number) => void; onRemove: () => void; onSetNameDraft: (nodeId: string, name: string) => void; onCommitName: (nodeId: string, name: string) => void; onOpenPicker: () => void; onClosePicker: () => void; onAdd: (entry: ShortcutCatalogEntry) => void; onRemoveShortcut: (shortcutId: string) => void; onMoveShortcut: (shortcutId: string, position: number) => void; onMoveShortcutToSection: (shortcutId: string, destinationNodeId: string) => void; onFocus: () => void; t: (key: string, options?: Record) => string; }; export function SidebarLayoutNodeEditor(props: NodeEditorProps) { const { node } = props; const sortable = useSortable({ id: nodeDragId(node.id), disabled: props.readOnly, data: { type: "node", nodeId: node.id } satisfies SidebarLayoutDragData, }); const style = { transform: CSS.Transform.toString(sortable.transform), transition: sortable.transition, }; return (
); } function SidebarLayoutNodeHeader({ node, projected, index, total, onToggle, onMove, onRemove, onSetNameDraft, onCommitName, dragHandleProps, readOnly, t, }: NodeEditorProps & { dragHandleProps: HTMLAttributes }) { const isGroup = node.kind === "shortcuts"; return (
{isGroup ? ( onSetNameDraft(node.id, event.target.value)} onBlur={() => onCommitName(node.id, node.name ?? "")} disabled={readOnly} className="min-h-7 min-w-0 flex-1 max-md:min-h-11 [@media(pointer:coarse)]:min-h-11" /> ) : ( {nodeTitle(node, projected, t)} )} {isGroup && ( )}
); } function SidebarLayoutShortcutList({ node, projected, sections, catalog, loading, catalogError, canvasError, readOnly, pickerOpen, query, onQueryChange, onOpenPicker, onClosePicker, onAdd, onRemoveShortcut, onMoveShortcut, onMoveShortcutToSection, onFocus, t, }: NodeEditorProps) { const { setNodeRef } = useDroppable({ id: groupDropId(node.id), disabled: readOnly, data: { type: "group", nodeId: node.id } satisfies SidebarLayoutDragData, }); if (node.kind !== "shortcuts") return null; const shortcuts = projected?.shortcuts ?? []; return (
shortcutDragId(node.id, shortcut.id))} strategy={verticalListSortingStrategy} > {shortcuts.map((shortcut, shortcutIndex) => ( ))}
{pickerOpen && !readOnly && ( `${shortcut.target.kind}:${shortcut.target.id}`)} t={t} /> )}
); } function SortableSidebarShortcut({ nodeId, shortcut, readOnly, shortcutIndex, shortcutCount, sections, t, onMoveShortcut, onRemoveShortcut, onMoveShortcutToSection, }: { nodeId: string; shortcut: ProjectedShortcut; readOnly: boolean; shortcutIndex: number; shortcutCount: number; sections: SidebarLayoutNode[]; t: (key: string, options?: Record) => string; onMoveShortcut: (shortcutId: string, position: number) => void; onRemoveShortcut: (shortcutId: string) => void; onMoveShortcutToSection: (shortcutId: string, destinationNodeId: string) => void; }) { const sortable = useSortable({ id: shortcutDragId(nodeId, shortcut.id), disabled: readOnly, data: { type: "shortcut", nodeId, shortcutId: shortcut.id } satisfies SidebarLayoutDragData, }); const Icon = shortcut.icon; return (
); }