File size: 2,107 Bytes
057576a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import React, { useCallback } from 'react';
import { useDropzone } from 'react-dropzone';
import { Upload, File, Image, Music, X } from 'lucide-react';
import { validateFile, formatFileSize } from '../../utils/fileValidation';
const FileUpload = ({ onFileSelect, multiple = false, acceptedTypes = [] }) => {
const onDrop = useCallback((acceptedFiles) => {
acceptedFiles.forEach(file => {
try {
validateFile(file);
onFileSelect(file);
} catch (error) {
alert(`Error with file ${file.name}: ${error.message}`);
}
});
}, [onFileSelect]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
multiple,
accept: acceptedTypes.length > 0 ? acceptedTypes.reduce((acc, type) => {
acc[type] = [];
return acc;
}, {}) : undefined
});
const getFileIcon = (file) => {
if (file.type.startsWith('image/')) return <Image className="w-8 h-8" />;
if (file.type.startsWith('audio/')) return <Music className="w-8 h-8" />;
return <File className="w-8 h-8" />;
};
return (
<div
{...getRootProps()}
className={`file-upload-zone ${isDragActive ? 'active' : ''}`}
>
<input {...getInputProps()} />
<div className="text-center">
<Upload className="w-12 h-12 text-gray-400 mx-auto mb-4" />
{isDragActive ? (
<p className="text-lg font-medium text-primary-600 dark:text-primary-400">
Drop the files here...
</p>
) : (
<>
<p className="text-lg font-medium text-gray-900 dark:text-white mb-2">
Drag & drop files here
</p>
<p className="text-sm text-gray-500 dark:text-gray-400">
or click to select files
</p>
</>
)}
<div className="mt-4 text-xs text-gray-500 dark:text-gray-400">
Supports: Images, Audio, PDF, Text files (Max: 50MB)
</div>
</div>
</div>
);
};
export default FileUpload; |