'use client'; import { useState, useRef } from 'react'; interface FileUploadModalProps { isOpen: boolean; onClose: () => void; onUpload: (files: File[]) => void; } export default function FileUploadModal({ isOpen, onClose, onUpload }: FileUploadModalProps) { const [selectedFiles, setSelectedFiles] = useState([]); const [dragActive, setDragActive] = useState(false); const fileInputRef = useRef(null); // Разрешенные типы файлов const allowedTypes = [ 'application/pdf', 'image/png', 'text/plain', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'text/csv', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ]; const allowedExtensions = ['.pdf', '.png', '.txt', '.doc', '.docx', '.csv', '.xls', '.xlsx']; const handleFileSelect = (files: FileList | null) => { if (!files) return; const validFiles: File[] = []; const invalidFiles: string[] = []; Array.from(files).forEach(file => { const isValidType = allowedTypes.includes(file.type) || allowedExtensions.some(ext => file.name.toLowerCase().endsWith(ext)); if (isValidType) { validFiles.push(file); } else { invalidFiles.push(file.name); } }); if (invalidFiles.length > 0) { alert(`Следующие файлы не поддерживаются:\n${invalidFiles.join('\n')}\n\nПоддерживаемые форматы: PDF, PNG, TXT, DOC, DOCX, CSV, XLS, XLSX`); } setSelectedFiles(prev => [...prev, ...validFiles]); }; const handleDrag = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (e.type === 'dragenter' || e.type === 'dragover') { setDragActive(true); } else if (e.type === 'dragleave') { setDragActive(false); } }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); handleFileSelect(e.dataTransfer.files); }; const handleFileInputChange = (e: React.ChangeEvent) => { handleFileSelect(e.target.files); }; const removeFile = (index: number) => { setSelectedFiles(prev => prev.filter((_, i) => i !== index)); }; const handleUpload = () => { if (selectedFiles.length === 0) { alert('Пожалуйста, выберите файлы для загрузки'); return; } onUpload(selectedFiles); setSelectedFiles([]); onClose(); }; const formatFileSize = (bytes: number) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]; }; if (!isOpen) return null; return (
e.stopPropagation()} >

Загрузка документов

fileInputRef.current?.click()} >
📄
Перетащите файлы сюда или нажмите для выбора
Поддерживаемые форматы: PDF, PNG, TXT, DOC, DOCX, CSV, XLS, XLSX
{selectedFiles.length > 0 && (

Выбранные файлы ({selectedFiles.length})

{selectedFiles.map((file, index) => (
{file.name}
{formatFileSize(file.size)}
))}
)}
); }