| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { useEffect, useState } from 'react';
- import type { ClaimChip } from '../api/types';
- import { api } from '../api/client';
- import { Badge, Spinner } from './Common';
- import { formatNumber, isPlainIntegerColumn } from '../lib/format';
- export interface TraceDrawerProps {
- sessionId: string;
- chip: ClaimChip | null;
- onClose(): void;
- /** 跳到"文件"页的结果表并高亮该行 */
- onOpenRow(objectId: string, table: string, rowIndex: number): void;
- }
- interface RowDetail {
- object_id: string;
- table: string;
- row_index: number;
- row_hash: string;
- values: Record<string, unknown>;
- lineage: {
- source_object_id?: string;
- source_rows?: Record<string, string> | null;
- } | null;
- }
- interface CalcDetail {
- call: Record<string, unknown>;
- values: Array<Record<string, unknown>>;
- }
- /**
- * 溯源抽屉:点开答案里的数字,看到它来自哪个对象、哪张表、第几行、行哈希,
- * 以及这一行的完整原文;如果是 calculator 算出来的,则展示算式与输入。
- *
- * 这是架构文档里"点击 chip 打开侧边面板,可跳转到结果工作区对应行"的落地。
- */
- export function TraceDrawer({ sessionId, chip, onClose, onOpenRow }: TraceDrawerProps) {
- const [row, setRow] = useState<RowDetail | null>(null);
- const [calc, setCalc] = useState<CalcDetail | null>(null);
- const [error, setError] = useState('');
- const [loading, setLoading] = useState(false);
- const isCalculator = chip?.table === 'calculator' || (chip?.row_index ?? 0) < 0;
- useEffect(() => {
- if (!chip) return;
- setRow(null);
- setCalc(null);
- setError('');
- setLoading(true);
- let alive = true;
- (async () => {
- try {
- if (isCalculator) {
- if (!chip.call_id) throw new Error('这个数字没有 call_id,无法追溯');
- const data = (await api.sessionLedgerCall(sessionId, chip.call_id)) as CalcDetail;
- if (alive) setCalc(data);
- } else if (chip.object_id && chip.table && (chip.row_index ?? -1) >= 0) {
- const data = (await api.objectRow(chip.object_id, chip.table, chip.row_index!)) as RowDetail;
- if (alive) setRow(data);
- } else {
- throw new Error('这个数字缺少行位置信息');
- }
- } catch (caught) {
- if (alive) setError((caught as Error).message);
- } finally {
- if (alive) setLoading(false);
- }
- })();
- return () => {
- alive = false;
- };
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [chip?.call_id, chip?.object_id, chip?.table, chip?.row_index, sessionId]);
- if (!chip) return null;
- const statusText = { verified: '已溯源', flagged: '估算', unverified: '无来源', exempt: '豁免' }[chip.status];
- const statusTone = chip.status === 'verified' ? 'success' : chip.status === 'flagged' ? 'warn' : chip.status === 'unverified' ? 'danger' : 'neutral';
- return (
- <div className="drawer-backdrop" onClick={onClose} role="presentation">
- <aside className="drawer" onClick={(event) => event.stopPropagation()} role="dialog" aria-modal="true">
- <header className="drawer-head">
- <div>
- <h3>数字溯源</h3>
- <span className="drawer-sub">{chip.field ?? '数值'}</span>
- </div>
- <button type="button" className="icon-button" onClick={onClose} aria-label="关闭">
- ×
- </button>
- </header>
- <div className="drawer-body">
- <div className="trace-value">
- <strong>{chip.display}</strong>
- <Badge tone={statusTone}>{statusText}</Badge>
- </div>
- {loading && <Spinner label="读取来源" />}
- {error && <div className="error-box">{error}</div>}
- {row && (
- <>
- <dl className="kv">
- <dt>对象</dt>
- <dd className="mono">{row.object_id}</dd>
- <dt>表</dt>
- <dd>{row.table}</dd>
- <dt>行号</dt>
- <dd>第 {row.row_index + 1} 行(0 基 {row.row_index})</dd>
- <dt>行哈希</dt>
- <dd className="mono break">{row.row_hash}</dd>
- <dt>字段名</dt>
- <dd>{chip.field ?? '—'}</dd>
- </dl>
- <h4>这一行的原文</h4>
- <div className="data-table compact">
- <table>
- <tbody>
- {Object.entries(row.values).map(([key, value]) => (
- <tr key={key} className={key === chip.field ? 'highlight' : ''}>
- <td className="trace-key">{key}</td>
- <td className={/金额|数量|指数|单价|占比|环比/.test(key) ? 'num' : ''}>
- {value === null || value === ''
- ? '—'
- : isPlainIntegerColumn(key) || !/金额|数量|指数|单价|占比|环比/.test(key)
- ? String(value)
- : formatNumber(value)}
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- {row.lineage?.source_rows && (
- <>
- <h4>下钻条件(这批记录的过滤条件)</h4>
- <pre className="code-block">{JSON.stringify(row.lineage.source_rows, null, 2)}</pre>
- <p className="muted">原始数据对象:{row.lineage.source_object_id}</p>
- </>
- )}
- <button type="button" className="primary block" onClick={() => onOpenRow(row.object_id, row.table, row.row_index)}>
- 在结果表中查看这一行
- </button>
- </>
- )}
- {calc && (
- <>
- <dl className="kv">
- <dt>计算 ID</dt>
- <dd className="mono">{String(calc.call.call_id ?? '—')}</dd>
- <dt>工具</dt>
- <dd>calculator</dd>
- <dt>参数</dt>
- <dd className="mono break">{JSON.stringify(calc.call.args ?? {}, null, 2)}</dd>
- <dt>结果哈希</dt>
- <dd className="mono break">{String(calc.call.result_hash ?? '—')}</dd>
- </dl>
- <h4>这次计算产出的值</h4>
- <pre className="code-block">{JSON.stringify(calc.values, null, 2)}</pre>
- <p className="muted">换算与合计类数字必须由 calculator 产生才能被核验,这条记录就是它的账。</p>
- </>
- )}
- </div>
- </aside>
- </div>
- );
- }
|