SqlPage.tsx
3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { BookmarkCheck, Database, Gauge, MessagesSquare, Table2 } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { api } from "@/lib/api";
import { cn } from "@/lib/utils";
import { Card, Empty, Segmented, Spinner } from "@/components/ui";
import type { DataSource } from "@/lib/types";
import { AskPanel } from "./AskPanel";
import { DataSourcePanel } from "./DataSourcePanel";
import { LibraryPanel } from "./LibraryPanel";
import { MetricsPanel } from "./MetricsPanel";
import { ModelingPanel } from "./ModelingPanel";
type Tab = "ask" | "sources" | "modeling" | "metrics" | "library";
const TABS: { key: Tab; label: string; icon: React.ReactNode }[] = [
{ key: "ask", label: "问答", icon: <MessagesSquare className="h-3.5 w-3.5" /> },
{ key: "sources", label: "数据源", icon: <Database className="h-3.5 w-3.5" /> },
{ key: "modeling", label: "建模", icon: <Table2 className="h-3.5 w-3.5" /> },
{ key: "metrics", label: "指标与规则", icon: <Gauge className="h-3.5 w-3.5" /> },
{ key: "library", label: "沉淀库", icon: <BookmarkCheck className="h-3.5 w-3.5" /> },
];
export function SqlPage() {
const [tab, setTab] = useState<Tab>("ask");
const [sources, setSources] = useState<DataSource[] | null>(null);
const [dsId, setDsId] = useState<number | null>(null);
const load = useCallback(() => {
api.datasources().then((d) => {
setSources(d);
setDsId((cur) => (cur && d.some((x) => x.id === cur) ? cur : d[0]?.id ?? null));
}).catch(() => setSources([]));
}, []);
useEffect(load, [load]);
// 一个数据源都没有时,直接把人带到数据源页 —— 其余几个页签这时都是空的
useEffect(() => {
if (sources && sources.length === 0) setTab("sources");
}, [sources]);
const current = sources?.find((s) => s.id === dsId) ?? null;
return (
<div className="flex h-full min-h-0 flex-col gap-3">
<div className="flex flex-wrap items-center gap-2">
<Segmented<Tab> value={tab} onChange={setTab}
options={TABS.map((t) => ({
key: t.key,
label: <span className="inline-flex items-center gap-1.5">{t.icon}{t.label}</span>,
}))} />
<span className="flex-1" />
{sources && sources.length > 0 && (
<label className="flex items-center gap-1.5 text-[12.5px] text-muted">
数据源
<select className="input w-auto py-1" value={dsId ?? ""}
onChange={(e) => setDsId(Number(e.target.value))}>
{sources.map((s) => (
<option key={s.id} value={s.id}>{s.name}</option>
))}
</select>
</label>
)}
</div>
{/* 问答页自己管滚动(对话区滚、输入框钉在底下),别再套一层外部滚动条 */}
<div className={cn("min-h-0 flex-1", tab === "ask" ? "overflow-hidden" : "overflow-auto")}>
{!sources ? (
<Card className="p-4"><Spinner /></Card>
) : tab === "sources" ? (
<DataSourcePanel selectedId={dsId} onSelect={setDsId} onChanged={load} />
) : !current ? (
<Card>
<Empty icon={<Database className="h-6 w-6" />} title="还没有数据源"
desc="先到「数据源」页添加一个 MySQL 连接。"
action={
<button className="btn btn-primary btn-sm" onClick={() => setTab("sources")}>
去添加
</button>
} />
</Card>
) : tab === "ask" ? (
<AskPanel dsId={current.id} dsName={current.name} />
) : tab === "modeling" ? (
<ModelingPanel dsId={current.id} />
) : tab === "metrics" ? (
<MetricsPanel dsId={current.id} />
) : (
<LibraryPanel dsId={current.id} />
)}
</div>
</div>
);
}