import { useId, useState } from "react"; import { useLocation } from "wouter"; import { z } from "zod"; import Alert from "@mui/material/Alert"; import Button from "@mui/material/Button"; import { ApiNotice } from "@/components/ApiNotice"; import { WorkspaceShell } from "@/components/WorkspaceShell"; import { ActionForm, Field, Pager, Panel, QueryView, Table, WorkspaceLink, readText, usePrivateQuery } from "@/components/WorkspaceTools"; import { isUnknownOutcome } from "@/lib/api"; import { uuid } from "@/lib/commerce"; import { usePlatform } from "@/lib/platform"; import { documentDecision, documentDetails, documentInput, documentVersion, prepareDocumentUpload, supplierDocument } from "@/lib/supplier-documents"; const kindOptions = [ { value: "REGISTRATION", label: "Registration" }, { value: "TAX_CERTIFICATE", label: "Tax certificate" }, { value: "COMPLIANCE_CERTIFICATE", label: "Compliance certificate" }, { value: "OTHER", label: "Other" }, ]; type Version = z.infer; function FindSupplier() { const { can } = usePlatform(); const [, navigate] = useLocation(); return {can("procurement.read") &&

Choose a supplier in the supplier directory.

} ({ supplierId: readText(data, "supplierId").trim() })} submit={async (input) => { const { supplierId } = z.object({ supplierId: uuid }).parse(input); navigate(`/admin/supplier-documents/${supplierId}`); }}>
; } function SupplierDocumentList({ id }: { id: string }) { const { can, request } = usePlatform(); const [offset, setOffset] = useState(0); const [selected, select] = useState(""); const path = `/suppliers/${id}/documents`; const query = usePrivateQuery(`${path}?limit=25&offset=${offset}`, z.array(supplierDocument)); return <>

Supplier ID {id}

Open another supplier {(rows) => <> row.id)} rows={rows.map((row) => [ row.code, row.title, kindOptions.find((kind) => kind.value === row.kind)?.label ?? row.kind, , ])} /> {can("supplier_document.manage") && documentInput.parse(Object.fromEntries(data))} submit={async (body) => { try { const result = await request(path, supplierDocument, { method: "POST", body }); select(result.id); setOffset(0); } finally { await query.refetch(); } }}> } } {selected && select("")} />} ; } function DocumentVersions({ id, close }: { id: string; close: () => void }) { const { can, request } = usePlatform(); const [versionId, select] = useState(""); const [uncertain, setUncertain] = useState(false); const fileId = useId(); const path = `/supplier-documents/${id}`; const query = usePrivateQuery(path, documentDetails); return <> {(record) => { const version = record.versions.find((entry) => entry.id === versionId); return
entry.id)} rows={record.versions.map((entry) => [ entry.version, entry.filename, new Date(entry.createdAt).toLocaleString(), entry.decision?.outcome ?? "Pending", , ])} /> {version && } {uncertain ? The server may have saved the PDF. Review the version list, then close and reopen the document before uploading again. : can("supplier_document.manage") && (record.versions.length < 20 ? ( prepareDocumentUpload(data.get("file"))} submit={async (body) => { try { const saved = await request(`${path}/versions`, documentVersion, { method: "POST", body }); select(saved.id); } catch (error) { if (isUnknownOutcome(error)) setUncertain(true); throw error; } finally { await query.refetch(); } }}>

Maximum 2 MiB. Use a simple .pdf filename.

) : This document has reached the 20-version limit.)} ; }} ; } function VersionActions({ version, refresh }: { version: Version; refresh: () => Promise }) { const { can, principal, request, downloadPdf } = usePlatform(); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const reviewer = can("supplier_document.validate"); const ownVersion = version.uploadedById === principal?.userId.toLowerCase(); return

{version.filename}

{version.decision &&

{version.decision.outcome}{version.decision.reason && ` · ${version.decision.reason}`}

} {(reviewer || version.decision?.outcome === "APPROVED") && <> } {reviewer && !version.decision && (ownVersion ? ( Another reviewer must decide on this version. ) : documentDecision.parse({ outcome: readText(data, "outcome"), reason: readText(data, "reason").trim() || undefined, })} submit={async (body) => { try { await request(`/supplier-document-versions/${version.id}/decision`, documentVersion, { method: "POST", body }); } finally { await refresh(); } }}>

Review decisions are final.

)}
; } export default function SupplierDocumentsPage({ supplierId }: { supplierId?: string }) { const id = uuid.safeParse(supplierId); return {!supplierId ? : id.success ? : Invalid supplier ID. Open supplier documents} ; }