"use client"; import { useState, useEffect } from "react"; import { Upload, FileText, Trash2, Download, Plus, Filter } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { fetchDocuments, deleteDocument, getDocumentDownloadUrl, } from "@/lib/supabase/documents-actions"; import PageSkeleton from "./PageSkeleton"; const CATEGORY_LABELS: Record = { contract: "Contract", tax_form: "Tax Form", id_document: "ID Document", certificate: "Certificate", policy: "Policy", general: "General", }; const CATEGORY_COLORS: Record = { contract: "bg-blue-100 text-blue-700 border-blue-200", tax_form: "bg-amber-100 text-amber-700 border-amber-200", id_document: "bg-purple-100 text-purple-700 border-purple-200", certificate: "bg-emerald-100 text-emerald-700 border-emerald-200", policy: "bg-gray-100 text-gray-700 border-gray-200", general: "bg-teal-100 text-teal-700 border-teal-200", }; function formatFileSize(bytes: number | null): string { if (!bytes) return "—"; if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; } function formatDateShort(dateStr: string): string { const d = new Date(dateStr); return d.toLocaleDateString("en-AU", { day: "numeric", month: "short", year: "numeric" }); } const CATEGORY_TABS = [ { label: "All", value: "all" }, { label: "Contracts", value: "contract" }, { label: "Tax Forms", value: "tax_form" }, { label: "ID Documents", value: "id_document" }, { label: "Certificates", value: "certificate" }, { label: "Policies", value: "policy" }, { label: "General", value: "general" }, ]; export default function DocumentsListPage({ onUpload }: { onUpload: () => void }) { const [documents, setDocuments] = useState([]); const [loading, setLoading] = useState(true); const [activeCategory, setActiveCategory] = useState("all"); const [downloadingId, setDownloadingId] = useState(null); useEffect(() => { let cancelled = false; fetchDocuments(activeCategory === "all" ? undefined : activeCategory) .then((docs) => { if (!cancelled) setDocuments(docs); }) .catch(() => {}) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [activeCategory]); const handleDelete = async (id: string) => { const result = await deleteDocument(id); if (result.success) setDocuments((prev) => prev.filter((d) => d.id !== id)); }; const handleDownload = async (id: string) => { setDownloadingId(id); const { url, error } = await getDocumentDownloadUrl(id); if (!error && url) { const a = document.createElement("a"); a.href = url; a.download = ""; document.body.appendChild(a); a.click(); document.body.removeChild(a); } setDownloadingId(null); }; if (loading) return ; return (
{/* Header */}

Documents

Store contracts, tax forms, and HR documents

{/* Category tabs */}
{CATEGORY_TABS.map((tab) => ( ))}
{/* Document list */} {documents.length === 0 ? (

No documents yet

Upload your first document to get started

) : (
{documents.map((doc) => ( ))}
Name Category Employee Size Date
{doc.name}
{CATEGORY_LABELS[doc.category] || doc.category} {doc.employee_name || "—"} {formatFileSize(doc.file_size_bytes)} {formatDateShort(doc.created_at)}
)}
); }