"use client"; import { useState, useRef, useEffect } from "react"; import { ArrowLeft, Upload, Loader2, FileText } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Card, CardContent } from "@/components/ui/card"; import { uploadDocument } from "@/lib/supabase/documents-actions"; import { fetchEmployees } from "@/lib/supabase/employees-actions"; const CATEGORY_LABELS: Record = { contract: "Contract", tax_form: "Tax Form", id_document: "ID Document", certificate: "Certificate", policy: "Policy", general: "General", }; export default function DocumentsUploadPage({ onBack, onSuccess, }: { onBack: () => void; onSuccess: () => void; }) { const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [employees, setEmployees] = useState([]); const fileInputRef = useRef(null); const [name, setName] = useState(""); const [category, setCategory] = useState("general"); const [employeeId, setEmployeeId] = useState(""); const [file, setFile] = useState(null); const [dragOver, setDragOver] = useState(false); useEffect(() => { fetchEmployees().then(setEmployees).catch(() => {}); }, []); const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setDragOver(false); if (e.dataTransfer.files.length > 0) { setFile(e.dataTransfer.files[0]); if (!name) setName(e.dataTransfer.files[0].name); } }; const handleSubmit = async () => { setError(null); if (!file) { setError("Select a file to upload"); return; } if (!name.trim()) { setError("Enter a document name"); return; } setSaving(true); try { const result = await uploadDocument({ name: name.trim(), category, employee_id: employeeId || null, file, }); if (!result.success) { setError(result.error || "Upload failed"); return; } onSuccess(); } catch (e: any) { setError(e.message || "An unexpected error occurred"); } finally { setSaving(false); } }; return (

Upload Document

Add a document to your organisation's library

{error && (
{error}
)} {/* File drop zone */}
{ e.preventDefault(); setDragOver(true); }} onDragLeave={() => setDragOver(false)} onDrop={handleDrop} onClick={() => fileInputRef.current?.click()} className={`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${ dragOver ? "border-[#1D9E75] bg-teal-50" : "border-gray-200 hover:border-gray-300" }`} > {file ? (

{file.name}

{(file.size / 1024).toFixed(1)} KB

Click to change file

) : (

Drag & drop a file here, or click to browse

Max 50 MB

)} { if (e.target.files?.[0]) { setFile(e.target.files[0]); if (!name) setName(e.target.files[0].name); } }} className="hidden" />
setName(e.target.value)} className="mt-1" placeholder="e.g. Employment Contract — John Smith" />
); }