'use client'; import { useCallback, useState } from 'react'; import { useDropzone } from 'react-dropzone'; import { UploadEmptyState } from './UploadEmptyState'; import { uploadClient } from '@/lib/api/upload-client'; import { cn } from '@/lib/utils'; import { Loader2, File, CheckCircle2, AlertCircle } from 'lucide-react'; export function UploadDropzone({ onUploadComplete }: { onUploadComplete?: (fileId: string, filename: string) => void }) { const [isUploading, setIsUploading] = useState(false); const [progress, setProgress] = useState(0); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [uploadingFilename, setUploadingFilename] = useState(null); const onDrop = useCallback(async (acceptedFiles: File[]) => { if (acceptedFiles.length > 0) { const file = acceptedFiles[0]; setUploadingFilename(file.name); setIsUploading(true); setProgress(0); setError(null); setSuccess(false); try { const response = await uploadClient.uploadFile(file, (percent) => setProgress(percent)); if (response.success && response.data) { setSuccess(true); setProgress(100); if (onUploadComplete) { onUploadComplete(response.data.fileId, response.data.filename); } } else { setError(response.message || "Failed to upload file"); } } catch (err: unknown) { setError(err instanceof Error ? err.message : "An unexpected error occurred"); } finally { setIsUploading(false); } } }, [onUploadComplete]); const { getRootProps, getInputProps, isDragActive, isDragReject } = useDropzone({ onDrop, disabled: isUploading || success, maxFiles: 1, accept: { 'application/x-netcdf': ['.nc'], 'application/x-hdf5': ['.h5', '.hdf5'] } }); return (
{!isUploading && !error && !success && ( )} {isUploading && (

Uploading {uploadingFilename}...

{progress}%
)} {error && (

{error}

Click or drag to try again

)} {success && (

{uploadingFilename} Uploaded Successfully

)} {/* Decorative corners */}
); }