import React, { useEffect, useMemo, useState } from 'react' function toImageFiles(files) { return Array.from(files || []).filter((file) => String(file.type || '').startsWith('image/')) } export default function ScreenshotUploader({ files = [], onChange, min = 1, max = 5, required = false, error = '', }) { const [dragging, setDragging] = useState(false) const previews = useMemo( () => files.map((file) => ({ file, url: window.URL.createObjectURL(file) })), [files] ) useEffect(() => { return () => { previews.forEach((preview) => window.URL.revokeObjectURL(preview.url)) } }, [previews]) const mergeFiles = (incomingFiles) => { const next = [...files, ...toImageFiles(incomingFiles)].slice(0, max) if (typeof onChange === 'function') { onChange(next) } } const replaceFiles = (incomingFiles) => { const next = toImageFiles(incomingFiles).slice(0, max) if (typeof onChange === 'function') { onChange(next) } } const removeAt = (index) => { const next = files.filter((_, idx) => idx !== index) if (typeof onChange === 'function') { onChange(next) } } const move = (from, to) => { if (to < 0 || to >= files.length) return const next = [...files] const [picked] = next.splice(from, 1) next.splice(to, 0, picked) if (typeof onChange === 'function') { onChange(next) } } return (
Drag & drop screenshots here
Minimum {min}, maximum {max}
{error}
: null} {previews.length > 0 ? (