visual-notebook / frontend /src /components /UploadPanel.tsx
Sid-WC121
initial
c21180c
Raw History Blame Contribute Delete
4.05 kB
import React, { useRef, useState } from 'react';
import { useUpload } from '../api/hooks';
import { useSessionStore } from '../store/session';
import { useUIStore } from '../store/ui';
import { useNotebookStore } from '../store/notebook';
import { useHistoryStore } from '../store/history';
import { formatFileSize } from '../lib/csv';
export function UploadPanel() {
const fileRef = useRef<HTMLInputElement>(null);
const [dragActive, setDragActive] = useState(false);
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const { upload, loading, error } = useUpload();
const { setSession, setSchema } = useSessionStore();
const { toggleUploadDialog, addError } = useUIStore();
const { addCell } = useNotebookStore();
const { reset: resetHistory } = useHistoryStore();
const handleUpload = async () => {
if (!selectedFile) return;
const result = await upload(selectedFile);
if (result) {
setSession(result.session_id, result.dataset_id, result.table_name);
setSchema(result.columns, result.row_count);
resetHistory();
addCell('table', 'Data Preview');
addCell('schema', 'Schema');
toggleUploadDialog();
} else if (error) {
addError(error);
}
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
setDragActive(false);
const file = e.dataTransfer.files[0];
if (file?.name.endsWith('.csv')) {
setSelectedFile(file);
}
};
return (
<div className="fixed inset-0 bg-black/40 z-50 flex items-center justify-center" onClick={() => toggleUploadDialog()}>
<div className="bg-white rounded-2xl shadow-2xl max-w-md w-full mx-4 p-6" onClick={(e) => e.stopPropagation()}>
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold">Upload CSV</h2>
<button onClick={() => toggleUploadDialog()} className="text-gray-400 hover:text-gray-600">
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div
className={`border-2 border-dashed rounded-xl p-8 text-center transition-colors ${
dragActive ? 'border-brand-500 bg-brand-50' : 'border-gray-300 hover:border-gray-400'
}`}
onDragOver={(e) => { e.preventDefault(); setDragActive(true); }}
onDragLeave={() => setDragActive(false)}
onDrop={handleDrop}
onClick={() => fileRef.current?.click()}
>
<svg className="w-10 h-10 text-gray-400 mx-auto mb-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" />
</svg>
{selectedFile ? (
<div>
<p className="font-medium text-gray-900">{selectedFile.name}</p>
<p className="text-sm text-gray-500">{formatFileSize(selectedFile.size)}</p>
</div>
) : (
<div>
<p className="text-gray-600">Drop a CSV file here or click to browse</p>
<p className="text-xs text-gray-400 mt-1">Supports files up to 500MB</p>
</div>
)}
<input ref={fileRef} type="file" accept=".csv" className="hidden" onChange={(e) => setSelectedFile(e.target.files?.[0] || null)} />
</div>
{error && (
<p className="text-red-600 text-sm mt-3">{error}</p>
)}
<div className="flex gap-3 mt-4">
<button onClick={() => toggleUploadDialog()} className="btn-secondary flex-1">Cancel</button>
<button onClick={handleUpload} disabled={!selectedFile || loading} className="btn-primary flex-1">
{loading ? 'Uploading...' : 'Upload'}
</button>
</div>
</div>
</div>
);
}