FrontmatterCard.tsx
5.85 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
"use client";
import { useState } from "react";
import { ChevronDown, ChevronRight, Tag } from "lucide-react";
import { cn } from "@/lib/utils";
const PRIMARY_KEYS = ["title", "type", "entity_type", "schema_version", "created", "updated"];
const CHIP_KEYS = ["tags", "brand", "series", "business_category", "sub_business", "role", "related"];
const HIDDEN_KEYS = [...PRIMARY_KEYS, ...CHIP_KEYS, "sources", "field_sources"];
function toStringArray(value: unknown): string[] {
if (value === null || value === undefined) return [];
if (Array.isArray(value)) return value.map((v) => String(v)).filter(Boolean);
return [String(value)].filter(Boolean);
}
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 truncate">
- {String(item)}
</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">{String(node)}</div>;
}
export default function FrontmatterCard({ data }: { data: Record<string, unknown> }) {
const [sourcesOpen, setSourcesOpen] = useState(false);
const [fieldSourcesOpen, setFieldSourcesOpen] = useState(false);
const title = data.title ? String(data.title) : null;
const sources = toStringArray(data.sources);
const fieldSources = data.field_sources;
const otherKeys = Object.keys(data).filter((k) => !HIDDEN_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-2">{title}</h1>}
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-slate-500 mb-3">
{data.type != null && (
<span className="font-medium">
类型: <span className="text-slate-700">{String(data.type)}</span>
</span>
)}
{data.entity_type != null && (
<span className="font-medium">
实体类型: <span className="text-slate-700">{String(data.entity_type)}</span>
</span>
)}
{data.schema_version != null && (
<span className="font-medium">
Schema: <span className="text-slate-700">{String(data.schema_version)}</span>
</span>
)}
{data.created != null && (
<span className="font-medium">
创建: <span className="text-slate-700">{String(data.created)}</span>
</span>
)}
{data.updated != null && (
<span className="font-medium">
更新: <span className="text-slate-700">{String(data.updated)}</span>
</span>
)}
</div>
<div className="space-y-2">
{CHIP_KEYS.map((key) => {
const values = toStringArray(data[key]);
if (values.length === 0) return null;
return (
<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-24">
{key}
</span>
<div className="flex flex-wrap gap-1.5">
{values.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" />
{v}
</span>
))}
</div>
</div>
);
})}
{otherKeys.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-24">
{key}
</span>
<span className="text-xs text-slate-600 break-all">{String(data[key])}</span>
</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 truncate">
{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>
);
}