2026-03-22 16:26:23 +08:00
|
|
|
import { useState, useEffect } from "react";
|
2026-03-15 11:13:40 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
2026-03-21 21:26:57 +08:00
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
2026-03-15 18:11:26 +08:00
|
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, DialogDescription, DialogFooter } from "@/components/ui/dialog";
|
2026-03-22 16:26:23 +08:00
|
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
2026-03-15 11:13:40 +08:00
|
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
2026-03-18 10:57:48 +08:00
|
|
|
import { Code, Table as TableIcon, BarChart as ChartIcon, LayoutDashboard, Copy, Check } from "lucide-react";
|
2026-03-15 11:13:40 +08:00
|
|
|
import { ScrollArea } from "@/components/ui/scroll-area";
|
2026-03-15 18:11:26 +08:00
|
|
|
import { useDashboardStore, type ChartConfig } from "@/store/dashboardStore";
|
2026-03-16 16:12:35 +08:00
|
|
|
import { useProjectStore } from "@/store/projectStore";
|
2026-03-21 21:26:57 +08:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-03-15 11:13:40 +08:00
|
|
|
import type { ChartSpec } from "@/store/visualizationStore";
|
|
|
|
|
import { VegaChart } from "./VegaChart";
|
2026-03-18 10:57:48 +08:00
|
|
|
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
|
|
|
|
|
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';
|
|
|
|
|
import { format } from 'sql-formatter';
|
2026-03-15 11:13:40 +08:00
|
|
|
|
|
|
|
|
interface InlineVisualizationCardProps {
|
|
|
|
|
viz: {
|
|
|
|
|
sql: string;
|
|
|
|
|
rows: unknown[];
|
|
|
|
|
chartSpec: ChartSpec | null;
|
|
|
|
|
canVisualize: boolean;
|
|
|
|
|
reasoning?: string;
|
|
|
|
|
error?: string | null;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function InlineVisualizationCard({ viz }: InlineVisualizationCardProps) {
|
2026-03-21 21:26:57 +08:00
|
|
|
const { t } = useTranslation();
|
2026-03-15 11:13:40 +08:00
|
|
|
const [view, setView] = useState<'table' | 'chart'>('chart');
|
2026-03-15 18:11:26 +08:00
|
|
|
const [confirmOpen, setConfirmOpen] = useState(false);
|
2026-03-18 10:57:48 +08:00
|
|
|
const [copied, setCopied] = useState(false);
|
2026-03-15 18:11:26 +08:00
|
|
|
const [pendingChart, setPendingChart] = useState<Omit<ChartConfig, 'layout'> | null>(null);
|
2026-03-22 16:26:23 +08:00
|
|
|
const [selectedDashboardId, setSelectedDashboardId] = useState<string>('');
|
|
|
|
|
const { dashboards, addChart, loadDashboards } = useDashboardStore();
|
2026-03-16 16:12:35 +08:00
|
|
|
const { currentProject } = useProjectStore();
|
2026-03-15 11:13:40 +08:00
|
|
|
const objectRows = viz.rows.filter((row) => row && typeof row === "object" && !Array.isArray(row)) as Record<string, unknown>[];
|
|
|
|
|
const columns = objectRows.length > 0 ? Object.keys(objectRows[0]) : [];
|
|
|
|
|
|
2026-03-22 16:26:23 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (currentProject) {
|
|
|
|
|
loadDashboards(currentProject.id);
|
|
|
|
|
}
|
|
|
|
|
}, [currentProject, loadDashboards]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (dashboards.length > 0 && !selectedDashboardId) {
|
|
|
|
|
setSelectedDashboardId(dashboards[0].id);
|
|
|
|
|
}
|
|
|
|
|
}, [dashboards, selectedDashboardId]);
|
|
|
|
|
|
2026-03-15 18:11:26 +08:00
|
|
|
const buildPendingChart = (): Omit<ChartConfig, 'layout'> => {
|
2026-03-17 11:47:30 +08:00
|
|
|
if (view === "table") {
|
|
|
|
|
return {
|
|
|
|
|
id: Date.now().toString(),
|
|
|
|
|
title: viz.chartSpec?.title || "Generated Analysis",
|
|
|
|
|
type: "table",
|
|
|
|
|
data: objectRows,
|
|
|
|
|
sql: viz.sql,
|
|
|
|
|
chartSpec: null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-03-15 11:13:40 +08:00
|
|
|
const mark = viz.chartSpec?.mark;
|
|
|
|
|
const markType = typeof mark === "string" ? mark : mark?.type;
|
|
|
|
|
const dashboardType = markType === "line" ? "line" : "bar";
|
2026-03-15 18:11:26 +08:00
|
|
|
return {
|
2026-03-15 11:13:40 +08:00
|
|
|
id: Date.now().toString(),
|
|
|
|
|
title: viz.chartSpec?.title || "Generated Analysis",
|
|
|
|
|
type: dashboardType,
|
|
|
|
|
data: objectRows,
|
|
|
|
|
sql: viz.sql,
|
2026-03-15 17:57:09 +08:00
|
|
|
chartSpec: viz.chartSpec,
|
2026-03-15 18:11:26 +08:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleAddToDashboard = () => {
|
2026-03-16 16:12:35 +08:00
|
|
|
if (!currentProject) return;
|
2026-03-15 18:11:26 +08:00
|
|
|
const chart = buildPendingChart();
|
|
|
|
|
setPendingChart(chart);
|
|
|
|
|
setConfirmOpen(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleConfirmAdd = () => {
|
2026-03-22 16:26:23 +08:00
|
|
|
if (!pendingChart || !currentProject || !selectedDashboardId) return;
|
|
|
|
|
addChart(pendingChart, selectedDashboardId, currentProject.id);
|
2026-03-15 18:11:26 +08:00
|
|
|
setConfirmOpen(false);
|
|
|
|
|
setPendingChart(null);
|
2026-03-15 11:13:40 +08:00
|
|
|
};
|
|
|
|
|
|
2026-03-18 10:57:48 +08:00
|
|
|
const handleCopySql = () => {
|
|
|
|
|
navigator.clipboard.writeText(viz.sql || "");
|
|
|
|
|
setCopied(true);
|
|
|
|
|
setTimeout(() => setCopied(false), 2000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const formattedSql = viz.sql ? format(viz.sql, { language: 'postgresql' }) : "--";
|
|
|
|
|
|
2026-03-15 11:13:40 +08:00
|
|
|
if (viz.error) {
|
|
|
|
|
return <div className="text-sm text-red-500">{viz.error}</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-03-28 16:25:35 +08:00
|
|
|
<Card className="w-full border border-border shadow-none">
|
2026-03-15 11:13:40 +08:00
|
|
|
<CardHeader className="pb-2">
|
2026-03-21 21:26:57 +08:00
|
|
|
<CardTitle className="text-base">{viz.chartSpec?.title || t('visualizationResult')}</CardTitle>
|
2026-03-15 11:13:40 +08:00
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent className="pt-0">
|
|
|
|
|
<div className="flex items-center justify-between mb-3">
|
2026-03-28 16:25:35 +08:00
|
|
|
<div className="flex bg-muted rounded-md p-1">
|
2026-03-15 11:13:40 +08:00
|
|
|
<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>
|
|
|
|
|
<Dialog>
|
|
|
|
|
<DialogTrigger render={
|
|
|
|
|
<Button variant="ghost" size="sm" className="h-7 px-3 text-xs">
|
|
|
|
|
<Code className="h-3.5 w-3.5 mr-1.5" />
|
|
|
|
|
SQL
|
|
|
|
|
</Button>
|
|
|
|
|
} />
|
2026-03-18 10:57:48 +08:00
|
|
|
<DialogContent className="sm:max-w-[700px]">
|
|
|
|
|
<DialogHeader className="flex flex-row items-start justify-between pr-8">
|
|
|
|
|
<div>
|
|
|
|
|
<DialogTitle>Generated SQL Query</DialogTitle>
|
2026-03-21 21:26:57 +08:00
|
|
|
<DialogDescription className="mt-1">{t('sqlQueryDescription')}</DialogDescription>
|
2026-03-18 10:57:48 +08:00
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
className="h-8 gap-1.5 shrink-0"
|
|
|
|
|
onClick={handleCopySql}
|
|
|
|
|
>
|
|
|
|
|
{copied ? (
|
|
|
|
|
<>
|
|
|
|
|
<Check className="h-3.5 w-3.5 text-emerald-500" />
|
2026-03-21 21:26:57 +08:00
|
|
|
<span>{t('copied')}</span>
|
2026-03-18 10:57:48 +08:00
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Copy className="h-3.5 w-3.5" />
|
2026-03-21 21:26:57 +08:00
|
|
|
<span>{t('copy')}</span>
|
2026-03-18 10:57:48 +08:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
2026-03-15 11:13:40 +08:00
|
|
|
</DialogHeader>
|
2026-03-28 16:25:35 +08:00
|
|
|
<div className="relative rounded-md overflow-hidden bg-[#1e1e1e] border border-border shadow-inner mt-2">
|
2026-03-18 10:57:48 +08:00
|
|
|
<ScrollArea className="max-h-[500px]">
|
|
|
|
|
<SyntaxHighlighter
|
|
|
|
|
language="sql"
|
|
|
|
|
style={vscDarkPlus}
|
|
|
|
|
customStyle={{
|
|
|
|
|
margin: 0,
|
|
|
|
|
padding: '1.25rem',
|
|
|
|
|
fontSize: '0.875rem',
|
|
|
|
|
lineHeight: '1.5',
|
|
|
|
|
background: 'transparent',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{formattedSql}
|
|
|
|
|
</SyntaxHighlighter>
|
|
|
|
|
</ScrollArea>
|
2026-03-15 11:13:40 +08:00
|
|
|
</div>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
2026-03-22 16:26:23 +08:00
|
|
|
<Button variant="outline" size="sm" className="h-7 text-xs" onClick={handleAddToDashboard} disabled={objectRows.length === 0 || dashboards.length === 0}>
|
2026-03-15 11:13:40 +08:00
|
|
|
<LayoutDashboard className="h-3.5 w-3.5 mr-1.5" />
|
|
|
|
|
Add to Dashboard
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{view === "chart" ? (
|
2026-03-19 12:27:31 +08:00
|
|
|
viz.chartSpec && objectRows.length > 0 ? (
|
2026-03-28 16:25:35 +08:00
|
|
|
<div className="w-full h-80 min-h-[320px] rounded-xl border border-border p-2">
|
2026-03-15 11:13:40 +08:00
|
|
|
<VegaChart data={objectRows} spec={viz.chartSpec} />
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-03-28 16:25:35 +08:00
|
|
|
<div className="text-sm text-muted-foreground">{t('resultNotSuitableForChart')}</div>
|
2026-03-15 11:13:40 +08:00
|
|
|
)
|
|
|
|
|
) : objectRows.length > 0 ? (
|
|
|
|
|
<ScrollArea className="h-80 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[col] ?? "")}</TableCell>
|
|
|
|
|
))}
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</ScrollArea>
|
|
|
|
|
) : (
|
2026-03-28 16:25:35 +08:00
|
|
|
<div className="text-sm text-muted-foreground">{t('noStructuredDataToRender')}</div>
|
2026-03-15 11:13:40 +08:00
|
|
|
)}
|
|
|
|
|
</CardContent>
|
2026-03-15 18:11:26 +08:00
|
|
|
<Dialog open={confirmOpen} onOpenChange={setConfirmOpen}>
|
|
|
|
|
<DialogContent className="sm:max-w-md">
|
|
|
|
|
<DialogHeader>
|
2026-03-22 16:26:23 +08:00
|
|
|
<DialogTitle>{t('pinChartToDashboard')}</DialogTitle>
|
2026-03-15 18:11:26 +08:00
|
|
|
<DialogDescription>
|
2026-03-22 16:26:23 +08:00
|
|
|
{t('selectDashboardToPin')}
|
2026-03-15 18:11:26 +08:00
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
2026-03-22 16:26:23 +08:00
|
|
|
<div className="py-4">
|
|
|
|
|
<label className="text-sm font-medium mb-2 block">{t('dashboardMenu')}</label>
|
|
|
|
|
<Select value={selectedDashboardId} onValueChange={(val) => { if (val) setSelectedDashboardId(val); }}>
|
|
|
|
|
<SelectTrigger>
|
|
|
|
|
<SelectValue placeholder={t('selectDashboard')}>
|
|
|
|
|
{dashboards.find(d => d.id === selectedDashboardId)?.name || t('selectDashboard')}
|
|
|
|
|
</SelectValue>
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{dashboards.map(d => (
|
|
|
|
|
<SelectItem key={d.id} value={d.id}>{d.name}</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
2026-03-15 18:11:26 +08:00
|
|
|
<DialogFooter>
|
|
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setConfirmOpen(false);
|
|
|
|
|
setPendingChart(null);
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-03-21 21:26:57 +08:00
|
|
|
{t('cancel')}
|
2026-03-15 18:11:26 +08:00
|
|
|
</Button>
|
2026-03-22 16:26:23 +08:00
|
|
|
<Button onClick={handleConfirmAdd} disabled={!selectedDashboardId}>
|
|
|
|
|
{t('submit')}
|
2026-03-15 18:11:26 +08:00
|
|
|
</Button>
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</Dialog>
|
2026-03-15 11:13:40 +08:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|