import React from "react" import { FileValue } from "@mwater/expressions" import FileEditorComponent from "../FileEditorComponent" import { FileManager } from "../formContext" /** Default maximum file size in MB when not specified */ const DEFAULT_MAX_FILE_SIZE_MB = 10 /** Mapping from preset category IDs to HTML accept attribute fragments */ const FILE_TYPE_ACCEPT_MAP: Record = { pdf: ".pdf,application/pdf", word: ".doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document", excel: ".xls,.xlsx,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", images: "image/*", audio: "audio/*", video: "video/*", text_csv: ".txt,.csv,text/plain,text/csv" } /** Convert an array of preset category IDs to an HTML accept attribute string */ export function buildAcceptString(acceptedFileTypes?: string[]): string | undefined { if (!acceptedFileTypes || acceptedFileTypes.length === 0) return undefined return acceptedFileTypes .map((id) => FILE_TYPE_ACCEPT_MAP[id]) .filter(Boolean) .join(",") || undefined } export interface FileAnswerComponentProps { file?: FileValue | null onFileChange?: (file: FileValue | null) => void fileManager?: FileManager /** Preset category IDs from the question design */ acceptedFileTypes?: string[] /** Maximum file size in MB (from question design). Defaults to 10. */ maxFileSizeMB?: number } /** Answer component for a single file attachment question */ export default function FileAnswerComponent(props: FileAnswerComponentProps) { const { file, onFileChange, fileManager, acceptedFileTypes, maxFileSizeMB } = props if (!fileManager) { return
{T("File uploads not available")}
} const accept = buildAcceptString(acceptedFileTypes) const maxFileSize = (maxFileSizeMB ?? DEFAULT_MAX_FILE_SIZE_MB) * 1024 * 1024 return ( ) }