import { Box, Button, Group, Paper, Stack, Text } from "@mantine/core"; import { FileText, Upload } from "lucide-react"; import type { ReactNode } from "react"; import type { Freight } from "@edr/types"; import { PhasedFileDropzone } from "@/components/contracts/PhasedFileDropzone"; import { PhasedUploadedFileRow, findWorkflowFile } from "@/components/contracts/PhasedUploadedFileRow"; export interface PhasedDocumentUploadFieldProps { fields: Array<{ key: string; label: string }>; files: Record; onChange: (key: string, file: File | null) => void; workflowFiles?: Freight.ClearanceWorkflowFile[]; helperText: string; /** Extra inputs rendered above the dropzones (e.g. required DO dates). */ extraFields?: ReactNode; replaceMode?: boolean; loading?: boolean; disabled?: boolean; submitLabel?: string; onSubmit: () => void; onViewFile?: (file: { name: string; url: string }) => void; onDownloadFile?: (file: { id: string; name: string }) => void; } /** Consistent phased customs document upload with drag-and-drop, preview, and uploaded rows. */ export function PhasedDocumentUploadField({ fields, files, onChange, workflowFiles = [], helperText, extraFields, replaceMode = false, loading = false, disabled = false, submitLabel, onSubmit, onViewFile, onDownloadFile, }: PhasedDocumentUploadFieldProps) { const hasStaged = Object.values(files).some(Boolean); const uploaded = fields .map((f) => ({ ...f, file: findWorkflowFile(workflowFiles, f.key) })) .filter((f) => f.file); const multiField = fields.length > 1; return ( {uploaded.length > 0 ? ( Current file{uploaded.length > 1 ? "s" : ""} {uploaded.map((row) => ( ))} ) : null} {replaceMode ? "Replace document" : "Upload document"} {helperText} {extraFields} {fields.map((f) => ( u.key === f.key) ? "Drop a new file to replace the current one." : `Upload ${f.label} (optional if another declaration type is provided).` : undefined } value={files[f.key] ?? null} onChange={(file) => onChange(f.key, file)} replaceMode={replaceMode || uploaded.some((u) => u.key === f.key)} onPreview={onViewFile} /> ))} ); }