PropertyPanel.tsx
12.2 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
import { BadgeCheck, Check, FileText, Maximize2, Pencil, X } from "lucide-react";
import { useEffect, useState } from "react";
import { api } from "@/lib/api";
import { typeColor } from "@/lib/colors";
import { shortHeading, truncate } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { Badge, Empty, ErrorNote, Pill, Skeleton } from "@/components/ui";
import type { EdgeBrief, EdgeDetail, NodeDetail } from "@/lib/types";
export type Selection =
| { kind: "node"; id: string }
| { kind: "edge"; source: string; target: string; relation: string }
| null;
/**
* 属性面板:实体/关系的全部属性、出入边、原文出处,并且**可编辑**。
* 编辑后标记 verified —— 人工校订过的内容在图上有区分,这是交付客户时的刚需。
*/
export function PropertyPanel({ selection, entityTypes, onSelect, onExpand, onChanged }: {
selection: Selection;
entityTypes: string[];
onSelect: (s: Selection) => void;
onExpand: (id: string) => void;
onChanged?: () => void;
}) {
const [node, setNode] = useState<NodeDetail | null>(null);
const [edge, setEdge] = useState<EdgeDetail | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState({ type: "", description: "" });
useEffect(() => {
setEditing(false);
setError(null);
if (!selection) { setNode(null); setEdge(null); return; }
let cancelled = false;
setLoading(true);
(async () => {
try {
if (selection.kind === "node") {
const d = await api.node(selection.id);
if (!cancelled) { setNode(d); setEdge(null); setDraft({ type: d.type, description: d.description }); }
} else {
const d = await api.edge(selection.source, selection.target, selection.relation);
if (!cancelled) { setEdge(d); setNode(null); setDraft({ type: "", description: d.description }); }
}
} catch (e) {
if (!cancelled) setError(e instanceof Error ? e.message : "加载失败");
} finally {
if (!cancelled) setLoading(false);
}
})();
return () => { cancelled = true; };
}, [selection]);
const save = async () => {
try {
if (node) {
const r = await api.updateNode({ id: node.id, type: draft.type, description: draft.description, verified: true });
setNode(r.node);
} else if (edge) {
await api.updateEdge({ source: edge.source, target: edge.target, relation: edge.relation,
description: draft.description, verified: true });
setEdge({ ...edge, description: draft.description, verified: true });
}
setEditing(false);
toast("ok", "已保存并标记为人工校订");
onChanged?.();
} catch (e) {
toast("error", "保存失败", e instanceof Error ? e.message : undefined);
}
};
if (!selection) {
return (
<Empty
icon={<Maximize2 className="h-8 w-8" />}
title="点击节点或关系查看详情"
desc="悬停会高亮它的邻居;双击节点可以展开它的邻域;拖拽可以调整位置。按 / 打开搜索。"
/>
);
}
if (loading) {
return (
<div className="space-y-2.5 p-3">
<Skeleton className="h-5 w-2/3" />
<Skeleton className="h-3 w-full" />
<Skeleton className="h-3 w-4/5" />
<Skeleton className="h-20 w-full" />
</div>
);
}
if (error) return <div className="p-3"><ErrorNote>{error}</ErrorNote></div>;
if (node) {
return (
<div className="flex h-full flex-col overflow-auto">
<div className="sticky top-0 z-10 border-b border-line bg-surface/95 px-3 py-2.5 backdrop-blur">
<div className="flex items-start gap-2">
<h3 className="min-w-0 flex-1 break-words text-[15px] font-semibold leading-snug">{node.label}</h3>
<button className="btn btn-ghost btn-sm shrink-0" onClick={() => setEditing(!editing)}
title={editing ? "取消编辑" : "编辑"}>
{editing ? <X className="h-3.5 w-3.5" /> : <Pencil className="h-3.5 w-3.5" />}
</button>
</div>
<div className="mt-1.5 flex flex-wrap items-center gap-1.5">
<Pill color={typeColor(node.type)}>{node.type}</Pill>
<Badge>{node.degree} 度</Badge>
<Badge>{node.occurrence} 处提及</Badge>
{node.community != null && <Badge>社区 {node.community}</Badge>}
{node.verified && (
<Badge tone="bg-ok/10 text-ok"><BadgeCheck className="h-3 w-3" />已校订</Badge>
)}
</div>
</div>
<div className="space-y-4 p-3">
{editing ? (
<div className="space-y-2">
<div>
<label className="label">实体类型</label>
<select className="input" value={draft.type}
onChange={(e) => setDraft({ ...draft, type: e.target.value })}>
{entityTypes.map((t) => <option key={t} value={t}>{t}</option>)}
</select>
</div>
<div>
<label className="label">描述</label>
<textarea className="input min-h-24 resize-y" value={draft.description}
onChange={(e) => setDraft({ ...draft, description: e.target.value })} />
</div>
<div className="flex gap-2">
<button className="btn btn-primary btn-sm" onClick={save}>
<Check className="h-3.5 w-3.5" />保存
</button>
<button className="btn btn-sm" onClick={() => setEditing(false)}>取消</button>
</div>
</div>
) : (
<p className="text-[13px] leading-relaxed">
{node.description || <span className="text-muted">(无描述)</span>}
</p>
)}
{node.aliases.length > 0 && (
<Field label={`别名(消歧合并 ${node.aliases.length} 个)`}>
<div className="flex flex-wrap gap-1.5">
{node.aliases.map((a) => <Badge key={a}>{a}</Badge>)}
</div>
</Field>
)}
<button className="btn w-full" onClick={() => onExpand(node.id)}>
<Maximize2 className="h-3.5 w-3.5" />以该节点为中心展开邻域
</button>
<EdgeList title={`出向关系(${node.out_edges.length})`} items={node.out_edges} dir="out"
onPick={(other, rel) => onSelect({ kind: "edge", source: node.id, target: other, relation: rel })}
onNode={(id) => onSelect({ kind: "node", id })} />
<EdgeList title={`入向关系(${node.in_edges.length})`} items={node.in_edges} dir="in"
onPick={(other, rel) => onSelect({ kind: "edge", source: other, target: node.id, relation: rel })}
onNode={(id) => onSelect({ kind: "node", id })} />
{node.sources.length > 0 && (
<Field label={`原文出处(${node.sources.length})`}>
<div className="space-y-1.5">
{node.sources.map((s) => <SourceCard key={s.chunk_id} s={s} />)}
</div>
</Field>
)}
</div>
</div>
);
}
if (edge) {
return (
<div className="flex h-full flex-col overflow-auto">
<div className="sticky top-0 z-10 border-b border-line bg-surface/95 px-3 py-2.5 backdrop-blur">
<div className="flex items-start gap-2">
<h3 className="min-w-0 flex-1 text-[15px] font-semibold">关系:{edge.relation}</h3>
<button className="btn btn-ghost btn-sm shrink-0" onClick={() => setEditing(!editing)}>
{editing ? <X className="h-3.5 w-3.5" /> : <Pencil className="h-3.5 w-3.5" />}
</button>
</div>
<div className="mt-1.5 flex flex-wrap items-center gap-1.5 text-[13px]">
<button className="rounded-md bg-brand/10 px-2 py-0.5 font-medium text-brand hover:bg-brand/20"
onClick={() => onSelect({ kind: "node", id: edge.source })}>{edge.source}</button>
<span className="text-warn">→</span>
<button className="rounded-md bg-brand/10 px-2 py-0.5 font-medium text-brand hover:bg-brand/20"
onClick={() => onSelect({ kind: "node", id: edge.target })}>{edge.target}</button>
<Badge>{edge.occurrence} 处佐证</Badge>
{edge.verified && <Badge tone="bg-ok/10 text-ok"><BadgeCheck className="h-3 w-3" />已校订</Badge>}
</div>
</div>
<div className="space-y-4 p-3">
{editing ? (
<div className="space-y-2">
<textarea className="input min-h-24 resize-y" value={draft.description}
onChange={(e) => setDraft({ ...draft, description: e.target.value })} />
<div className="flex gap-2">
<button className="btn btn-primary btn-sm" onClick={save}><Check className="h-3.5 w-3.5" />保存</button>
<button className="btn btn-sm" onClick={() => setEditing(false)}>取消</button>
</div>
</div>
) : (
<p className="text-[13px] leading-relaxed">
{edge.description || <span className="text-muted">(无描述)</span>}
</p>
)}
{edge.sources.length > 0 && (
<Field label={`原文出处(${edge.sources.length})`}>
<div className="space-y-1.5">{edge.sources.map((s) => <SourceCard key={s.chunk_id} s={s} />)}</div>
</Field>
)}
</div>
</div>
);
}
return null;
}
function Field({ label, children }: { label: string; children: React.ReactNode }) {
return <div><p className="label">{label}</p>{children}</div>;
}
function EdgeList({ title, items, dir, onPick, onNode }: {
title: string; items: EdgeBrief[]; dir: "in" | "out";
onPick: (other: string, rel: string) => void; onNode: (id: string) => void;
}) {
const [expanded, setExpanded] = useState(false);
if (!items.length) return null;
const shown = expanded ? items : items.slice(0, 8);
return (
<Field label={title}>
<div className="space-y-1">
{shown.map((e, i) => {
const other = (dir === "out" ? e.target : e.source) ?? "";
const otype = (dir === "out" ? e.target_type : e.source_type) ?? "";
return (
<div key={`${other}-${e.relation}-${i}`}
className="rounded-lg border border-line bg-elevated px-2 py-1.5">
<div className="flex flex-wrap items-center gap-1.5 text-[12.5px]">
<button className="text-muted hover:text-brand"
onClick={() => onPick(other, e.relation)}>
{dir === "out" ? `—[${e.relation}]→` : `←[${e.relation}]—`}
</button>
<button className="min-w-0 truncate font-medium hover:text-brand" onClick={() => onNode(other)}>
{other}
</button>
<Pill color={typeColor(otype)}>{otype || "其他"}</Pill>
{e.occurrence > 1 && <Badge>{e.occurrence}×</Badge>}
</div>
{e.description && (
<p className="mt-0.5 text-[11.5px] leading-relaxed text-muted">{truncate(e.description, 120)}</p>
)}
</div>
);
})}
{items.length > 8 && (
<button className="btn btn-ghost btn-sm w-full" onClick={() => setExpanded(!expanded)}>
{expanded ? "收起" : `展开其余 ${items.length - 8} 条`}
</button>
)}
</div>
</Field>
);
}
function SourceCard({ s }: { s: { chunk_id: string; heading: string; snippet: string } }) {
return (
<div className="rounded-lg border border-line bg-elevated p-2">
<p className="flex items-center gap-1 text-[11px] font-medium text-brand2">
<FileText className="h-3 w-3 shrink-0" />
<span className="truncate">{shortHeading(s.heading)}</span>
<code className="ml-auto shrink-0 rounded bg-ink/5 px-1 font-mono text-[10px] text-muted">{s.chunk_id}</code>
</p>
<p className="mt-1 text-[11.5px] leading-relaxed text-muted">{s.snippet}</p>
</div>
);
}