import { FC, Fragment } from 'react'; import { useDropzone, DropzoneState } from 'react-dropzone'; import { Box, Button, LinearProgress, Theme, Typography, useTheme } from '@mui/material'; import CloudUploadIcon from '@mui/icons-material/CloudUpload'; import CancelIcon from '@mui/icons-material/Cancel'; const progressPercentage = (progress: ProgressEvent) => Math.round((progress.loaded * 100) / progress.total); const getBorderColor = (theme: Theme, props: DropzoneState) => { if (props.isDragAccept) { return theme.palette.success.main; } if (props.isDragReject) { return theme.palette.error.main; } if (props.isDragActive) { return theme.palette.info.main; } return theme.palette.grey[700]; }; export interface SingleUploadProps { onDrop: (acceptedFiles: File[]) => void; onCancel: () => void; uploading: boolean; progress?: ProgressEvent; } const SingleUpload: FC = ({ onDrop, onCancel, uploading, progress }) => { const dropzoneState = useDropzone({ onDrop, accept: { 'application/octet-stream': ['.bin'], 'application/json': ['.json'] }, disabled: uploading, multiple: false }); const { getRootProps, getInputProps } = dropzoneState; const theme = useTheme(); const progressText = () => { if (uploading) { if (progress?.lengthComputable) { return `Uploading: ${progressPercentage(progress)}%`; } return 'Uploading\u2026'; } return 'Drop file or click here'; }; return ( {progressText()} {uploading && ( )} ); }; export default SingleUpload;