visual-notebook / frontend /src /components /NotebookPage.tsx
Sid-WC121
bugfix2 + update
8ac8526
Raw History Blame Contribute Delete
9 kB
import React, { useEffect, useState } from 'react';
import { useSessionStore } from '../store/session';
import { useUIStore } from '../store/ui';
import { useNotebookStore } from '../store/notebook';
import { useHistoryStore } from '../store/history';
import { UploadPanel } from './UploadPanel';
import { TableCellView } from './TableCellView';
import { ChartCellView } from './ChartCellView';
import { SchemaView } from './SchemaView';
import { MapCanvas } from './MapCanvas';
import { Sidebar } from './Sidebar';
import { getDemo, loadDemo } from '../api/client';
export function NotebookPage() {
const { isLoaded, setSession, setSchema } = useSessionStore();
const { showUploadDialog, addError } = useUIStore();
const { cells, activeCellId, setActiveCell, removeCell, toggleCollapse } = useNotebookStore();
const { reset: resetHistory } = useHistoryStore();
const { showSidebar } = useUIStore();
const [demoAvailable, setDemoAvailable] = useState(false);
const [demoLoading, setDemoLoading] = useState(false);
useEffect(() => {
getDemo().then((res) => {
setDemoAvailable(res.available);
}).catch(() => {});
}, []);
const handleLoadDemo = async () => {
setDemoLoading(true);
try {
const result = await loadDemo();
setSession(result.session_id, result.dataset_id, result.table_name);
setSchema(result.columns, result.row_count);
resetHistory();
useNotebookStore.getState().addCell('table', 'Data Preview');
useNotebookStore.getState().addCell('schema', 'Schema');
} catch (err: any) {
addError(err.message || 'Failed to load demo');
} finally {
setDemoLoading(false);
}
};
if (!isLoaded) {
return (
<div className="flex flex-col items-center justify-center h-[calc(100vh-57px)]">
<div className="w-24 h-24 bg-gradient-to-br from-brand-500 to-brand-700 rounded-3xl flex items-center justify-center mb-8 shadow-lg shadow-brand-200">
<svg className="w-12 h-12 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 17V7m0 10a2 2 0 01-2 2H5a2 2 0 01-2-2V7a2 2 0 012-2h2a2 2 0 012 2m0 10a2 2 0 002 2h2a2 2 0 002-2M9 7a2 2 0 012-2h2a2 2 0 012 2m0 10V7m0 10a2 2 0 002 2h2a2 2 0 002-2V7a2 2 0 00-2-2h-2a2 2 0 00-2 2" />
</svg>
</div>
<h2 className="text-3xl font-bold text-gray-900 mb-3">Visual Notebook</h2>
<p className="text-gray-500 mb-4 text-center max-w-lg leading-relaxed">
Upload a CSV file to start exploring your data with interactive tables, charts, and transformations.
</p>
<div className="flex gap-6 mb-8 text-sm text-gray-500">
<span className="flex items-center gap-1.5 hover:text-brand-600 transition-colors cursor-default">
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M3 10h18M3 14h18" /></svg>
Tables
</span>
<span className="flex items-center gap-1.5 hover:text-brand-600 transition-colors cursor-default">
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6m6 0V9a2 2 0 012-2h2a2 2 0 012 2v10m6 0v-4a2 2 0 00-2-2h-2a2 2 0 00-2 2v4" /></svg>
Charts
</span>
<span className="flex items-center gap-1.5 hover:text-brand-600 transition-colors cursor-default">
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /></svg>
Transforms
</span>
<span className="flex items-center gap-1.5 hover:text-brand-600 transition-colors cursor-default">
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" /></svg>
Maps
</span>
</div>
<div className="flex gap-3">
<button onClick={() => useUIStore.getState().toggleUploadDialog()} className="btn-primary text-base px-8 py-3.5 shadow-lg shadow-brand-200 hover:shadow-xl hover:shadow-brand-300 transition-all">
Upload CSV
</button>
{demoAvailable && (
<button
onClick={handleLoadDemo}
disabled={demoLoading}
className="btn-secondary text-base px-8 py-3.5 transition-all"
>
{demoLoading ? 'Loading...' : 'Try Demo Data'}
</button>
)}
</div>
{showUploadDialog && <UploadPanel />}
</div>
);
}
return (
<div className="flex h-[calc(100vh-57px)] overflow-hidden max-w-full">
{/* Left sidebar */}
{showSidebar && <Sidebar />}
{/* Main content area */}
<div className="flex-1 min-w-0 overflow-y-auto overflow-x-hidden">
<div className="p-4 lg:p-6 max-w-full min-w-0">
{cells.length === 0 ? (
<div className="text-center py-12 text-gray-400">
<svg className="w-16 h-16 mx-auto mb-3 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 17V7m0 10a2 2 0 01-2 2H5a2 2 0 01-2-2V7a2 2 0 012-2h2a2 2 0 012 2m0 10a2 2 0 002 2h2a2 2 0 002-2M9 7a2 2 0 012-2h2a2 2 0 012 2m0 10V7m0 10a2 2 0 002 2h2a2 2 0 002-2V7a2 2 0 00-2-2h-2a2 2 0 00-2 2" />
</svg>
<p className="text-lg mb-1">No cells yet</p>
<p className="text-sm">Use the sidebar to transform and visualize your data</p>
</div>
) : (
<div className="space-y-4 max-w-full">
{cells.map((cell, index) => (
<div
key={cell.id}
className={`cell-card transition-all max-w-full ${
activeCellId === cell.id ? 'ring-2 ring-brand-500 ring-offset-2' : ''
}`}
onClick={() => setActiveCell(cell.id)}
>
<div className="flex items-center justify-between px-4 py-2 border-b border-gray-100">
<div className="flex items-center gap-2 min-w-0">
<span className="text-xs font-mono text-gray-400 flex-shrink-0">[{index + 1}]</span>
<span className="text-sm font-medium text-gray-700 truncate">{cell.title}</span>
<span className="badge badge-categorical text-[10px] flex-shrink-0">{cell.type}</span>
</div>
<div className="flex items-center gap-1 flex-shrink-0">
<button
onClick={(e) => { e.stopPropagation(); toggleCollapse(cell.id); }}
className="p-1 text-gray-400 hover:text-gray-600 rounded"
title={cell.collapsed ? 'Expand' : 'Collapse'}
>
<svg className={`w-4 h-4 transition-transform ${cell.collapsed ? '' : 'rotate-180'}`} fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
</svg>
</button>
<button
onClick={(e) => { e.stopPropagation(); removeCell(cell.id); }}
className="p-1 text-gray-400 hover:text-red-500 rounded"
title="Remove cell"
>
<svg className="w-4 h-4" 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>
{!cell.collapsed && (
<div className="p-4 overflow-x-auto max-w-full">
{cell.type === 'table' && <TableCellView cellId={cell.id} config={cell.config} />}
{cell.type === 'chart' && <ChartCellView cellId={cell.id} config={cell.config} />}
{cell.type === 'schema' && <SchemaView />}
{cell.type === 'map' && <MapCanvas cellId={cell.id} config={cell.config} />}
</div>
)}
</div>
))}
</div>
)}
</div>
</div>
{showUploadDialog && <UploadPanel />}
</div>
);
}