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"; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, LineChart, Line } from 'recharts'; import { Code, Table as TableIcon, BarChart as ChartIcon, LineChart as LineChartIcon, Download, LayoutDashboard, Loader2 } from "lucide-react"; import { ScrollArea } from "@/components/ui/scroll-area"; import { useDashboardStore } from "@/store/dashboardStore"; import { useVisualizationStore } from "@/store/visualizationStore"; export function VisualizationPanel() { const [view, setView] = useState<'table' | 'chart'>('chart'); const [chartType, setChartType] = useState<'bar' | 'line'>('bar'); const { addChart } = useDashboardStore(); const { currentData, currentSQL, isLoading, error } = useVisualizationStore(); const handleAddToDashboard = () => { if (!currentData || !currentSQL) return; addChart({ id: Date.now().toString(), title: 'Generated Analysis', // Could be dynamic based on query type: chartType, data: currentData, sql: currentSQL, }); alert("Added to Dashboard!"); }; if (isLoading) { return (
Generating visualization...
); } if (error) { return (
Visualization Error
{error}
) } if (!currentData || currentData.length === 0) { return (

No data to visualize.

Ask the chat to generate some insights!

); } const objectRows = currentData.filter((row) => row && typeof row === "object" && !Array.isArray(row)); if (objectRows.length === 0) { return (

Data format is not supported for visualization.

); } const columns = Object.keys(objectRows[0] as Record); const firstRow = objectRows[0] as Record; const stringColumn = columns.find(col => typeof firstRow[col] === 'string') || columns[0]; const numberColumns = columns.filter(col => typeof firstRow[col] === 'number'); return (
{/* Toolbar */}

Visualization

{view === 'chart' && (
)} SQL } /> Generated SQL Query This is the SQL query generated by the AI to retrieve the data shown below.
{currentSQL}
{/* Content */}
Analysis Result Generated from your query {view === 'chart' ? (
{chartType === 'bar' ? ( {numberColumns.map((col, idx) => ( ))} ) : ( {numberColumns.map((col, idx) => ( ))} )}
) : ( {columns.map(col => {col})} {objectRows.map((row, i) => ( {columns.map(col => ( {String((row as Record)[col] ?? "")} ))} ))}
)}
); }