"use client"; // components/DocumentTypeManager.tsx // Modal for managing document types: rename name/description. // Located under the filters bar on the Directors/Companies tab pages. import { useState, useEffect, useRef } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { fetchDocumentTypes, updateDocumentType, deleteDocumentType, type DocType, } from "@/lib/api"; interface Props { context: "director" | "company"; open: boolean; onClose: () => void; } export default function DocumentTypeManager({ context, open, onClose }: Props) { const qc = useQueryClient(); const overlayRef = useRef(null); const [editingId, setEditingId] = useState(null); const [editName, setEditName] = useState(""); const [editDesc, setEditDesc] = useState(""); const [saving, setSaving] = useState(false); const [deleteId, setDeleteId] = useState(null); const [deleting, setDeleting] = useState(false); const [error, setError] = useState(null); const { data, isLoading } = useQuery({ queryKey: ["document-types", context], queryFn: () => fetchDocumentTypes(context), enabled: open, }); useEffect(() => { if (!open) { setEditingId(null); setDeleteId(null); setError(null); } }, [open]); if (!open) return null; const types: DocType[] = data?.items ?? []; const handleOverlayClick = (e: React.MouseEvent) => { if (e.target === overlayRef.current) onClose(); }; const startEdit = (t: DocType) => { setEditingId(t.id); setEditName(t.name); setEditDesc(t.description ?? ""); setError(null); }; const handleSaveEdit = async () => { if (!editName.trim()) return; setSaving(true); setError(null); try { await updateDocumentType(editingId!, { name: editName.trim(), description: editDesc.trim() || undefined, }); await qc.invalidateQueries({ queryKey: ["document-types", context] }); setEditingId(null); } catch (err) { setError(err instanceof Error ? err.message : "Save failed"); } finally { setSaving(false); } }; const handleDelete = async () => { if (!deleteId) return; setDeleting(true); setError(null); try { await deleteDocumentType(deleteId); await qc.invalidateQueries({ queryKey: ["document-types", context] }); setDeleteId(null); } catch (err) { setError(err instanceof Error ? err.message : "Delete failed"); } finally { setDeleting(false); } }; return (
{/* Header */}

Document types

Rename or delete types. Docs using a deleted type will show the old type name.

{/* Body */}
{error && (
{error}
)} {isLoading ? (

Loading…

) : types.length === 0 ? (

No {context} document types yet. Create one when you upload a document.

) : (
    {types.map((t) => (
  • {editingId === t.id ? ( /* Edit form */
    setEditName(e.target.value)} className="w-full rounded border border-border bg-surface-warm px-3 py-2 text-sm text-fg focus:outline-none focus:border-accent" placeholder="Type name" required /> setEditDesc(e.target.value)} className="w-full rounded border border-border bg-surface-warm px-3 py-2 text-sm text-fg placeholder:text-muted focus:outline-none focus:border-accent" placeholder="Description (optional)" />
    ) : deleteId === t.id ? ( /* Delete confirm */

    Delete type "{t.name}"? Existing documents will keep this name.

    ) : ( /* Display row */

    {t.name}

    {t.description ? (

    {t.description}

    ) : null}
    )}
  • ))}
)}
{/* Footer */}
); }