ObjectsPage.tsx
13 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
import { Boxes, ExternalLink, History, 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, STATUS_LABEL, STATUS_TONE, type CapDef, type ConceptBrief, 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("kernel")}>去本体目录</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.synced} 个对象名`); }
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">{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>
)}
<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>
);
}
/** 从对象页直接发起动作:填参数 → 看预览 → 提交审批。 */
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 [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 });
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>
))}
<Field label="提交理由" required><input className="input" value={reason} onChange={(e) => setReason(e.target.value)} /></Field>
{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>
);
}