"use client"; import { useState } from "react"; import { ChevronDown } from "lucide-react"; import { SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarMenu, SidebarMenuButton, SidebarMenuItem, } from "../ui/sidebar"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "../ui/collapsible"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger, } from "../ui/context-menu"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, } from "../ui/dialog"; import { Input } from "../ui/input"; import { Button } from "../ui/button"; import { Route, Workspace, LayoutNode } from "@/lib/types"; type DialogMode = "rename" | "add-item" | "add-group"; type Props = { node: LayoutNode; workspace: Workspace; setRoute: (route: Route) => void; addEntity: ( type: "todo" | "doc", parentGroupId: string | null, name?: string, ) => string | undefined; addGroup: (name: string, parentGroupId: string | null) => string; renameEntity: (entityId: string, newName: string) => void; deleteEntity: (entityId: string) => void; removeNode: (id: string) => void; renameGroup: (id: string, name: string) => void; }; export default function EntitySwitch(props: Props) { const { node, workspace, setRoute, addEntity, addGroup, renameEntity, deleteEntity, removeNode, renameGroup, } = props; const [dialogValue, setDialogValue] = useState(""); const [dialogMode, setDialogMode] = useState(); const parentId = node.type === "group" ? node.id : null; const openDialog = (mode: DialogMode, initial = "") => { setDialogMode(mode); setDialogValue( initial || (mode === "add-item" ? "New list" : mode === "add-group" ? "New group" : ""), ); }; const closeDialog = () => setDialogMode(undefined); const handleSubmit = () => { const value = dialogValue.trim(); if (!value) return; switch (dialogMode) { case "rename": if (node.type === "group") renameGroup(node.id, value); else if (node.type === "item") renameEntity(node.entityId, value); break; case "add-item": addEntity("todo", parentId, value); break; case "add-group": addGroup(value, parentId); break; } closeDialog(); }; const confirmDelete = (message: string, action: () => void) => { if (window.confirm(message)) action(); }; const dialogTitle = dialogMode?.startsWith("rename") ? "Rename" : dialogMode === "add-item" ? "Add new list" : "Add new group"; const dialog = ( !o && closeDialog()}> {dialogTitle} {(dialogMode === "add-item" || dialogMode === "add-group") && ( Enter a name below. )} setDialogValue(e.target.value)} /> ); /* ================= GROUP ================= */ if (node.type === "group") { return ( <> {node.name} openDialog("add-item")}> Add list openDialog("add-group")}> Add group openDialog("rename", node.name)} > Rename confirmDelete("Delete this group and its children?", () => removeNode(node.id), ) } > Delete {node.children.map((child) => ( ))} {dialog} ); } /* ================= ITEM ================= */ const entity = workspace.entities[node.entityId]; if (!entity) return null; return ( <> setRoute({ workspace: workspace.id, entity: node.entityId, }) } > {entity.name} openDialog("rename", entity.name)}> Rename confirmDelete("Delete this list?", () => deleteEntity(node.entityId), ) } > Delete {dialog} ); }