ModelingPage.tsx 4.57 KB
/**
 * AI 建模:先是一页「建模卡片」(每次建模一张,看得到状态、产出、用时),点进去才是这次建模的完整工作区
 * (流水线日志 + 草案工作台);新建单独一页。
 */
import { ArrowLeft, Database, FileText, Plus } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Empty, Spinner } from "@/components/ui";
import { go, useHashParams } from "@/lib/nav";
import { onto, type Me, type ModelRunRec } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { loadFailed } from "@/store/useAppStore";
import { NewRun } from "./NewRun";
import { RunWorkspace } from "./RunWorkspace";
import { fmtDur, RUN_STATUS } from "./util";

export function ModelingPage({ me }: { me: Me | null }) {
  const params = useHashParams();
  const runId = params.get("run") || "";
  const isNew = !!params.get("new");
  const canEdit = !!me?.permissions.includes("build");
  const [runs, setRuns] = useState<ModelRunRec[] | null>(null);
  const load = useCallback(() => {
    void onto.modelingRuns().then(setRuns).catch((e) => { setRuns([]); loadFailed(e); });
  }, []);
  useEffect(load, [load]);
  // 有在跑的就定时刷新卡片(状态、耗时)
  const anyRunning = !!runs?.some((r) => r.status === "running");
  useEffect(() => {
    if (!anyRunning || runId || isNew) return undefined;
    const h = window.setInterval(load, 4000);
    return () => window.clearInterval(h);
  }, [anyRunning, runId, isNew, load]);

  if (isNew || runId) {
    return (
      <div className="flex h-full min-h-0 flex-col gap-2">
        <button className="btn btn-ghost btn-sm self-start" onClick={() => go("modeling")}>
          <ArrowLeft className="h-4 w-4" />全部建模
        </button>
        <section className="flex min-h-0 min-w-0 flex-1 flex-col">
          {isNew ? <NewRun canEdit={canEdit} onCreated={load} />
            : <RunWorkspace key={runId} id={runId} canEdit={canEdit} onChanged={load} />}
        </section>
      </div>
    );
  }

  return (
    <div className="h-full overflow-auto pr-1">
      <div className="mb-3 flex items-center gap-2">
        <p className="text-label text-muted">每次建模是一张卡片,点进去看流水线日志和草案,逐条采纳上线。</p>
        {canEdit && (
          <button className="btn btn-primary btn-sm ml-auto" onClick={() => go("modeling", { new: 1 })}>
            <Plus className="h-4 w-4" />新建建模
          </button>
        )}
      </div>
      {!runs ? <div className="p-8"><Spinner /></div> : !runs.length ? (
        <Empty title="还没有建模记录" desc={canEdit ? "点右上角「新建建模」,选文档和 / 或数据库开始。" : undefined} />
      ) : (
        <div className="grid gap-3 sm:grid-cols-2 xl:grid-cols-3">
          {runs.map((r) => <RunCard key={r.id} r={r} />)}
        </div>
      )}
    </div>
  );
}

function RunCard({ r }: { r: ModelRunRec }) {
  const st = RUN_STATUS[r.status] || RUN_STATUS.draft;
  const c = r.counts || {};
  const docs = r.inputs.files.length + r.inputs.doc_ids.length;
  const nums: [string, number][] = [["对象", c.object_type ?? 0], ["关系", c.link_type ?? 0], ["函数", c.function ?? 0],
    ["动作", c.action ?? 0], ["流程卡", c.sop ?? 0]];
  return (
    <button onClick={() => go("modeling", { run: r.id })}
            className="card flex flex-col gap-2 p-4 text-left transition-shadow hover:shadow-md">
      <div className="flex items-start gap-2">
        <span className="min-w-0 flex-1 break-words text-body font-semibold">{r.title}</span>
        <Badge variant={st.variant}>{st.label}</Badge>
      </div>
      <p className="flex items-center gap-1.5 text-caption text-muted">
        {docs ? <FileText className="h-3.5 w-3.5" /> : <Database className="h-3.5 w-3.5" />}
        {docs ? `${docs} 份文档` : "只用数据库"}
        {(r.options?.ds_ids || []).length ? ` · ${(r.options.ds_ids || []).length} 个数据库` : ""}
      </p>
      {r.status === "done" ? (
        <div className="grid grid-cols-5 gap-1 rounded-lg bg-ink/[0.03] px-2 py-2 text-center">
          {nums.map(([k, n]) => (
            <span key={k}><span className="block text-section font-semibold tabular-nums">{n}</span>
              <span className="text-caption text-muted">{k}</span></span>
          ))}
        </div>
      ) : r.error ? <p className="line-clamp-2 text-caption text-bad">{r.error}</p> : null}
      <p className={cn("mt-auto text-caption text-muted")}>
        {r.created_at.replace("T", " ").slice(0, 16)}{r.elapsed_ms ? ` · 用时 ${fmtDur(r.elapsed_ms)}` : ""}
      </p>
    </button>
  );
}