import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import CancelIcon from '@mui/icons-material/Cancel';
import {
Box,
Button,
LinearProgress,
type LinearProgressProps,
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';
function LinearProgressWithLabel(props: LinearProgressProps & { value: number }) {
return (
{`${Math.round(
props.value
)}%`}
);
}
const SingleUpload = ({ setRestartNeeded }) => {
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) {
setMd5(data.md5 as string);
toast.success(LL.UPLOAD() + ' MD5 ' + LL.SUCCESSFUL());
setFile(undefined);
} else {
setRestartNeeded(true);
}
});
useEffect(async () => {
if (file) {
console.log('going to upload file ', file.name);
await sendUpload(file).catch((error: Error) => {
if (error.message === 'The user aborted a request') {
toast.warning(LL.UPLOAD() + ' ' + LL.ABORTED());
} else if (error.message === 'Network Error') {
toast.warning('Invalid file extension or incompatible bin file');
} else {
toast.error(error.message);
}
});
}
}, [file]);
return (
<>
{isUploading ? (
<>
}
variant="outlined"
color="error"
onClick={cancelUpload}
>
{LL.CANCEL()}
>
) : (
)}
{md5 && (
{'MD5: ' + md5}
)}
>
);
};
export default SingleUpload;