"use client"; import { useEffect, useRef, useState } from "react"; import { Upload } from "lucide-react"; import { listDocumentVersions } from "@/app/lib/mikeApi"; import type { Document } from "./types"; import { Modal } from "./Modal"; interface Props { open: boolean; onClose: () => void; doc: Document | null; onSubmit: (file: File, filename: string) => Promise; } export function UploadNewVersionModal({ open, onClose, doc, onSubmit }: Props) { const [name, setName] = useState(""); const [stagedFile, setStagedFile] = useState(null); const [submitting, setSubmitting] = useState(false); const [currentVersion, setCurrentVersion] = useState(null); const fileInputRef = useRef(null); useEffect(() => { if (!open || !doc) return; setName(doc.filename); setStagedFile(null); setSubmitting(false); setCurrentVersion(null); let cancelled = false; (async () => { try { const { current_version_id, versions } = await listDocumentVersions(doc.id); const current = versions.find( (v) => v.id === current_version_id, ); const initial = (current?.filename && current.filename.trim()) || doc.filename; if (!cancelled) { setName(initial); setCurrentVersion(current?.version_number ?? null); } } catch { /* keep fallback */ } })(); return () => { cancelled = true; }; }, [open, doc]); if (!open || !doc) return null; const accept = ".pdf,.docx,.doc"; function handleFilePick(e: React.ChangeEvent) { const file = e.target.files?.[0] ?? null; setStagedFile(file); if (fileInputRef.current) fileInputRef.current.value = ""; } async function handleSubmit() { if (!stagedFile || submitting || !doc) return; const finalName = name.trim() || doc.filename; setSubmitting(true); try { await onSubmit(stagedFile, finalName); onClose(); } finally { setSubmitting(false); } } return ( , onClick: () => fileInputRef.current?.click(), disabled: submitting, }} primaryAction={{ label: submitting ? "Saving…" : "Save", onClick: handleSubmit, disabled: !stagedFile || submitting, }} > setName(e.target.value)} placeholder="Version name" className="w-full rounded-lg border border-gray-200 px-3 py-2 text-sm outline-none focus:border-gray-400" />
Current Version:{" "} {currentVersion ?? "—"}
{stagedFile && (
New Version File:{" "} {stagedFile.name}
)}
); }