CheckView.tsx 11.1 KB
/**
 * 对账:文档里的每一条要求(「列条目」拆出来的),草案有没有落实、落实得对不对。
 * 默认只看要处理的(有出入、漏了),每条写清「文档要求 / 草案现在 / 差在哪」和建议怎么改,可以让 AI 补上或忽略;
 * 已落实的、平台本来就有的(审批留痕、问数…)、忽略了的放在别的页签里。
 * 「上线后试问」是按文档里「谁关心什么」改写的问题:采纳上线后点一下到问数里问,看答得对不对。
 */
import { CheckCircle2, ClipboardCheck, EyeOff, MessageSquare, RotateCcw, Sparkles, Wand2 } from "lucide-react";
import { useMemo, useState } from "react";
import { Tabs } from "@/components/kit";
import { Badge, Empty, Spinner } from "@/components/ui";
import { go } from "@/lib/nav";
import { errText, onto, type CheckStatus, type ModelCheckItem, type ModelRunFull } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";

const STATUS: Record<CheckStatus, { label: string; variant: "danger" | "warn" | "ok" | "neutral" | "info" }> = {
  missing: { label: "漏了", variant: "danger" },
  partial: { label: "有出入", variant: "warn" },
  covered: { label: "已落实", variant: "ok" },
  builtin: { label: "不用建模", variant: "info" },
  na: { label: "背景说明", variant: "neutral" },
};
const FIX_TYPE: Record<string, string> = { modify: "改草案", add: "补一个元素", sop: "改流程卡", watch: "改监控" };
type Sub = "todo" | "covered" | "builtin" | "ignored" | "ask";

export function CheckView({ run, canEdit, onJump, onChanged }: {
  run: ModelRunFull; canEdit: boolean; onJump: (name: string) => void; onChanged: () => void;
}) {
  const check = run.check || {};
  const items = check.items || [];
  const [sub, setSub] = useState<Sub>("todo");
  const [busy, setBusy] = useState("");
  const fixing = run.status === "running" && run.stages.find((s) => s.key === "fix")?.status === "running";
  const groups = useMemo(() => ({
    todo: items.filter((x) => (x.status === "partial" || x.status === "missing") && !x.ignored)
      .sort((a, b) => (a.status === "missing" ? 0 : 1) - (b.status === "missing" ? 0 : 1)),
    covered: items.filter((x) => x.status === "covered" && !x.ignored),
    builtin: items.filter((x) => (x.status === "builtin" || x.status === "na") && !x.ignored),
    ignored: items.filter((x) => x.ignored),
  }), [items]);
  const fixable = groups.todo.filter((x) => x.fix?.type && x.fix.type !== "none");
  const live = Object.values(run.items).flat().some((x) => x.concept?.status === "published");
  const questions = check.questions || [];

  if (!items.length) {
    return <div className="p-6"><Empty icon={<ClipboardCheck className="h-9 w-9" />} title="还没有对账结果" desc="「对账」这一步跑完后显示在这里。" /></div>;
  }
  const fix = async (ids?: string[]) => {
    setBusy(ids ? ids[0] : "all");
    try {
      await onto.fixModelingCheck(run.id, ids);
      toast("ok", "AI 开始按对账修改", "只改草案、不上线;改完会再核对一次,进度看流水线「按对账修改」这一步");
      onChanged();
    } catch (e) { toast("error", "没开始", errText(e)); } finally { setBusy(""); }
  };
  const ignore = async (x: ModelCheckItem, on: boolean) => {
    try { await onto.ignoreModelingCheck(run.id, x.id, on); onChanged(); } catch (e) { toast("error", errText(e)); }
  };

  return (
    <div className="flex h-full flex-col">
      <div className="flex flex-wrap items-center gap-2 border-b border-line px-4 py-2">
        <Tabs className="border-b-0" value={sub} onChange={(k) => setSub(k as Sub)}
              options={[
                { key: "todo", label: <span className={cn(groups.todo.length && "text-bad")}>要处理</span>, count: groups.todo.length },
                { key: "covered", label: "已落实", count: groups.covered.length },
                { key: "builtin", label: "不用建模", count: groups.builtin.length },
                ...(groups.ignored.length ? [{ key: "ignored" as Sub, label: "已忽略", count: groups.ignored.length }] : []),
                { key: "ask", label: "上线后试问", count: questions.length || null },
              ]} />
        <span className="flex-1" />
        {fixing && <span className="flex items-center gap-1.5 text-label text-brand"><Spinner className="h-3.5 w-3.5" />AI 正在按对账修改…</span>}
        {canEdit && sub === "todo" && fixable.length > 0 && !fixing && (
          <button className="btn btn-primary btn-sm" disabled={!!busy || run.status === "running"} onClick={() => void fix()}
                  title="把能改的都交给 AI 改(只改草案,不上线),改完再核对一次">
            {busy === "all" ? <Spinner className="h-3.5 w-3.5" /> : <Wand2 className="h-3.5 w-3.5" />}让 AI 把能改的都改一遍({fixable.length})</button>
        )}
      </div>
      <div className="min-h-0 flex-1 space-y-2 overflow-auto p-4">
        {sub === "todo" && (groups.todo.length
          ? groups.todo.map((x) => <IssueCard key={x.id} x={x} canEdit={canEdit && !fixing && run.status !== "running"}
                                              busy={busy === x.id} onFix={() => void fix([x.id])}
                                              onIgnore={() => void ignore(x, true)} onJump={onJump} />)
          : <Empty icon={<CheckCircle2 className="h-9 w-9" />} title="没有要处理的了" desc="文档里的要求都落实了,或者平台本来就有。" />)}
        {sub === "covered" && <Plain items={groups.covered} onJump={onJump} />}
        {sub === "builtin" && <Plain items={groups.builtin} onJump={onJump} why />}
        {sub === "ignored" && groups.ignored.map((x) => (
          <div key={x.id} className="flex items-start gap-2 rounded-lg border border-line px-3 py-2 text-label">
            <span className="text-muted">{x.id}</span><span className="flex-1">{x.text}</span>
            {canEdit && <button className="text-caption text-brand" onClick={() => void ignore(x, false)}><RotateCcw className="inline h-3 w-3" />恢复</button>}
          </div>
        ))}
        {sub === "ask" && (
          <section className="space-y-2">
            <p className="text-label text-muted">按文档里「谁关心什么」改写的问题:采纳上线以后,点一下到问数里问,看答得对不对 ——
              答不上来的,说明还缺元素或口径不对。{!live && <b className="text-warn">现在还没有元素上线,先采纳再来问。</b>}</p>
            {questions.length ? questions.map((q, i) => (
              <div key={i} className="flex items-center gap-2 rounded-lg border border-line px-3 py-2 text-label">
                <MessageSquare className="h-3.5 w-3.5 shrink-0 text-muted" /><span className="flex-1">{q}</span>
                <button className="btn btn-sm" disabled={!live} onClick={() => go("ask", { q })}>去问数里问</button>
              </div>
            )) : <p className="text-label text-muted">文档里没写谁关心什么</p>}
          </section>
        )}
      </div>
    </div>
  );
}

function Names({ by, onJump }: { by: string[]; onJump: (n: string) => void }) {
  if (!by?.length) return null;
  return <span className="inline-flex flex-wrap gap-1">{by.map((n) => (
    <button key={n} className="rounded bg-brand/[0.07] px-1.5 text-caption text-brand hover:underline" onClick={() => onJump(n)}>{n}</button>
  ))}</span>;
}

function IssueCard({ x, canEdit, busy, onFix, onIgnore, onJump }: {
  x: ModelCheckItem; canEdit: boolean; busy: boolean; onFix: () => void; onIgnore: () => void; onJump: (n: string) => void;
}) {
  const st = STATUS[x.status];
  const canFix = !!x.fix?.type && x.fix.type !== "none";
  return (
    <div className={cn("rounded-lg border px-3 py-2.5", x.status === "missing" ? "border-bad/30" : "border-warn/40")}>
      <div className="flex flex-wrap items-center gap-2">
        <Badge variant={st.variant}>{st.label}</Badge>
        <span className="text-caption text-muted">{x.id} · {x.kind_label || ""}{x.section ? ` · ${x.section}` : ""}</span>
        <span className="flex-1" />
        {canEdit && canFix && (
          <button className="btn btn-sm" disabled={busy} onClick={onFix} title={x.fix?.instruction}>
            {busy ? <Spinner className="h-3.5 w-3.5" /> : <Wand2 className="h-3.5 w-3.5" />}让 AI 补上</button>
        )}
        {canEdit && <button className="btn btn-ghost btn-sm" onClick={onIgnore} title="这条不用管(比如文档写得不严谨,或者以后再说)">
          <EyeOff className="h-3.5 w-3.5" />忽略</button>}
      </div>
      <p className="mt-1.5 text-body font-medium">{x.text}</p>
      {(x.doc_says || x.draft_has) && (
        <div className="mt-1.5 grid gap-2 text-label sm:grid-cols-2">
          <div className="rounded-md bg-ink/[0.03] px-2.5 py-1.5"><p className="text-caption text-muted">文档要求</p>{x.doc_says || x.quote || x.text}</div>
          <div className="rounded-md bg-ink/[0.03] px-2.5 py-1.5"><p className="text-caption text-muted">草案现在</p>
            {x.draft_has || (x.status === "missing" ? "没有对应的元素" : "—")}</div>
        </div>
      )}
      {x.gap && <p className="mt-1.5 text-label"><b className={x.status === "missing" ? "text-bad" : "text-[#b45309]"}>差在哪:</b>{x.gap}</p>}
      {!x.gap && x.note && <p className="mt-1.5 text-label text-muted">{x.note}</p>}
      {canFix && <p className="mt-1 text-caption text-muted">建议{FIX_TYPE[x.fix!.type!] ? `(${FIX_TYPE[x.fix!.type!]}${x.fix?.target ? `「${x.fix.target}」` : x.fix?.name ? `「${x.fix.name}」` : ""})` : ""}:{x.fix?.instruction}</p>}
      {x.fix?.type === "none" && <p className="mt-1 text-caption text-muted">这条要人拍板:{x.fix.instruction || "AI 改不了"}</p>}
      {x.fixed && (
        <p className={cn("mt-1 flex items-start gap-1 text-caption", x.fixed.ok ? "text-brand" : "text-muted")}>
          <Sparkles className="mt-0.5 h-3 w-3 shrink-0" />AI 改过「{x.fixed.target}」:{x.fixed.summary},复核后还是{STATUS[x.status].label}</p>
      )}
      {x.by?.length > 0 && <p className="mt-1 text-caption text-muted">对应的草案:<Names by={x.by} onJump={onJump} /></p>}
    </div>
  );
}

function Plain({ items, onJump, why }: { items: ModelCheckItem[]; onJump: (n: string) => void; why?: boolean }) {
  if (!items.length) return <p className="text-label text-muted">没有</p>;
  return (
    <div className="overflow-x-auto rounded-lg border border-line">
      <table className="tbl text-label">
        <thead><tr><th>#</th><th>文档条目</th><th>{why ? "为什么不用建模" : "落实它的草案"}</th></tr></thead>
        <tbody>{items.map((x) => (
          <tr key={x.id}>
            <td className="whitespace-nowrap text-muted">{x.id}</td>
            <td className="min-w-[14rem] break-words">{x.text}
              {x.fixed?.ok && <span className="ml-1 text-caption text-brand">(AI 按对账改好的)</span>}</td>
            <td className="break-words">{why ? <span className="text-muted">{x.reason || STATUS[x.status].label}</span> : <Names by={x.by} onJump={onJump} />}</td>
          </tr>
        ))}</tbody>
      </table>
    </div>
  );
}