WorkbenchPage.tsx
13.7 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { ArrowRight, BellRing, CheckCircle2, ClipboardList, MessageSquareText, Play, Send, ShieldCheck, X } from "lucide-react";
import { Ago } from "@/components/kit";
import { fmtDuration, fmtValue as fmtField } from "@/lib/format";
import { useCallback, useEffect, useState } from "react";
import { Badge, Card, CardHeader, Empty, Spinner } from "@/components/ui";
import { useDemoMode } from "@/lib/env";
import { go } from "@/lib/nav";
import { RunDialog } from "@/features/sop/RunParts";
import { errText, onto, PRIORITY_TONE, type MemPattern, type SopParam, type Workbench } from "@/lib/onto";
import { toast } from "@/store/useAppStore";
const RUN_STATUS: Record<string, [string, string]> = {
pending_approval: ["待审批", "bg-warn/10 text-warn"], done: ["已完成", "bg-ok/10 text-ok"],
rejected: ["已驳回", "bg-bad/10 text-bad"], failed: ["执行失败", "bg-bad/10 text-bad"],
cancelled: ["已撤回", "bg-ink/5 text-muted"], reverted: ["已撤销", "bg-ink/5 text-muted"],
};
const PRIORITY_CN: Record<string, string> = { P0: "特急", P1: "紧急" };
/**
* 工作台:老板和员工每天打开系统的第一页。不讲本体、元素、管道这些搭建期的概念,
* 只回答「数字怎么样、有什么等我处理、我提交的办得怎样、常用的事一点就做」。
*/
export function WorkbenchPage() {
const [w, setW] = useState<Workbench | null>(null);
const [q, setQ] = useState("");
const [running, setRunning] = useState("");
const [runDlg, setRunDlg] = useState<{ id: string; title: string; params: SopParam[] } | null>(null);
const demo = useDemoMode();
const load = useCallback(() => { void onto.workbench().then(setW).catch((e) => toast("error", "加载失败", errText(e))); }, []);
useEffect(load, [load]);
if (!w) return <div className="p-8"><Spinner /></div>;
const askNow = (text: string) => { if (text.trim()) go("ask", { q: text.trim() }); };
const runSop = async (id: string, title: string) => {
setRunning(id);
try {
// 流程卡有参数就先弹框让人填(以前直接空参数启动,有必填参数的流程卡必失败)
const d = await onto.sop(id);
const v = await onto.sopVersion(id, d.current_version);
if ((v.spec.params || []).length) { setRunDlg({ id, title, params: v.spec.params }); return; }
const r = await onto.runSop(id, {});
toast("ok", `已启动「${title}」`);
go("sop", { run: r.id });
} catch (e) { toast("error", "启动失败", errText(e)); } finally { setRunning(""); }
};
const decideHabit = async (h: MemPattern, adopt: boolean) => {
try {
await onto.decidePattern(h.id, adopt);
if (adopt && h.kind === "action") go("sop", { draft: `每天检查需要「${h.title}」的情况,自动起草并提交审批` });
else if (adopt && h.kind === "sop") {
// 「设为定时运行」以前跳到流程卡的触发方式页签,而触发器已经搬进「监控与定时」了。
// 这里直接把监控器建好(默认每天 8 点跑这张卡),再跳过去让人调频率。
try {
const wt = await onto.createWatch({
title: `「${h.title}」定时`,
description: "",
schedule: { kind: "daily", at: "08:00" },
source: { kind: "sop", sop: h.ref },
condition: "",
strategies: [{ kind: "run_sop", sop: h.ref }],
});
toast("ok", "已设为每天 8:00 运行");
go("watch", { id: wt.id });
} catch (e) { toast("error", "创建定时失败", errText(e)); }
}
else toast("ok", adopt ? "已加入常用问题" : "好的,不再提示");
load();
} catch (e) { toast("error", "操作失败", errText(e)); }
};
const today = new Date().toLocaleDateString("zh-CN", { month: "long", day: "numeric", weekday: "long" });
// 演示模式不展示本体变更提案(搭建期内容),件数也跟着不算
const releases = demo ? [] : w.todo.releases;
const todoCount = w.todo.approvals_total + releases.length + w.todo.tasks.length;
return (
<div className="space-y-5">
<div>
<h2 className="text-page font-semibold">{w.user.name},你好</h2>
<p className="mt-1 text-body text-muted">
{today}{todoCount ? <>,有 <b className="text-ink">{todoCount}</b> 件事等你处理</> : ",没有等你处理的事"}
{(w.alerts || []).length > 0 && <>,<b className="text-warn">{w.alerts!.length}</b> 条预警</>}
</p>
</div>
<Card className="p-4">
<div className="flex gap-2">
<div className="relative min-w-0 flex-1">
<MessageSquareText className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted" />
<input className="input h-11 pl-9 text-body" value={q} placeholder="有什么想问的,直接说"
onChange={(e) => setQ(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") askNow(q); }} />
</div>
<button className="btn btn-primary h-11 px-5" disabled={!q.trim()} onClick={() => askNow(q)}><Send className="h-4 w-4" />提问</button>
</div>
{w.questions.length > 0 && (
<div className="mt-3 flex flex-wrap gap-2">
{w.questions.map((x) => (
<button key={x} className="chip hover:border-brand/50 hover:text-brand" onClick={() => askNow(x)}>{x}</button>
))}
</div>
)}
</Card>
{(w.habits || []).slice(0, 1).map((h) => (
<div key={h.id} className="flex flex-wrap items-center gap-3 rounded-lg border border-line bg-surface px-4 py-2.5 text-label">
<span className="min-w-0 flex-1 truncate">
{h.kind === "question" ? <>你问过好几次「{h.title}」,要不要放进常用问题?</>
: h.kind === "sop" ? <>你手动跑过好几次「{h.title}」,要不要设成每天自动跑?</> : (h.text || h.title)}
</span>
<button className="btn btn-sm" onClick={() => void decideHabit(h, true)}>
{h.kind === "question" ? "放进去" : h.kind === "action" ? "做成流程卡" : "设成自动"}</button>
<button className="rounded p-1 text-muted hover:text-ink" aria-label="不用了" onClick={() => void decideHabit(h, false)}><X className="h-4 w-4" /></button>
</div>
))}
{w.kpis.length > 0 ? (
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
{w.kpis.map((k) => (
<button key={k.name} className="card px-4 py-3.5 text-left transition-colors hover:border-brand/40" title={k.formula}
onClick={() => askNow(`${k.name}是多少?按门店或类别拆开看看`)}>
<p className="text-label text-muted">{k.name}</p>
<p className="mt-1.5 text-[26px] font-semibold leading-8 tabular-nums">{fmtField(k.value, { format: k.format, unit: "" })}
{k.unit && k.format !== "money" && k.format !== "percent" && <span className="ml-1 text-label font-normal text-muted">{k.unit}</span>}</p>
</button>
))}
</div>
) : (
<Card className="p-4 text-label text-muted">还没有可以展示的指标。</Card>
)}
<div className="grid gap-4 lg:grid-cols-2 [&>*]:min-w-0">
<Card>
<CardHeader title={<span className="flex items-center gap-1.5"><ShieldCheck className="h-4 w-4" />等我处理</span>}
right={todoCount > 0 && <button className="btn btn-ghost btn-sm" onClick={() => go("inbox")}>全部<ArrowRight className="h-3.5 w-3.5" /></button>} />
<div className="divide-y divide-line">
{w.todo.approvals.map((r) => (
<button key={r.id} className="flex w-full items-start gap-2 px-4 py-2.5 text-left hover:bg-ink/[0.03]"
onClick={() => go("inbox", { tab: "approvals", run: r.id })}>
<ShieldCheck className="mt-0.5 h-4 w-4 shrink-0 text-warn" />
<span className="min-w-0 flex-1">
<span className="block text-body font-medium">审批:{r.action_name}
{PRIORITY_CN[r.priority] && <Badge tone={PRIORITY_TONE[r.priority]} className="ml-2">{PRIORITY_CN[r.priority]}</Badge>}</span>
<span className="block truncate text-label text-muted">{r.summary}{r.submitted_name ? ` · ${r.submitted_name} 提交` : ""}</span>
</span>
<Ago at={r.created_at} className="shrink-0 text-label text-muted" />
</button>
))}
{releases.map((r) => (
<button key={r.id} className="flex w-full items-center gap-2 px-4 py-2.5 text-left hover:bg-ink/[0.03]" onClick={() => go("inbox", { tab: "releases" })}>
<Badge>本体变更</Badge><span className="min-w-0 flex-1 truncate text-body">{r.title}</span>
</button>
))}
{w.todo.tasks.map((t) => (
<button key={t.id} className="flex w-full items-center gap-2 px-4 py-2.5 text-left hover:bg-ink/[0.03]" onClick={() => go("inbox", { tab: "tasks" })}>
<ClipboardList className="h-4 w-4 text-brand" /><span className="min-w-0 flex-1 truncate text-body">待办:{t.title}</span>
{t.due_at && <span className="text-label text-muted">截止 <Ago at={t.due_at} /></span>}
</button>
))}
{!todoCount && <Empty icon={<CheckCircle2 className="h-8 w-8" />} title="都处理完了" />}
</div>
</Card>
<Card>
<CardHeader title={<span className="flex items-center gap-1.5"><BellRing className="h-4 w-4" />预警</span>}
right={(w.alerts || []).length > 0 && <button className="btn btn-ghost btn-sm" onClick={() => go("alerts")}>全部<ArrowRight className="h-3.5 w-3.5" /></button>} />
<div className="divide-y divide-line">
{(w.alerts || []).map((a) => (
<button key={a.id} className="flex w-full items-start gap-2 px-4 py-2.5 text-left hover:bg-ink/[0.03]" onClick={() => go("alerts", { watch: a.watch_id })}>
<span className="mt-2 h-1.5 w-1.5 shrink-0 rounded-full bg-warn" />
<span className="min-w-0 flex-1">
<span className="block truncate text-body font-medium">{a.label}</span>
<span className="block truncate text-label text-muted">{a.watch}</span>
</span>
<span className="shrink-0 text-label text-muted">已持续 {fmtDuration(a.first_seen_at) || "不到 1 分钟"}</span>
</button>
))}
{!(w.alerts || []).length && <Empty icon={<CheckCircle2 className="h-8 w-8" />} title="现在没有预警" />}
</div>
</Card>
</div>
<div className="grid gap-4 lg:grid-cols-2 [&>*]:min-w-0">
<Card>
<CardHeader title="我提交的" />
<div className="divide-y divide-line">
{w.mine.map((r) => {
const [label, tone] = RUN_STATUS[r.status] || [r.status, "bg-ink/5 text-muted"];
return (
<button key={r.id} className="flex w-full items-start gap-2 px-4 py-2.5 text-left hover:bg-ink/[0.03]"
onClick={() => go("inbox", { tab: "runs", run: r.id })}>
<Badge tone={tone}>{label}</Badge>
<span className="min-w-0 flex-1">
<span className="block text-body font-medium">{r.action_name}</span>
<span className="block truncate text-label text-muted">{r.summary}</span>
</span>
<Ago at={r.created_at} className="shrink-0 text-label text-muted" />
</button>
);
})}
{!w.mine.length && <Empty title="还没有提交过什么" />}
</div>
</Card>
<Card>
<CardHeader title="常用流程" />
<div className="divide-y divide-line">
{w.sops.map((s) => (
<div key={s.id} className="flex items-center gap-2 px-4 py-2.5">
<span className="min-w-0 flex-1">
<span className="block text-body font-medium">{s.title}</span>
<span className="block truncate text-label text-muted">{s.description}</span>
</span>
{w.user.can_run
? <button className="btn btn-sm" disabled={!!running} onClick={() => void runSop(s.id, s.title)}>
{running === s.id ? <Spinner className="h-3.5 w-3.5" /> : <Play className="h-3.5 w-3.5" />}运行</button>
: <button className="btn btn-ghost btn-sm" onClick={() => go("sop", { id: s.id })}>查看</button>}
</div>
))}
{!w.sops.length && <Empty title="还没有可以运行的流程" />}
</div>
</Card>
</div>
<Card>
<CardHeader title="最近发生的事" right={!demo && <button className="btn btn-ghost btn-sm" onClick={() => go("monitor", { tab: "audit" })}>全部<ArrowRight className="h-3.5 w-3.5" /></button>} />
<div className="divide-y divide-line">
{w.feed.map((f, i) => (
<p key={i} className="flex gap-3 px-5 py-2.5 text-label">
<Ago at={f.ts} className="w-24 shrink-0 text-muted" />
<span className="w-28 shrink-0 truncate text-muted">{f.actor}</span>
<span className="min-w-0 flex-1 truncate">{f.summary}</span>
</p>
))}
{!w.feed.length && <Empty title="还没有" />}
</div>
</Card>
{runDlg && <RunDialog title={runDlg.title} params={runDlg.params} mode="live" defId={runDlg.id} onClose={() => setRunDlg(null)}
onDone={(r) => { setRunDlg(null); go("sop", { run: r.id }); }} />}
</div>
);
}