FrontmatterCard.tsx
5.64 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
"use client";
import { useState } from "react";
import { ChevronDown, ChevronRight, Tag } from "lucide-react";
import { cn } from "@/lib/utils";
// 单独走折叠区渲染(内容通常很长),不进通用字段循环,避免重复。
const COLLAPSIBLE_KEYS = ["sources", "field_sources"];
// 常见字段的中文标签;未列出的字段回退为原始 key,保证「全量渲染」不漏字段。
const FIELD_LABELS: Record<string, string> = {
type: "类型",
entity_type: "实体类型",
schema_version: "Schema 版本",
created: "创建时间",
updated: "更新时间",
brand: "品牌",
series: "体系",
business_category: "业务类别",
sub_business: "子业务",
role: "角色",
related: "关联",
tags: "标签",
};
function isScalarArray(arr: unknown[]): boolean {
return arr.every((v) => typeof v !== "object" || v === null);
}
function FieldSourcesTree({ node, depth = 0 }: { node: unknown; depth?: number }) {
if (Array.isArray(node)) {
return (
<ul className="space-y-0.5">
{node.map((item, idx) => (
<li key={idx} className="text-[11px] font-mono text-slate-500 break-all">
- {typeof item === "object" && item !== null ? "" : String(item)}
{typeof item === "object" && item !== null && <FieldSourcesTree node={item} depth={depth + 1} />}
</li>
))}
</ul>
);
}
if (node && typeof node === "object") {
return (
<div className={cn("space-y-1.5", depth > 0 && "pl-3 border-l border-slate-200")}>
{Object.entries(node as Record<string, unknown>).map(([key, value]) => (
<div key={key}>
<div className="text-[11px] font-bold text-slate-600">{key}</div>
<FieldSourcesTree node={value} depth={depth + 1} />
</div>
))}
</div>
);
}
return <div className="text-[11px] font-mono text-slate-500 break-all">{String(node)}</div>;
}
// 通用字段值渲染:标量→文本,标量数组→标签块,对象/嵌套数组→树。
function FieldValue({ value }: { value: unknown }) {
if (value === null || value === undefined || value === "") {
return <span className="text-xs text-slate-400">—</span>;
}
if (Array.isArray(value)) {
if (value.length === 0) return <span className="text-xs text-slate-400">—</span>;
if (isScalarArray(value)) {
return (
<div className="flex flex-wrap gap-1.5">
{value.map((v, idx) => (
<span
key={idx}
className="inline-flex items-center gap-1 rounded-full bg-blue-50 text-blue-700 px-2 py-0.5 text-[11px] font-medium"
>
<Tag className="w-2.5 h-2.5" />
{String(v)}
</span>
))}
</div>
);
}
return <FieldSourcesTree node={value} />;
}
if (typeof value === "object") {
return <FieldSourcesTree node={value} />;
}
return <span className="text-xs text-slate-600 break-all">{String(value)}</span>;
}
export default function FrontmatterCard({ data }: { data: Record<string, unknown> }) {
const [sourcesOpen, setSourcesOpen] = useState(false);
const [fieldSourcesOpen, setFieldSourcesOpen] = useState(false);
const title = data.title != null ? String(data.title) : null;
const sources = Array.isArray(data.sources) ? data.sources.map(String).filter(Boolean) : [];
const fieldSources = data.field_sources;
// 除 title 与折叠区字段外,其余一律通用全量渲染(含未来新增字段,不会漏)。
const fieldKeys = Object.keys(data).filter(
(k) => k !== "title" && !COLLAPSIBLE_KEYS.includes(k)
);
return (
<div className="mb-8 rounded-2xl border border-slate-200 bg-slate-50/70 p-5">
{title && <h1 className="text-lg font-bold text-slate-800 mb-3">{title}</h1>}
<div className="space-y-2">
{fieldKeys.map((key) => (
<div key={key} className="flex items-start gap-2">
<span className="shrink-0 text-[11px] font-bold text-slate-400 uppercase tracking-wide pt-0.5 w-28 break-all">
{FIELD_LABELS[key] || key}
</span>
<div className="min-w-0 flex-1">
<FieldValue value={data[key]} />
</div>
</div>
))}
</div>
{sources.length > 0 && (
<div className="mt-3 pt-3 border-t border-slate-200">
<button
onClick={() => setSourcesOpen((o) => !o)}
className="flex items-center gap-1 text-[11px] font-bold text-slate-500 hover:text-slate-700"
>
{sourcesOpen ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
来源文件 ({sources.length})
</button>
{sourcesOpen && (
<ul className="mt-1.5 space-y-0.5 pl-4">
{sources.map((s, idx) => (
<li key={idx} className="text-[11px] font-mono text-slate-500 break-all">
{s}
</li>
))}
</ul>
)}
</div>
)}
{fieldSources != null && typeof fieldSources === "object" && (
<div className="mt-3 pt-3 border-t border-slate-200">
<button
onClick={() => setFieldSourcesOpen((o) => !o)}
className="flex items-center gap-1 text-[11px] font-bold text-slate-500 hover:text-slate-700"
>
{fieldSourcesOpen ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
字段来源明细
</button>
{fieldSourcesOpen && (
<div className="mt-2 pl-4">
<FieldSourcesTree node={fieldSources} />
</div>
)}
</div>
)}
</div>
);
}