| 1 | /* 实体的认知变更历史(记录时间轴)。 |
| 2 | 与同面板的 Timeline 视图正交:那条轴问"这件事在现实里何时成立",这条轴问 |
| 3 | "我们何时这么认为、又何时改了主意"。数据来自 append-only 账本里那些被 |
| 4 | entity_detail 用 invalidated_at IS NULL 滤掉的行。 */ |
| 5 | import { useEffect, useState } from "react"; |
| 6 | import { useQuery } from "@tanstack/react-query"; |
| 7 | import { Link } from "@tanstack/react-router"; |
| 8 | import { FileText, Merge, PencilLine, Tag, Undo2 } from "lucide-react"; |
| 9 | import { api, type EntityHistoryEvent } from "../api"; |
| 10 | import { S } from "../i18n"; |
| 11 | import { Pager } from "../ui"; |
| 12 | |
| 13 | const PER = 20; |
| 14 | |
| 15 | /** 事件类型 → 图标与色调(语义色只给"被推翻",其余保持中性) */ |
| 16 | const KIND_ICON = { |
| 17 | asserted: FileText, |
| 18 | corrected: PencilLine, |
| 19 | rejected: Undo2, |
| 20 | // 并入不是撤回:内容一字未少地进了另一条断言 |
| 21 | merged: Merge, |
| 22 | // 改类不是事实变更:图上的节点换了个类,事实一条没动 |
| 23 | retyped: Tag, |
| 24 | retype_reverted: Undo2, |
| 25 | } as const; |
| 26 | |
| 27 | const KIND_TONE: Record<string, string> = { |
| 28 | asserted: "text-neutral-500", |
| 29 | corrected: "text-[var(--u-warn)]", |
| 30 | rejected: "text-[var(--u-danger)]", |
| 31 | merged: "text-neutral-500", |
| 32 | retyped: "text-neutral-500", |
| 33 | retype_reverted: "text-[var(--u-warn)]", |
| 34 | }; |
| 35 | |
| 36 | const ymd = (iso: string) => iso.slice(0, 10); |
| 37 | const ym = (iso: string | null) => (iso ? iso.slice(0, 7) : null); |
| 38 | |
| 39 | /** 宾语:实体名优先,其次字面值(属性事实)的摘要/值 */ |
| 40 | function objectText(e: EntityHistoryEvent): string { |
| 41 | if (e.other_name) return e.other_name; |
| 42 | const v = e.object_value as { summary?: unknown; value?: unknown } | null; |
| 43 | const raw = v?.summary ?? v?.value; |
| 44 | return raw === undefined || raw === null ? "—" : String(raw); |
| 45 | } |
| 46 | |
| 47 | /** 这次变更对有效区间做了什么(记录轴上的事件,改的是有效轴上的边界) */ |
| 48 | function intervalNote(e: EntityHistoryEvent): string | null { |
| 49 | if (e.kind === "corrected") { |
| 50 | return e.valid_to ? S.graph.historyClosedAt(ym(e.valid_to)!) : null; |
| 51 | } |
| 52 | const from = ym(e.valid_from); |
| 53 | if (!from) return null; |
| 54 | return e.valid_to |
| 55 | ? `${from} → ${ym(e.valid_to)}` |
| 56 | : `${S.graph.historyFrom(from)} · ${S.graph.historyOngoing}`; |
| 57 | } |
| 58 | |
| 59 | function EventRow({ e }: { e: EntityHistoryEvent }) { |
| 60 | const Icon = KIND_ICON[e.kind] ?? FileText; |
| 61 | const note = intervalNote(e); |
| 62 | return ( |
| 63 | <div className="flex gap-2.5 px-2 py-2"> |
| 64 | <Icon size={13} className={`mt-0.5 shrink-0 ${KIND_TONE[e.kind] ?? ""}`} /> |
| 65 | <div className="min-w-0 flex-1"> |
| 66 | <div className="flex items-baseline gap-1.5 flex-wrap"> |
| 67 | <span className="text-[11px] font-medium text-neutral-300"> |
| 68 | {S.graph.historyKind[e.kind] ?? e.kind} |
| 69 | </span> |
| 70 | {note && <span className="u-num text-[11px] text-neutral-500">{note}</span>} |
| 71 | </div> |
| 72 | {/* 改类事件没有谓词也没有宾语,正文换成类的两端。 |
| 73 | 起点为空 = 从「未分类」改过来,0009 之后最常见的一种 */} |
| 74 | {e.kind === "retyped" || e.kind === "retype_reverted" ? ( |
| 75 | <div className="mt-0.5 text-[12.5px] text-neutral-400 truncate"> |
| 76 | <span className="text-neutral-500"> |
| 77 | {e.from_type_label ?? S.graph.untyped} →{" "} |
| 78 | </span> |
| 79 | <span className="text-neutral-200">{e.to_type_label}</span> |
| 80 | </div> |
| 81 | ) : ( |
| 82 | <div className="mt-0.5 text-[12.5px] text-neutral-400 truncate"> |
| 83 | <span className="text-neutral-500"> |
| 84 | {e.direction === "in" ? "← " : ""} |
| 85 | <span className={e.predicate_label === null ? "italic text-neutral-600" : undefined}> |
| 86 | {e.predicate_label ?? S.graph.unknownPredicate} |
| 87 | </span> |
| 88 | {e.direction === "in" ? "" : " →"} |
| 89 | </span>{" "} |
| 90 | <span className="text-neutral-200">{objectText(e)}</span> |
| 91 | </div> |
| 92 | )} |
| 93 | <div className="mt-0.5 flex items-center gap-1.5 text-[10.5px] text-neutral-600"> |
| 94 | <span className="u-num">{ymd(e.at)}</span> |
| 95 | <span>·</span> |
| 96 | {/* 归因:人名,或引擎(抽取写入 / 时态对账自动闭合) */} |
| 97 | <span>{e.actor_name ?? S.graph.historyEngine}</span> |
| 98 | {e.filename && e.document_id && ( |
| 99 | <> |
| 100 | <span>·</span> |
| 101 | <Link |
| 102 | to="/doc/$docId" |
| 103 | params={{ docId: e.document_id }} |
| 104 | search={{}} |
| 105 | className="truncate hover:text-neutral-300" |
| 106 | title={e.quote ?? e.filename} |
| 107 | > |
| 108 | {e.filename} |
| 109 | </Link> |
| 110 | </> |
| 111 | )} |
| 112 | </div> |
| 113 | </div> |
| 114 | </div> |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | export function EntityHistory({ kbId, entityId }: { kbId: string; entityId: string }) { |
| 119 | const [page, setPage] = useState(0); |
| 120 | useEffect(() => setPage(0), [entityId]); |
| 121 | const q = useQuery({ |
| 122 | queryKey: ["entityHistory", kbId, entityId, page], |
| 123 | queryFn: () => api.entityHistory(kbId, entityId, page, PER), |
| 124 | }); |
| 125 | |
| 126 | const total = q.data?.total ?? 0; |
| 127 | if (q.isPending) return <p className="p-2 text-sm text-neutral-500">{S.nav.loading}</p>; |
| 128 | // 只有"一条都没有"才是空。记录轴上首次断言本身就是一次事件—— |
| 129 | // "我们何时、从哪份文档得知这件事"是这条轴要回答的问题的一半 |
| 130 | if (total === 0) return <p className="p-2 text-xs text-neutral-500">{S.graph.historyEmpty}</p>; |
| 131 | |
| 132 | return ( |
| 133 | <div> |
| 134 | <p className="px-2 pb-1.5 text-[11px] text-neutral-600">{S.graph.historyHint}</p> |
| 135 | <div className="divide-y divide-white/[0.06]"> |
| 136 | {/* key 里用 fact_id ?? at:改类事件没有 fact_id */} |
| 137 | {(q.data?.events ?? []).map((e) => ( |
| 138 | <EventRow key={`${e.fact_id ?? e.at}-${e.kind}`} e={e} /> |
| 139 | ))} |
| 140 | </div> |
| 141 | <Pager total={total} pageSize={PER} page={page} onPage={setPage} /> |
| 142 | </div> |
| 143 | ); |
| 144 | } |