Files
DataClaw/frontend/src/components/VisualizationPanel.tsx
T

181 lines
7.4 KiB
TypeScript
Raw Normal View History

2026-03-14 15:52:27 +08:00
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription } from "@/components/ui/dialog";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
2026-03-15 01:29:36 +08:00
import { Code, Table as TableIcon, BarChart as ChartIcon, Download, LayoutDashboard, Loader2 } from "lucide-react";
2026-03-14 15:52:27 +08:00
import { ScrollArea } from "@/components/ui/scroll-area";
import { useDashboardStore } from "@/store/dashboardStore";
import { useVisualizationStore } from "@/store/visualizationStore";
2026-03-15 01:29:36 +08:00
import { VegaChart } from "./VegaChart";
2026-03-14 15:52:27 +08:00
export function VisualizationPanel() {
const [view, setView] = useState<'table' | 'chart'>('chart');
const { addChart } = useDashboardStore();
2026-03-15 10:49:37 +08:00
const { currentData, currentSQL, currentChartSpec, currentChartInfo, isLoading, error } = useVisualizationStore();
2026-03-14 15:52:27 +08:00
const handleAddToDashboard = () => {
if (!currentData || !currentSQL) return;
2026-03-15 10:49:37 +08:00
const mark = currentChartSpec?.mark;
const markType = typeof mark === "string" ? mark : mark?.type;
const dashboardType = markType === "line" ? "line" : "bar";
2026-03-14 15:52:27 +08:00
addChart({
id: Date.now().toString(),
2026-03-15 01:29:36 +08:00
title: currentChartSpec?.title || 'Generated Analysis',
2026-03-15 10:49:37 +08:00
type: dashboardType,
2026-03-14 15:52:27 +08:00
data: currentData,
sql: currentSQL,
2026-03-15 17:57:09 +08:00
chartSpec: currentChartSpec,
2026-03-14 15:52:27 +08:00
});
alert("Added to Dashboard!");
};
if (isLoading) {
return (
<div className="h-full flex items-center justify-center bg-muted/10">
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
<span className="ml-2 text-muted-foreground">Generating visualization...</span>
</div>
);
}
if (error) {
return (
<div className="h-full flex flex-col items-center justify-center bg-muted/10 p-4">
<div className="text-destructive font-semibold mb-2">Visualization Error</div>
<div className="text-sm text-muted-foreground text-center">{error}</div>
</div>
)
}
if (!currentData || currentData.length === 0) {
return (
<div className="h-full flex flex-col items-center justify-center bg-muted/10 text-muted-foreground">
<ChartIcon className="h-12 w-12 mb-4 opacity-20" />
<p>No data to visualize.</p>
<p className="text-sm">Ask the chat to generate some insights!</p>
</div>
);
}
const objectRows = currentData.filter((row) => row && typeof row === "object" && !Array.isArray(row));
if (objectRows.length === 0) {
return (
<div className="h-full flex flex-col items-center justify-center bg-muted/10 text-muted-foreground">
<ChartIcon className="h-12 w-12 mb-4 opacity-20" />
<p>Data format is not supported for visualization.</p>
</div>
);
}
const columns = Object.keys(objectRows[0] as Record<string, unknown>);
return (
<div className="h-full flex flex-col bg-muted/10 overflow-hidden">
{/* Toolbar */}
<div className="border-b p-3 bg-background flex justify-between items-center shrink-0">
<h2 className="font-semibold text-sm uppercase tracking-wider text-muted-foreground ml-2">Visualization</h2>
<div className="flex gap-2 items-center">
<div className="flex bg-muted rounded-md p-1 mr-2">
<Button
variant={view === 'table' ? "secondary" : "ghost"}
size="sm"
className="h-7 px-3 text-xs"
onClick={() => setView('table')}
>
<TableIcon className="h-3.5 w-3.5 mr-1.5" />
Table
</Button>
<Button
variant={view === 'chart' ? "secondary" : "ghost"}
size="sm"
className="h-7 px-3 text-xs"
onClick={() => setView('chart')}
>
<ChartIcon className="h-3.5 w-3.5 mr-1.5" />
Chart
</Button>
</div>
<Button variant="outline" size="sm" className="h-7 text-xs" onClick={handleAddToDashboard}>
<LayoutDashboard className="h-3.5 w-3.5 mr-1.5" />
Add to Dashboard
</Button>
<Dialog>
<DialogTrigger render={
<Button variant="outline" size="sm" className="h-7 text-xs">
<Code className="h-3.5 w-3.5 mr-1.5" />
SQL
</Button>
} />
<DialogContent className="sm:max-w-[625px]">
<DialogHeader>
<DialogTitle>Generated SQL Query</DialogTitle>
<DialogDescription>
This is the SQL query generated by the AI to retrieve the data shown below.
</DialogDescription>
</DialogHeader>
<div className="bg-slate-950 text-slate-50 p-4 rounded-md overflow-x-auto relative group">
<pre className="text-sm font-mono">{currentSQL}</pre>
<Button size="icon" variant="secondary" className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 h-6 w-6">
<Code className="h-3 w-3" />
</Button>
</div>
</DialogContent>
</Dialog>
<Button variant="outline" size="sm" className="h-7 text-xs">
<Download className="h-3.5 w-3.5 mr-1.5" />
Export
</Button>
</div>
</div>
{/* Content */}
<div className="flex-1 p-4 overflow-hidden min-h-0">
<Card className="h-full flex flex-col shadow-sm border-muted">
<CardHeader className="pb-2 shrink-0">
2026-03-15 01:29:36 +08:00
<CardTitle>{currentChartSpec?.title || 'Analysis Result'}</CardTitle>
2026-03-15 10:49:37 +08:00
<CardDescription>{currentChartInfo?.reasoning || currentChartSpec?.description || 'Generated from your query'}</CardDescription>
2026-03-14 15:52:27 +08:00
</CardHeader>
<CardContent className="flex-1 min-h-0 p-4">
{view === 'chart' ? (
<div className="h-full w-full">
2026-03-15 01:29:36 +08:00
{currentChartSpec ? (
<VegaChart data={objectRows} spec={currentChartSpec} />
) : (
<div className="flex flex-col items-center justify-center h-full text-muted-foreground">
<ChartIcon className="h-12 w-12 mb-4 opacity-20" />
<p>No chart configuration available for this data.</p>
<Button variant="link" onClick={() => setView('table')}>View Table</Button>
</div>
)}
2026-03-14 15:52:27 +08:00
</div>
) : (
<ScrollArea className="h-full border rounded-md">
<Table>
<TableHeader>
<TableRow>
{columns.map(col => <TableHead key={col}>{col}</TableHead>)}
</TableRow>
</TableHeader>
<TableBody>
{objectRows.map((row, i) => (
<TableRow key={i}>
{columns.map(col => (
<TableCell key={`${i}-${col}`}>{String((row as Record<string, unknown>)[col] ?? "")}</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
</ScrollArea>
)}
</CardContent>
</Card>
</div>
</div>
);
}