TraceDrawer.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { useEffect, useState } from 'react';
  2. import type { ClaimChip } from '../api/types';
  3. import { api } from '../api/client';
  4. import { Badge, Spinner } from './Common';
  5. import { formatNumber, isPlainIntegerColumn } from '../lib/format';
  6. export interface TraceDrawerProps {
  7. sessionId: string;
  8. chip: ClaimChip | null;
  9. onClose(): void;
  10. /** 跳到"文件"页的结果表并高亮该行 */
  11. onOpenRow(objectId: string, table: string, rowIndex: number): void;
  12. }
  13. interface RowDetail {
  14. object_id: string;
  15. table: string;
  16. row_index: number;
  17. row_hash: string;
  18. values: Record<string, unknown>;
  19. lineage: {
  20. source_object_id?: string;
  21. source_rows?: Record<string, string> | null;
  22. } | null;
  23. }
  24. interface CalcDetail {
  25. call: Record<string, unknown>;
  26. values: Array<Record<string, unknown>>;
  27. }
  28. /**
  29. * 溯源抽屉:点开答案里的数字,看到它来自哪个对象、哪张表、第几行、行哈希,
  30. * 以及这一行的完整原文;如果是 calculator 算出来的,则展示算式与输入。
  31. *
  32. * 这是架构文档里"点击 chip 打开侧边面板,可跳转到结果工作区对应行"的落地。
  33. */
  34. export function TraceDrawer({ sessionId, chip, onClose, onOpenRow }: TraceDrawerProps) {
  35. const [row, setRow] = useState<RowDetail | null>(null);
  36. const [calc, setCalc] = useState<CalcDetail | null>(null);
  37. const [error, setError] = useState('');
  38. const [loading, setLoading] = useState(false);
  39. const isCalculator = chip?.table === 'calculator' || (chip?.row_index ?? 0) < 0;
  40. useEffect(() => {
  41. if (!chip) return;
  42. setRow(null);
  43. setCalc(null);
  44. setError('');
  45. setLoading(true);
  46. let alive = true;
  47. (async () => {
  48. try {
  49. if (isCalculator) {
  50. if (!chip.call_id) throw new Error('这个数字没有 call_id,无法追溯');
  51. const data = (await api.sessionLedgerCall(sessionId, chip.call_id)) as CalcDetail;
  52. if (alive) setCalc(data);
  53. } else if (chip.object_id && chip.table && (chip.row_index ?? -1) >= 0) {
  54. const data = (await api.objectRow(chip.object_id, chip.table, chip.row_index!)) as RowDetail;
  55. if (alive) setRow(data);
  56. } else {
  57. throw new Error('这个数字缺少行位置信息');
  58. }
  59. } catch (caught) {
  60. if (alive) setError((caught as Error).message);
  61. } finally {
  62. if (alive) setLoading(false);
  63. }
  64. })();
  65. return () => {
  66. alive = false;
  67. };
  68. // eslint-disable-next-line react-hooks/exhaustive-deps
  69. }, [chip?.call_id, chip?.object_id, chip?.table, chip?.row_index, sessionId]);
  70. if (!chip) return null;
  71. const statusText = { verified: '已溯源', flagged: '估算', unverified: '无来源', exempt: '豁免' }[chip.status];
  72. const statusTone = chip.status === 'verified' ? 'success' : chip.status === 'flagged' ? 'warn' : chip.status === 'unverified' ? 'danger' : 'neutral';
  73. return (
  74. <div className="drawer-backdrop" onClick={onClose} role="presentation">
  75. <aside className="drawer" onClick={(event) => event.stopPropagation()} role="dialog" aria-modal="true">
  76. <header className="drawer-head">
  77. <div>
  78. <h3>数字溯源</h3>
  79. <span className="drawer-sub">{chip.field ?? '数值'}</span>
  80. </div>
  81. <button type="button" className="icon-button" onClick={onClose} aria-label="关闭">
  82. ×
  83. </button>
  84. </header>
  85. <div className="drawer-body">
  86. <div className="trace-value">
  87. <strong>{chip.display}</strong>
  88. <Badge tone={statusTone}>{statusText}</Badge>
  89. </div>
  90. {loading && <Spinner label="读取来源" />}
  91. {error && <div className="error-box">{error}</div>}
  92. {row && (
  93. <>
  94. <dl className="kv">
  95. <dt>对象</dt>
  96. <dd className="mono">{row.object_id}</dd>
  97. <dt>表</dt>
  98. <dd>{row.table}</dd>
  99. <dt>行号</dt>
  100. <dd>第 {row.row_index + 1} 行(0 基 {row.row_index})</dd>
  101. <dt>行哈希</dt>
  102. <dd className="mono break">{row.row_hash}</dd>
  103. <dt>字段名</dt>
  104. <dd>{chip.field ?? '—'}</dd>
  105. </dl>
  106. <h4>这一行的原文</h4>
  107. <div className="data-table compact">
  108. <table>
  109. <tbody>
  110. {Object.entries(row.values).map(([key, value]) => (
  111. <tr key={key} className={key === chip.field ? 'highlight' : ''}>
  112. <td className="trace-key">{key}</td>
  113. <td className={/金额|数量|指数|单价|占比|环比/.test(key) ? 'num' : ''}>
  114. {value === null || value === ''
  115. ? '—'
  116. : isPlainIntegerColumn(key) || !/金额|数量|指数|单价|占比|环比/.test(key)
  117. ? String(value)
  118. : formatNumber(value)}
  119. </td>
  120. </tr>
  121. ))}
  122. </tbody>
  123. </table>
  124. </div>
  125. {row.lineage?.source_rows && (
  126. <>
  127. <h4>下钻条件(这批记录的过滤条件)</h4>
  128. <pre className="code-block">{JSON.stringify(row.lineage.source_rows, null, 2)}</pre>
  129. <p className="muted">原始数据对象:{row.lineage.source_object_id}</p>
  130. </>
  131. )}
  132. <button type="button" className="primary block" onClick={() => onOpenRow(row.object_id, row.table, row.row_index)}>
  133. 在结果表中查看这一行
  134. </button>
  135. </>
  136. )}
  137. {calc && (
  138. <>
  139. <dl className="kv">
  140. <dt>计算 ID</dt>
  141. <dd className="mono">{String(calc.call.call_id ?? '—')}</dd>
  142. <dt>工具</dt>
  143. <dd>calculator</dd>
  144. <dt>参数</dt>
  145. <dd className="mono break">{JSON.stringify(calc.call.args ?? {}, null, 2)}</dd>
  146. <dt>结果哈希</dt>
  147. <dd className="mono break">{String(calc.call.result_hash ?? '—')}</dd>
  148. </dl>
  149. <h4>这次计算产出的值</h4>
  150. <pre className="code-block">{JSON.stringify(calc.values, null, 2)}</pre>
  151. <p className="muted">换算与合计类数字必须由 calculator 产生才能被核验,这条记录就是它的账。</p>
  152. </>
  153. )}
  154. </div>
  155. </aside>
  156. </div>
  157. );
  158. }