ObjectsPage.tsx
18.5 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { Boxes, ExternalLink, History, Radar, RefreshCw, ShieldCheck, Workflow, Zap } from "lucide-react";
import { useEffect, useState } from "react";
import { Badge, Card, CardHeader, Empty, Field, Modal, Spinner, Stat } from "@/components/ui";
import { go, useHashParams } from "@/lib/nav";
import {
errText, fmtTime, onto, PRIORITY_TONE, STATUS_LABEL, STATUS_TONE, type CapDef, type ConceptBrief, type ImpactResult,
type Me, type ObjectRec,
} from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { EffectList } from "../ask/ActionCard";
/**
* 对象 360:任一对象实例的全貌 —— 属性(业务库 + 写回层)、关联对象、指标、相关文档、
* 动作记录、SOP 运行、审计时间线。业务人员最直观的入口:「这个客户/网点现在怎么样、谁动过它」。
*/
export function ObjectsPage({ me }: { me: Me | null }) {
const params = useHashParams();
const [types, setTypes] = useState<ConceptBrief[]>([]);
const [type, setType] = useState(params.get("type") || "");
const [q, setQ] = useState("");
const [items, setItems] = useState<ObjectRec[] | null>(null);
const [key, setKey] = useState(params.get("key") || "");
const [err, setErr] = useState("");
useEffect(() => {
void onto.concepts({ kind: "object_type", status: "published" }).then((t) => {
setTypes(t);
if (!type && t[0]) setType(t[0].id);
});
}, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
const t = params.get("type"); const k = params.get("key");
if (t) setType(t); if (k) setKey(k);
}, [params]);
useEffect(() => {
if (!type) return;
setItems(null); setErr("");
const h = window.setTimeout(() => {
void onto.objects(type, q, 50).then((r) => setItems(r.items)).catch((e) => { setErr(errText(e)); setItems([]); });
}, 250);
return () => window.clearTimeout(h);
}, [type, q]);
if (!types.length) {
return <Card><Empty icon={<Boxes className="h-10 w-10" />} title="还没有已发布的对象类型"
desc="在本体目录里定义对象类型、绑定到数据表并发布后,这里就能按对象浏览。"
action={<button className="btn" onClick={() => go("b_object")}>去对象类型</button>} /></Card>;
}
return (
<div className="grid gap-4 lg:grid-cols-[300px_minmax(0,1fr)]">
<Card className="flex max-h-[calc(100vh-140px)] flex-col">
<div className="space-y-2 border-b border-line p-2">
<select className="input" value={type} onChange={(e) => { setType(e.target.value); setKey(""); }}>
{types.map((t) => <option key={t.id} value={t.id}>{t.name}</option>)}
</select>
<div className="flex gap-1.5">
<input className="input" placeholder="按名称搜索" value={q} onChange={(e) => setQ(e.target.value)} />
{me?.permissions.includes("build") && (
<button className="btn btn-sm" title="同步这类对象的骨架(对象搜索、影响分析用;定时同步在「数据管道」里配)" onClick={async () => {
try { const r = await onto.syncObjects(type); toast("ok", `已同步 ${r.rows} 个对象、${r.edges} 条关系边`); }
catch (e) { toast("error", errText(e)); }
}}><RefreshCw className="h-3.5 w-3.5" /></button>
)}
</div>
</div>
<div className="min-h-0 flex-1 overflow-auto">
{err && <p className="p-3 text-[12px] text-bad">{err}</p>}
{!items ? <div className="p-4"><Spinner /></div> : items.map((o) => (
<button key={o.key} onClick={() => { setKey(o.key); go("objects", { type, key: o.key }); }}
className={cn("block w-full border-b border-line/60 px-3 py-2 text-left hover:bg-ink/[0.03]", key === o.key && "bg-brand/5")}>
<p className="truncate text-[13px] font-medium">{o.label}</p>
<p className="text-[11px] text-muted">主键 {o.key}{o.overridden?.length ? ` · 有写回` : ""}</p>
</button>
))}
</div>
</Card>
<div className="min-w-0">
{key ? <Object360 key={`${type}:${key}`} type={type} objKey={key} me={me} />
: <Card><Empty title="选择左侧一个对象" /></Card>}
</div>
</div>
);
}
function Object360({ type, objKey, me }: { type: string; objKey: string; me: Me | null }) {
const [d, setD] = useState<any>(null);
const [actions, setActions] = useState<CapDef[]>([]);
const [act, setAct] = useState<CapDef | null>(null);
const load = () => { void onto.object360(type, objKey).then(setD).catch((e) => toast("error", "加载失败", errText(e))); };
useEffect(() => {
load();
void onto.capDefs("action").then((a) => setActions(a.filter((x) => x.status === "published" && x.spec.target_type === type)));
}, []); // eslint-disable-line react-hooks/exhaustive-deps
if (!d) return <Card className="p-6"><Spinner /></Card>;
const o = d.object;
const props = (d.object_type.spec?.properties || []) as any[];
return (
<div className="space-y-4">
<Card>
<CardHeader title={`${d.object_type.name}:${o.label}`} desc={`主键 ${o.key}`}
right={me?.permissions.includes("operate") && actions.map((a) => (
<button key={a.id} className="btn btn-sm" onClick={() => setAct(a)}><Zap className="h-3.5 w-3.5" />{a.display_name || a.name}</button>
))} />
<div className="grid gap-x-6 gap-y-2 p-4 sm:grid-cols-2 xl:grid-cols-3">
{Object.entries(o.properties).map(([k, v]) => (
<div key={k} className="min-w-0">
<p className="text-[11px] text-muted" title={k}>{props.find((p) => p.name === k)?.display_name || k}{o.overridden?.includes(k) && <Badge tone="bg-brand/10 text-brand" className="ml-1">写回</Badge>}
{props.find((p) => p.name === k)?.is_writable && !o.overridden?.includes(k) && <span className="ml-1 text-[10px]">可写</span>}</p>
<p className="truncate text-[13px]">{v === null || v === undefined ? "—" : String(v)}</p>
</div>
))}
</div>
</Card>
{d.metrics.length > 0 && (
<Card className="grid gap-4 p-4 sm:grid-cols-3 xl:grid-cols-4">
{d.metrics.map((m: any) => (
<Stat key={m.metric.id} label={m.metric.name}
value={m.error ? "—" : m.value === null ? "∅" : `${typeof m.value === "number" ? Number(m.value.toFixed(4)) : m.value}${m.metric.unit || ""}`}
hint={m.error ? m.error : m.period ? `本期 ${m.period.period}` : "全量"} />
))}
</Card>
)}
<ImpactPanel type={type} objKey={objKey} />
<div className="grid gap-4 xl:grid-cols-2">
<Card>
<CardHeader title="关联对象(一跳,实时)" />
<div className="space-y-3 p-4">
{!d.links.length && <p className="text-[12px] text-muted">本体里没有涉及它的关系类型,或关系未绑定关联列</p>}
{d.links.map((g: any) => (
<div key={g.link_id}>
<p className="mb-1 text-[12px] font-medium">{g.link_type} → {g.object_type}({g.items.length})</p>
<div className="flex flex-wrap gap-1.5">
{g.items.map((x: ObjectRec) => (
<button key={x.key} className="chip" onClick={() => go("objects", { type: g.object_type_id, key: x.key })}>{x.label}</button>
))}
</div>
</div>
))}
</div>
</Card>
<Card>
<CardHeader title="相关文档" />
<div className="space-y-1.5 p-4">
{!d.documents.length && <p className="text-[12px] text-muted">文档里没有提到它</p>}
{d.documents.map((c: any) => (
<p key={c.id} className="line-clamp-2 text-[12px]"><b>{c.heading || c.id}</b> {c.snippet}</p>
))}
</div>
</Card>
<Card>
<CardHeader title="动作记录" />
<div className="divide-y divide-line">
{!(d.action_runs || []).length && <p className="p-4 text-[12px] text-muted">没有动作</p>}
{(d.action_runs || []).map((r: any) => (
<button key={r.id} className="flex w-full items-center gap-2 px-4 py-2 text-left text-[12.5px] hover:bg-ink/[0.03]"
onClick={() => go("inbox", { tab: "runs", run: r.id })}>
<ShieldCheck className="h-3.5 w-3.5 text-muted" />
<span className="min-w-0 flex-1 truncate">{r.action_name} · {r.preview.summary}</span>
<Badge tone={STATUS_TONE[r.status]}>{STATUS_LABEL[r.status] || r.status}</Badge>
</button>
))}
</div>
</Card>
<Card>
<CardHeader title="流程运行" />
<div className="divide-y divide-line">
{!(d.sop_runs || []).length && <p className="p-4 text-[12px] text-muted">没有流程运行</p>}
{(d.sop_runs || []).map((r: any) => (
<button key={r.id} className="flex w-full items-center gap-2 px-4 py-2 text-left text-[12.5px] hover:bg-ink/[0.03]"
onClick={() => go("sop", { run: r.id })}>
<Workflow className="h-3.5 w-3.5 text-muted" />
<span className="min-w-0 flex-1 truncate">{r.title} · {fmtTime(r.created_at)}</span>
<Badge tone={STATUS_TONE[r.status]}>{STATUS_LABEL[r.status] || r.status}</Badge>
</button>
))}
</div>
</Card>
</div>
<Card>
<CardHeader title={<span className="flex items-center gap-1.5"><History className="h-4 w-4" />变更时间线</span>}
desc="写回层的每次修改与相关审计" />
<div className="divide-y divide-line">
{[...d.edits.map((e: any) => ({ ts: e.created_at, text: `${e.property}:${JSON.stringify(e.prev)} → ${JSON.stringify(e.value)}${e.active ? "" : "(已撤销)"}`, who: e.created_by })),
...d.audit.map((a: any) => ({ ts: a.ts, text: a.summary, who: a.actor_name }))]
.sort((a, b) => (a.ts < b.ts ? 1 : -1)).slice(0, 40).map((x, i) => (
<div key={i} className="flex gap-3 px-4 py-2 text-[12.5px]">
<span className="w-32 shrink-0 text-muted">{fmtTime(x.ts)}</span>
<span className="min-w-0 flex-1">{x.text}</span>
<span className="text-muted">{x.who}</span>
</div>
))}
{!d.edits.length && !d.audit.length && <p className="p-4 text-[12px] text-muted">没有变更</p>}
</div>
</Card>
{act && <ProposeActionModal action={act} objKey={objKey} onClose={() => setAct(null)} onDone={() => { setAct(null); load(); }} />}
</div>
);
}
/**
* 实例级影响分析:顺着关系网往外推 —— 这台机台出问题,哪些批次受影响、连累哪些订单、交期会不会破。
* 遍历走数据管道同步的骨架;最外层对象回业务库实时取一次最新属性。
*/
function ImpactPanel({ type, objKey }: { type: string; objKey: string }) {
const [depth, setDepth] = useState(3);
const [r, setR] = useState<ImpactResult | null>(null);
const [err, setErr] = useState("");
const [busy, setBusy] = useState(false);
const run = async () => {
setBusy(true); setErr("");
try { setR(await onto.impactObject(type, objKey, depth)); } catch (e) { setR(null); setErr(errText(e)); } finally { setBusy(false); }
};
return (
<Card>
<CardHeader title={<span className="flex items-center gap-1.5"><Radar className="h-4 w-4 text-brand" />影响分析</span>}
desc={r ? `${r.summary}${r.synced_at ? ` · 骨架数据截至 ${fmtTime(r.synced_at)}` : ""}${r.hidden ? ` · ${r.hidden} 个不在你的数据范围内` : ""}`
: "沿关系网多跳推演:会波及哪些对象、最外层的最新状态、可以采取的动作"}
right={<>
<select className="input h-8 w-24 py-0 text-[12px]" value={depth} onChange={(e) => setDepth(Number(e.target.value))}>
{[1, 2, 3, 4].map((d) => <option key={d} value={d}>{d} 跳</option>)}
</select>
<button className="btn btn-sm btn-primary" disabled={busy} onClick={() => void run()}>
{busy ? <Spinner className="h-3.5 w-3.5" /> : <Radar className="h-3.5 w-3.5" />}跑一次</button>
</>} />
{(err || r) && (
<div className="space-y-3 p-4 text-[12.5px]">
{err && <p className="rounded-md bg-warn/10 p-2 text-warn">{err}{err.includes("数据管道") &&
<button className="ml-2 text-brand underline" onClick={() => go("pipeline")}>去数据管道</button>}</p>}
{r && (
<>
<div className="flex flex-wrap items-center gap-2">
<span className="rounded-lg border border-brand/40 bg-brand/5 px-2 py-1 font-medium">{r.root.type_name}「{r.root.label}」</span>
{r.layers.map((l) => (
<span key={l.depth} className="flex items-center gap-2">
<span className="text-muted">→ 第 {l.depth} 跳</span>
{l.groups.map((g) => <Badge key={g.type_name} tone="bg-warn/10 text-warn">{g.type_name} {g.count}</Badge>)}
</span>
))}
{Object.entries(r.context || {}).map(([k, v]) => <span key={k} className="chip text-muted" title="沿外键往上走到的上级对象,只列出、不再往外推">涉及{k} {v}</span>)}
{!r.layers.length && <span className="text-muted">没有找到关联对象(检查关系是否绑定了关联列、两端是否都同步了)</span>}
{r.truncated && <Badge>结果已截断</Badge>}
</div>
{r.layers.map((l) => l.groups.map((g) => (
<div key={`${l.depth}:${g.type_name}`}>
<p className="mb-1 text-[11.5px] text-muted">第 {l.depth} 跳 · {g.type_name}({g.count})</p>
<div className="flex flex-wrap gap-1.5">
{g.items.map((n) => (
<button key={n.id} className="chip" title={n.via ? `经「${n.via.link}」` : ""}
onClick={() => go("objects", { type: n.type_id, key: n.key })}>{n.label}</button>
))}
</div>
</div>
)))}
{r.live.length > 0 && (
<div className="rounded-lg border border-line">
<p className="border-b border-line bg-ink/[0.02] px-2 py-1 text-[11.5px] font-medium">最外层对象的最新状态(实时)</p>
<div className="max-h-48 overflow-auto">
{r.live.map((x) => (
<p key={x.id} className="border-b border-line/50 px-2 py-1 last:border-0">
<b>{x.type_name}「{x.label}」</b>
<span className="ml-2 text-muted">{Object.entries(x.properties).slice(0, 6).map(([k, v]) => `${k} ${v ?? "—"}`).join(" · ")}</span></p>
))}
</div>
</div>
)}
{r.actions.length > 0 && (
<p className="text-muted">可以对受影响对象采取的动作:{r.actions.map((a) => a.display_name).join("、")}(在对应对象页发起,进审批)</p>
)}
</>
)}
</div>
)}
</Card>
);
}
/** 从对象页直接发起动作:填参数 → 看预览 → 提交审批。 */
export function ProposeActionModal({ action, objKey, onClose, onDone }: { action: CapDef; objKey: string; onClose: () => void; onDone: () => void }) {
const [vals, setVals] = useState<Record<string, any>>({});
const [reason, setReason] = useState("");
const [priority, setPriority] = useState("");
const [preview, setPreview] = useState<any>(null);
const [err, setErr] = useState("");
const params = action.spec.params || [];
const doPreview = async () => {
setErr("");
try { setPreview(await onto.prepareAction(action.id, vals, objKey)); } catch (e) { setPreview(null); setErr(errText(e)); }
};
return (
<Modal open onClose={onClose} title={`发起动作:${action.display_name || action.name}`} desc={action.description}
footer={<>
<button className="btn" onClick={onClose}>取消</button>
<button className="btn" onClick={() => void doPreview()}>预览</button>
<button className="btn btn-primary" disabled={!preview || !reason.trim()} onClick={async () => {
try {
const r = await onto.proposeAction(action.id, { params: vals, object_key: objKey, reason, priority });
toast("ok", "已提交审批"); onDone(); go("inbox", { tab: "runs", run: r.id });
} catch (e) { toast("error", "提交失败", errText(e)); }
}}>提交审批 <ExternalLink className="h-3 w-3" /></button>
</>}>
<div className="space-y-3">
{params.map((p: any) => (
<Field key={p.name} label={p.label} required={p.required} hint={p.description}>
{p.enum?.length ? (
<select className="input" value={vals[p.name] || ""} onChange={(e) => setVals({ ...vals, [p.name]: e.target.value })}>
<option value="">请选择</option>{p.enum.map((x: string) => <option key={x}>{x}</option>)}
</select>
) : (
<input className="input" value={vals[p.name] || ""} onChange={(e) => setVals({ ...vals, [p.name]: e.target.value })} />
)}
</Field>
))}
<div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_120px]">
<Field label="提交理由(审批人看到的依据)" required><input className="input" value={reason} onChange={(e) => setReason(e.target.value)} /></Field>
<Field label="优先级" hint="不选按动作风险定">
<select className={`input ${priority ? PRIORITY_TONE[priority] : ""}`} value={priority} onChange={(e) => setPriority(e.target.value)}>
<option value="">自动</option>{["P0", "P1", "P2", "P3"].map((p) => <option key={p}>{p}</option>)}
</select>
</Field>
</div>
{err && <p className="rounded-md bg-bad/10 p-2 text-[12.5px] text-bad">{err}</p>}
{preview && <EffectList preview={preview} />}
<p className="text-[11px] text-muted">所有动作都需要人工审批后才执行;你不能审批自己提交的动作。</p>
</div>
</Modal>
);
}