"use client"; import { useState, useCallback } from "react"; import { IconChevronDown, IconChevronRight, IconPlus, IconTrash, IconDownload, } from "@tabler/icons-react"; import { Button } from "@kandev/ui/button"; import { Textarea } from "@kandev/ui/textarea"; import { toast } from "sonner"; import { formatRelativeTime } from "@/lib/utils"; import { listDocuments, createOrUpdateDocument, deleteDocument, type TaskDocument, } from "@/lib/api/domains/office-extended-api"; // --- Document type config --- type DocTypeConfig = { label: string; className: string; }; const DOC_TYPE_CONFIG: Record = { PLAN: { label: "PLAN", className: "bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400", }, SPEC: { label: "SPEC", className: "bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-400", }, NOTES: { label: "NOTES", className: "bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-400", }, REVIEW: { label: "REVIEW", className: "bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400", }, ATTACHMENT: { label: "ATTACH", className: "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400", }, }; const DOCUMENT_TYPES = ["PLAN", "SPEC", "NOTES", "REVIEW", "ATTACHMENT"] as const; function getTypeConfig(type: string): DocTypeConfig { return ( DOC_TYPE_CONFIG[type.toUpperCase()] ?? { label: type, className: "bg-muted text-muted-foreground", } ); } // --- New document form --- type NewDocFormState = { key: string; type: string; title: string; content: string; }; const EMPTY_FORM: NewDocFormState = { key: "", type: "NOTES", title: "", content: "", }; function NewDocumentForm({ taskId, onCreated, onCancel, }: { taskId: string; onCreated: () => void; onCancel: () => void; }) { const [form, setForm] = useState(EMPTY_FORM); const [saving, setSaving] = useState(false); const update = useCallback((patch: Partial) => { setForm((prev) => ({ ...prev, ...patch })); }, []); const handleSubmit = async () => { if (!form.key.trim() || !form.content.trim()) { toast.error("Key and content are required"); return; } setSaving(true); try { await createOrUpdateDocument(taskId, form.key.trim(), { type: form.type, title: form.title.trim() || undefined, content: form.content, }); toast.success("Document saved"); onCreated(); } catch { toast.error("Failed to save document"); } finally { setSaving(false); } }; return (
update({ key: e.target.value })} />
update({ title: e.target.value })} />