import React from "react"; import { Box, Typography, Avatar, Chip, Button, FormHelperText } from "@mui/material"; import type { FieldConfig } from "../../../types"; import { getApi } from "../../../hooks/useApi"; import { useAppContext } from "../../../context/AppContext"; import { resolveMediaUrl } from "../utils"; interface Props { field: FieldConfig; value: any; onChange: (value: any) => void; } const acceptMap: Record = { image: "image/*", pdf: ".pdf", csv: ".csv", html: ".html,.htm", }; export function FileUploadField({ field, value, onChange }: Props) { const uploadConfig = field.upload!; const inputRef = React.useRef(null); const { config } = useAppContext(); const handleUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const arrayBuffer = await file.arrayBuffer(); const binary = new Uint8Array(arrayBuffer); const api = getApi(); const res = await api.post(uploadConfig.url, binary, { headers: { "Content-Type": file.type, "Content-Disposition": `attachment; filename="${file.name}"`, }, }); onChange(res.data.saved_as); if (inputRef.current) inputRef.current.value = ""; }; const handleReplace = () => { onChange(null); setTimeout(() => inputRef.current?.click(), 0); }; const accept = acceptMap[uploadConfig.type] ?? undefined; return ( {field.label} {value ? ( uploadConfig.type === "image" ? ( ) : ( ) ) : ( )} {field.description && {field.description}} ); }