"use client" import { useRef, useState } from "react" import { useRouter } from "next/navigation" import { toast } from "sonner" import { FileText, Upload, ExternalLink, Loader2 } from "lucide-react" import { setLeaseDocument } from "@/app/actions/esign" export function LeaseDocument({ leaseId, documentUrl, canWrite, }: { leaseId: string documentUrl: string | null canWrite: boolean }) { const router = useRouter() const inputRef = useRef(null) const [uploading, setUploading] = useState(false) async function onPick(e: React.ChangeEvent) { const file = e.target.files?.[0] if (!file) return if (!/\.(pdf|docx?)$/i.test(file.name)) { toast.error("Upload a PDF or Word document") if (inputRef.current) inputRef.current.value = "" return } setUploading(true) try { const fd = new FormData() fd.append("file", file) fd.append("scope", "documents") const res = await fetch("/api/upload", { method: "POST", body: fd }) if (!res.ok) { const j = await res.json().catch(() => ({})) throw new Error(j?.error || "Upload failed") } const { url } = (await res.json()) as { url: string } await setLeaseDocument(leaseId, url) toast.success(documentUrl ? "Lease document replaced" : "Lease document attached") router.refresh() } catch (err) { toast.error(err instanceof Error ? err.message : "Upload failed") } finally { setUploading(false) if (inputRef.current) inputRef.current.value = "" } } return (
{documentUrl ? "Lease document" : "No lease document yet"}
{documentUrl && ( View )}
{!documentUrl && (

Upload the lease PDF to enable sending it for e-signature.

)} {canWrite && (
)}
) }