SqlPage.tsx 3.89 KB
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>
  );
}