import { MoreHorizontal, Pencil, Trash2 } from "lucide-react"; import { useEffect, useState } from "react"; import { DeleteSpaceDialog } from "@/components/delete-space-dialog"; import { EditSpaceDialog } from "@/components/edit-space-dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/base-dropdown-menu"; import { Button } from "@/components/ui/button"; import type { ColorValue } from "@/components/ui/color-picker"; import { deleteSpace, updateSpace } from "@/lib/appwrite-db"; export type SpaceActionsProps = { space: { id: string; name: string; color: string }; userId?: string; onUpdated?: () => void; onDeleted?: () => void; }; export function SpaceActions({ space, userId, onUpdated, onDeleted, }: SpaceActionsProps) { const [openEdit, setOpenEdit] = useState(false); const [openDelete, setOpenDelete] = useState(false); const [title, setTitle] = useState(space.name); const [color, setColor] = useState(normalizeColor(space.color)); const [isSaving, setIsSaving] = useState(false); const [isDeleting, setIsDeleting] = useState(false); useEffect(() => { setTitle(space.name); setColor(normalizeColor(space.color)); }, [space.name, space.color]); const handleSave = async () => { if (!userId) { return; } if (!title.trim()) { return; } setIsSaving(true); try { await updateSpace({ spaceId: space.id, title: title.trim(), color: color as string, userId, }); setOpenEdit(false); onUpdated?.(); } catch { // TODO: add toast } finally { setIsSaving(false); } }; const handleDelete = async () => { if (!userId) { return; } setIsDeleting(true); try { await deleteSpace({ spaceId: space.id, userId }); setOpenDelete(false); onDeleted?.(); } catch { // TODO: add toast } finally { setIsDeleting(false); } }; return ( { e.preventDefault(); e.stopPropagation(); }} onKeyDown={(e) => { // Prevent bubbling to card e.stopPropagation(); }} > { e.preventDefault(); e.stopPropagation(); setOpenEdit(true); }} > Edit { e.preventDefault(); e.stopPropagation(); setOpenDelete(true); }} variant="destructive" > Delete ); } // Color helpers constrained to the allowed palette from ColorPicker const allowedColors = new Set([ "#3b82f6", "#22c55e", "#a855f7", "#f59e0b", "#ec4899", "#06b6d4", "#ef4444", "#10b981", ]); function normalizeColor(value?: string): ColorValue { if (value && (allowedColors as Set).has(value)) { return value as ColorValue; } return "#3b82f6"; }