import { useDropzone } from 'react-dropzone';
// @mui
import { alpha } from '@mui/material/styles';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
//
import Iconify from '../iconify';
import Image from '../image';
//
import { UploadProps } from './types';
import RejectionFiles from './errors-rejection-files';
// ----------------------------------------------------------------------
export default function UploadAvatar({
error,
file,
disabled,
helperText,
sx,
...other
}: UploadProps) {
const { getRootProps, getInputProps, isDragActive, isDragReject, fileRejections } = useDropzone({
multiple: false,
disabled,
accept: {
'image/*': [],
},
...other,
});
const hasFile = !!file;
const hasError = isDragReject || !!error;
const imgUrl = typeof file === 'string' ? file : file?.preview;
const renderPreview = hasFile && (
);
const renderPlaceholder = (
alpha(theme.palette.grey[500], 0.08),
transition: (theme) =>
theme.transitions.create(['opacity'], {
duration: theme.transitions.duration.shorter,
}),
'&:hover': {
opacity: 0.72,
},
...(hasError && {
color: 'error.main',
bgcolor: (theme) => alpha(theme.palette.error.main, 0.08),
}),
...(hasFile && {
zIndex: 9,
opacity: 0,
color: 'common.white',
bgcolor: (theme) => alpha(theme.palette.grey[900], 0.64),
}),
}}
>
{file ? 'Update photo' : 'Upload photo'}
);
const renderContent = (
{renderPreview}
{renderPlaceholder}
);
return (
<>
`1px dashed ${alpha(theme.palette.grey[500], 0.2)}`,
...(isDragActive && {
opacity: 0.72,
}),
...(disabled && {
opacity: 0.48,
pointerEvents: 'none',
}),
...(hasError && {
borderColor: 'error.main',
}),
...(hasFile && {
...(hasError && {
bgcolor: (theme) => alpha(theme.palette.error.main, 0.08),
}),
'&:hover .upload-placeholder': {
opacity: 1,
},
}),
...sx,
}}
>
{renderContent}
{helperText && helperText}
>
);
}