import React, { useRef, useState } from 'react'; import './DragUpload.css'; export default function DragUpload() { const [files, setFiles] = useState([]); const [over, setOver] = useState(false); const fileRef = useRef(null); const addFiles = (flist) => { Array.prototype.forEach.call(flist, (f) => { const id = 'f' + Date.now() + Math.random().toString(36).slice(2, 6); const item = { id, name: f.name, progress: 0, done: false, thumb: '' }; if (/^image\//.test(f.type) && f.size < 2 * 1024 * 1024) { const reader = new FileReader(); reader.onload = (e) => setFiles((fs) => fs.map((x) => (x.id === id ? { ...x, thumb: e.target.result } : x))); reader.readAsDataURL(f); } setFiles((fs) => [...fs, item]); const t = setInterval(() => { setFiles((fs) => fs.map((x) => { if (x.id !== id) return x; const p = Math.min(100, x.progress + Math.random() * 22); return { ...x, progress: p, done: p >= 100 }; }) ); setFiles((fs) => { if (fs.find((x) => x.id === id && x.progress >= 100)) clearInterval(t); return fs; }); }, 220); }); }; const remove = (id) => setFiles((fs) => fs.filter((x) => x.id !== id)); return (