WatchDraftView.tsx
11.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* 定时任务 / 监控草稿:AI 按文档里写的执行时间(「每天 9 点巡检」)和盯防条件(「低于某个数就通知某角色」)配好的,
* 采纳时直接建成监控并启用。这里说清楚什么时候跑、看什么、命中了做什么、依据的原文,
* 以及按今天的数据扫一遍会命中几条;采纳之前可以直接改,也可以一句话让 AI 改。
*/
import { AlarmClock, AlertTriangle, Check, ExternalLink, Pencil, Save, X } from "lucide-react";
import { useState } from "react";
import { Badge, Spinner } from "@/components/ui";
import { go, here } from "@/lib/nav";
import { errText, onto, type ModelWatchDraft } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { AiEditBox, TrialLine } from "./parts";
const WEEKDAY = ["", "周一", "周二", "周三", "周四", "周五", "周六", "周日"];
export function whenText(w: ModelWatchDraft): string {
const s = w.body.schedule || { kind: "daily" };
if (s.kind === "interval") return `每 ${s.every_minutes ?? 60} 分钟`;
if (s.kind === "weekly") return `每${WEEKDAY[s.weekday ?? 1] || "周"} ${s.at || "08:00"}`;
if (s.kind === "monthly") return `每月 ${s.day ?? 1} 日 ${s.at || "08:00"}`;
if (s.kind === "cron") return `按 cron「${s.cron}」`;
return `每天 ${s.at || "08:00"}`;
}
export function whatText(w: ModelWatchDraft): string {
const src = w.body.source;
if (!src) return "";
if (src.kind === "sop") return `跑流程卡「${src.sop}」`;
if (src.kind === "object") return `逐个看「${src.object_type}」`;
return `看指标「${src.metric}」${(src.dimensions || []).length ? `(按${src.dimensions!.join("、")}拆开)` : ""}`;
}
function strategyText(s: NonNullable<ModelWatchDraft["body"]["strategies"]>[number]): string {
switch (s.kind) {
case "run_sop": return `启动流程卡「${s.sop}」`;
case "notify": return `通知${s.role}:${s.subject || ""}`;
case "task": return `派待办给${s.role}:${s.title || ""}`;
case "propose_action": return `提交动作「${s.action}」(按动作的审批方式走)`;
default: return s.kind;
}
}
export function watchState(w: ModelWatchDraft): { label: string; variant: "ok" | "warn" | "neutral" } {
if (w.status === "accepted") return { label: "已建好", variant: "ok" };
if (w.status === "discarded") return { label: "不要", variant: "neutral" };
return { label: "待定", variant: "warn" };
}
export function WatchRow({ w, active, picked, canPick, onPick, onOpen }: {
w: ModelWatchDraft; active: boolean; picked: boolean; canPick: boolean; onPick: (on: boolean) => void; onOpen: () => void;
}) {
const st = watchState(w);
return (
<div onClick={onOpen}
className={cn("flex cursor-pointer items-start gap-2 border-b border-line/60 px-3 py-2 hover:bg-ink/[0.03]",
active && "bg-brand/[0.07] hover:bg-brand/[0.07]")}>
<input type="checkbox" className="mt-1" disabled={!canPick} checked={picked}
onClick={(e) => e.stopPropagation()} onChange={(e) => onPick(e.target.checked)} />
<div className="min-w-0 flex-1">
<p className="break-words text-body font-medium">{w.title}</p>
<p className="mt-0.5 text-caption text-muted">{whenText(w)} · {whatText(w)}</p>
<p className="mt-1 flex flex-wrap items-center gap-1">
<Badge variant={st.variant}>{st.label}</Badge>
{w.status === "pending" && w.issue && <Badge variant="danger" title={w.issue}>有问题</Badge>}
{w.status === "pending" && !w.issue && w.trial?.ok && w.body.source?.kind !== "sop" && (
<span className="text-caption text-muted">今天会命中 {(w.trial as any).hits ?? 0} 条</span>
)}
</p>
</div>
</div>
);
}
export function WatchDraftView({ w, runId, canEdit, busy, onAdopt, onDiscard, onChanged }: {
w: ModelWatchDraft; runId: string; canEdit: boolean; busy: boolean; onAdopt: () => void; onDiscard: () => void;
onChanged: () => void;
}) {
const [editing, setEditing] = useState(false);
const st = watchState(w);
const b = w.body;
const src = b.source;
return (
<div className="space-y-4 p-4">
<div>
<div className="flex flex-wrap items-center gap-2">
<Badge variant="info"><AlarmClock className="h-3 w-3" />定时与监控</Badge>
<h3 className="min-w-0 break-words text-section font-semibold">{w.title}</h3>
<Badge variant={st.variant}>{st.label}</Badge>
</div>
{b.description && <p className="mt-2 break-words text-body">{b.description}</p>}
<p className="mt-1 text-caption text-muted">负责角色:{b.owner_role || "没指定"}</p>
</div>
{w.issue && (
<p className="flex items-start gap-1.5 rounded-lg border border-bad/30 bg-bad/5 px-3 py-2 text-label text-bad">
<AlertTriangle className="mt-0.5 h-4 w-4 shrink-0" /><span className="break-words">{w.issue}</span></p>
)}
{canEdit && (
<div className="flex flex-wrap gap-1.5">
{w.status === "pending" && <>
<button className="btn btn-primary btn-sm" disabled={busy} onClick={onAdopt}
title="建成监控并启用;它引用的流程卡、指标、对象要一起采纳(或已经上线)">
{busy ? <Spinner className="h-3.5 w-3.5" /> : <Check className="h-3.5 w-3.5" />}采纳并启用</button>
<button className="btn btn-sm" disabled={busy} onClick={() => setEditing(!editing)}><Pencil className="h-3.5 w-3.5" />{editing ? "收起编辑" : "编辑"}</button>
<button className="btn btn-sm" disabled={busy} onClick={onDiscard}><X className="h-3.5 w-3.5" />不要</button>
</>}
{w.watch_id && <button className="btn btn-sm" onClick={() => go("watch", { id: w.watch_id, back: here() })}>
<ExternalLink className="h-3.5 w-3.5" />到「监控与定时」里看</button>}
</div>
)}
{editing && w.status === "pending" && (
<WatchEdit w={w} runId={runId} onDone={() => { setEditing(false); onChanged(); }} onCancel={() => setEditing(false)} />
)}
{w.status === "pending" && src?.kind !== "sop" && w.trial && <TrialLine t={w.trial} label="按今天的数据扫了一遍" />}
{(w.req_ids || []).length > 0 && <p className="text-caption text-muted">落实文档条目 {w.req_ids!.join("、")}</p>}
<section className="space-y-1 text-label">
<p><span className="inline-block w-20 text-muted">什么时候</span>{whenText(w)}</p>
<p><span className="inline-block w-20 text-muted">看什么</span>{whatText(w)}</p>
{b.condition && src?.kind !== "sop" && (
<p><span className="inline-block w-20 text-muted">命中条件</span>
<code className="rounded bg-ink/[0.05] px-1.5 py-0.5 font-mono text-caption">{b.condition}</code></p>
)}
<div className="flex"><span className="inline-block w-20 shrink-0 text-muted">命中后</span>
<ul className="list-disc pl-4">{(b.strategies || []).map((s, i) => <li key={i}>{strategyText(s)}</li>)}</ul></div>
</section>
{w.basis && (
<section>
<h4 className="mb-1.5 text-label font-semibold">依据的原文</h4>
<blockquote className="border-l-2 border-line pl-3 text-label text-muted">{w.basis}</blockquote>
</section>
)}
{canEdit && w.status === "pending" && (
<AiEditBox placeholder="比如:改成每天 7 点;命中后也通知另一个角色;只看某一类"
ask={(q) => onto.aiEditModelingWatch(runId, w.id, q)}
apply={async (after) => { await onto.saveModelingWatch(runId, w.id, after); toast("ok", "已保存,按今天的数据重新扫过了"); onChanged(); }} />
)}
</div>
);
}
/** 监控的小表单:什么时候、条件、命中后通知谁(复杂的改动用「告诉 AI 怎么改」) */
function WatchEdit({ w, runId, onDone, onCancel }: { w: ModelWatchDraft; runId: string; onDone: () => void; onCancel: () => void }) {
const [b, setB] = useState<any>(JSON.parse(JSON.stringify(w.body)));
const [busy, setBusy] = useState(false);
const sch = b.schedule || { kind: "daily", at: "08:00" };
const setSch = (k: string, v: any) => setB({ ...b, schedule: { ...sch, [k]: v } });
const strategies: any[] = b.strategies || [];
const save = async () => {
setBusy(true);
try {
const r = await onto.saveModelingWatch(runId, w.id, b);
toast(r.issue ? "warn" : "ok", "已保存", r.issue || (r.trial?.summary ?? ""));
onDone();
} catch (e) { toast("error", "保存失败", errText(e)); } finally { setBusy(false); }
};
return (
<div className="space-y-2 rounded-lg border border-brand/30 bg-brand/[0.02] p-3 text-label">
<div className="grid gap-2 sm:grid-cols-3">
<label><span className="block text-caption text-muted">名称</span>
<input className="input h-8 w-full" value={b.title || ""} onChange={(e) => setB({ ...b, title: e.target.value })} /></label>
<label><span className="block text-caption text-muted">频率</span>
<select className="input h-8 w-full" value={sch.kind} onChange={(e) => setSch("kind", e.target.value)}>
<option value="daily">每天</option><option value="weekly">每周</option><option value="monthly">每月</option><option value="interval">每隔几分钟</option></select></label>
{sch.kind === "interval"
? <label><span className="block text-caption text-muted">间隔(分钟)</span>
<input className="input h-8 w-full" type="number" value={sch.every_minutes ?? 60} onChange={(e) => setSch("every_minutes", Number(e.target.value))} /></label>
: <label><span className="block text-caption text-muted">时间</span>
<input className="input h-8 w-full" type="time" value={sch.at || "08:00"} onChange={(e) => setSch("at", e.target.value)} /></label>}
</div>
{b.source?.kind !== "sop" && (
<label className="block"><span className="block text-caption text-muted">命中条件(对每一行求值,可用对象的属性和计算属性)</span>
<input className="input h-8 w-full font-mono" value={b.condition || ""} onChange={(e) => setB({ ...b, condition: e.target.value })} /></label>
)}
{strategies.map((s, i) => (s.kind === "notify" || s.kind === "task") ? (
<div key={i} className="grid gap-2 sm:grid-cols-2">
<label><span className="block text-caption text-muted">{s.kind === "notify" ? "通知谁(角色)" : "待办派给谁(角色)"}</span>
<input className="input h-8 w-full" value={s.role || ""} onChange={(e) => setB({ ...b, strategies: strategies.map((x, j) => (j === i ? { ...x, role: e.target.value } : x)) })} /></label>
<label><span className="block text-caption text-muted">标题</span>
<input className="input h-8 w-full" value={s.subject || s.title || ""}
onChange={(e) => setB({ ...b, strategies: strategies.map((x, j) => (j === i ? { ...x, [s.kind === "notify" ? "subject" : "title"]: e.target.value } : x)) })} /></label>
</div>
) : null)}
<div className="flex gap-1.5">
<button className="btn btn-primary btn-sm" disabled={busy} onClick={() => void save()}>
{busy ? <Spinner className="h-3.5 w-3.5" /> : <Save className="h-3.5 w-3.5" />}保存并试扫</button>
<button className="btn btn-sm" disabled={busy} onClick={onCancel}><X className="h-3.5 w-3.5" />取消</button>
</div>
</div>
);
}