import React from 'react'; import { useDropzone } from 'react-dropzone'; import { Iconify } from '@seliseblocks/next-images'; import Box from '@mui/material/Box'; import Stack from '@mui/material/Stack'; import Button from '@mui/material/Button'; import { alpha } from '@mui/material/styles'; import IconButton from '@mui/material/IconButton'; import Typography from '@mui/material/Typography'; import { IUploadProps } from '../types'; import { MultiFilePreview } from './preview-multi-file'; import { RejectionFiles } from './errors-rejection-files'; import { SingleFilePreview } from './preview-single-file'; import { UploadIllustration } from './upload-illustration'; /** * @description {Upload} - Function for handling file uploads. * @param {boolean} disabled - Indicates whether the upload is disabled. * @param {boolean} multiple - Indicates whether multiple files can be uploaded. * @param {boolean} error - Indicates whether there is an error. * @param {React.ReactNode} helperText - The helper text for the upload. * @param {ICustomFile | string | null} file - The file to be uploaded. * @param {VoidFunction} onDelete - Function to delete the file. * @param {File[]} files - The files to be uploaded. * @param {boolean} thumbnail - Indicates whether to show a thumbnail. * @param {VoidFunction} onUpload - Function to upload the file. * @param {VoidFunction} onRemove - Function to remove the file. * @param {VoidFunction} onRemoveAll - Function to remove all files. * @param {SxProps} sx - The style for the upload. * @return {JSX.Element} The rendered upload component. */ export const Upload = ({ disabled, multiple = false, error, helperText, file, onDelete, files, thumbnail, onUpload, onRemove, onRemoveAll, sx, ...other }: IUploadProps) => { const { getRootProps, getInputProps, isDragActive, isDragReject, fileRejections } = useDropzone({ multiple, disabled, ...other, }); const hasFile = !!file && !multiple; const hasFiles = !!files && multiple && !!files.length; const hasError = isDragReject || !!error; const renderPlaceholder = ( Drop or Select file Drop files here or click browse thorough your machine ); const renderSinglePreview = ( ); const removeSinglePreview = hasFile && onDelete && ( alpha(theme.palette.common.white, 0.8), bgcolor: (theme) => alpha(theme.palette.grey[900], 0.72), '&:hover': { bgcolor: (theme) => alpha(theme.palette.grey[900], 0.48), }, }} > ); const renderMultiPreview = hasFiles && ( <> {onRemoveAll && ( )} {onUpload && ( )} ); return ( alpha(theme.palette.grey[500], 0.08), border: (theme) => `1px dashed ${alpha(theme.palette.grey[500], 0.2)}`, transition: (theme) => theme.transitions.create(['opacity', 'padding']), '&:hover': { opacity: 0.72, }, ...(isDragActive && { opacity: 0.72, }), ...(disabled && { opacity: 0.48, pointerEvents: 'none', }), ...(hasError && { color: 'error.main', borderColor: 'error.main', bgcolor: (theme) => alpha(theme.palette.error.main, 0.08), }), ...(hasFile && { padding: '24% 0', }), }} > {hasFile ? renderSinglePreview : renderPlaceholder} {removeSinglePreview} {helperText && helperText} {renderMultiPreview} ); };