import { useCallback, useState } from 'react' import { Upload, FileJson, AlertCircle } from 'lucide-react' interface FileDropzoneProps { onFileSelect: (file: File) => void disabled?: boolean error?: string | null } export default function FileDropzone({ onFileSelect, disabled = false, error }: FileDropzoneProps) { const [isDragging, setIsDragging] = useState(false) const handleDragOver = useCallback( (e: React.DragEvent) => { e.preventDefault() if (!disabled) { setIsDragging(true) } }, [disabled] ) const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault() setIsDragging(false) }, []) const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) if (disabled) return const files = e.dataTransfer.files if (files.length > 0) { const file = files[0] if (file.type === 'application/json' || file.name.endsWith('.json')) { onFileSelect(file) } } }, [disabled, onFileSelect] ) const handleFileInput = useCallback( (e: React.ChangeEvent) => { const files = e.target.files if (files && files.length > 0) { onFileSelect(files[0]) } // Reset input value to allow selecting the same file again e.target.value = '' }, [onFileSelect] ) return (
{isDragging ? ( ) : ( )}

{isDragging ? '释放文件以上传' : '拖拽 JSON 文件到此处'}

或点击选择文件

支持格式: JSON 数组/单对象/JSONL(支持注释与末尾逗号)
{error && (
{error}
)}
) }