import { useEffect, useState } from 'react'; import { toast } from 'react-toastify'; import CancelIcon from '@mui/icons-material/Cancel'; import { Box, Button, Typography } from '@mui/material'; import * as SystemApi from 'api/system'; import { useRequest } from 'alova/client'; import { useI18nContext } from 'i18n/i18n-react'; import DragNdrop from './DragNdrop'; import { LinearProgressWithLabel } from './LinearProgressWithLabel'; interface SingleUploadProps { doRestart: () => void; } const SingleUpload = ({ doRestart }: SingleUploadProps) => { const [md5, setMd5] = useState(); const [file, setFile] = useState(); const { LL } = useI18nContext(); const { loading: isUploading, uploading: progress, send: sendUpload, abort: cancelUpload } = useRequest(SystemApi.uploadFile, { immediate: false }).onSuccess(({ data }) => { if (data && typeof data === 'object' && 'md5' in data) { setMd5((data as { md5: string }).md5); toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL()); setFile(undefined); } else { doRestart(); } }); useEffect(() => { const uploadFile = async () => { if (file) { await sendUpload(file).catch((error: Error) => { if (error.message.includes('The user aborted a request')) { toast.warning(LL.UPLOAD() + ' ' + LL.ABORTED()); } else { toast.warning('Invalid file extension or incompatible bin file'); } }); } }; void uploadFile(); }, [file]); return ( <> {isUploading ? ( <> ) : ( )} {md5 && ( {'MD5: ' + md5} )} ); }; export default SingleUpload;