dsh-token-dash 2.3.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +8 -5
- package/README.md +9 -5
- package/client.js +450 -133
- package/index.js +86 -17
- package/locale/en.json +5 -0
- package/locale/zh.json +5 -0
- package/package.json +3 -2
- package/src/adapters.js +608 -561
- package/src/core.js +936 -887
- package/src/diagnostics.js +160 -108
- package/src/quota-sources.js +228 -224
- package/src/version.js +44 -37
package/client.js
CHANGED
|
@@ -11,6 +11,36 @@ window.__ModuleLoader__.load({
|
|
|
11
11
|
|
|
12
12
|
const API = '/token-dash/api'
|
|
13
13
|
const PKG = 'dsh-token-dash'
|
|
14
|
+
|
|
15
|
+
// 客户端事件上报:走宿主 /log 落到本机日志。三层限流防止「增强本身变成故障源」。
|
|
16
|
+
const CLIENT_LOG_PER_EVENT = 20
|
|
17
|
+
const CLIENT_LOG_PER_HOUR = 100
|
|
18
|
+
const clientLogState = { seen: new Set(), perEvent: new Map(), windowStart: 0, total: 0 }
|
|
19
|
+
|
|
20
|
+
function reportClientLog(module, event, meta) {
|
|
21
|
+
try {
|
|
22
|
+
const now = Date.now()
|
|
23
|
+
const state = clientLogState
|
|
24
|
+
if (now - state.windowStart > 3600_000) {
|
|
25
|
+
state.windowStart = now
|
|
26
|
+
state.perEvent.clear()
|
|
27
|
+
state.seen.clear()
|
|
28
|
+
state.total = 0
|
|
29
|
+
}
|
|
30
|
+
const key = `${event}|${meta?.message ?? ''}|${meta?.path ?? ''}`
|
|
31
|
+
if (state.seen.has(key)) return
|
|
32
|
+
if ((state.perEvent.get(event) ?? 0) >= CLIENT_LOG_PER_EVENT) return
|
|
33
|
+
if (state.total >= CLIENT_LOG_PER_HOUR) return
|
|
34
|
+
state.seen.add(key)
|
|
35
|
+
state.perEvent.set(event, (state.perEvent.get(event) ?? 0) + 1)
|
|
36
|
+
state.total += 1
|
|
37
|
+
void fetch(`${API}/log`, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
headers: { 'content-type': 'application/json' },
|
|
40
|
+
body: JSON.stringify({ module, event, meta }),
|
|
41
|
+
}).catch(() => {})
|
|
42
|
+
} catch { /* 日志失败绝不影响渲染 */ }
|
|
43
|
+
}
|
|
14
44
|
const PAGE_ID = 'token-dash'
|
|
15
45
|
const REPO_URL = 'https://github.com/spartawhy117/dsh-token-dash'
|
|
16
46
|
const SUB_CRITICAL = 90
|
|
@@ -44,6 +74,11 @@ window.__ModuleLoader__.load({
|
|
|
44
74
|
'30秒': '30s', '1分钟': '1m', '5分钟': '5m',
|
|
45
75
|
'社区': 'Community', '内置兜底': 'Built-in fallback',
|
|
46
76
|
'查询失败': 'Query failed', '套餐查询失败': 'Quota query failed',
|
|
77
|
+
'数据加载中': 'Loading data',
|
|
78
|
+
'含估算': 'incl. estimates',
|
|
79
|
+
'API 与订阅金额均为本地按目录价估算,非供应商账单;缓存读按缓存价计,命中率高的行每 token 成本显著更低,不同行不可直接按 token 数比较;不同币种需自行换算': 'Both API and subscription amounts are estimated locally from catalog prices, not provider invoices; cached reads use the cache price, so rows with a higher hit rate cost much less per token and rows are not directly comparable by token count; currencies are not converted.',
|
|
80
|
+
'本月估算': 'Est. this month',
|
|
81
|
+
'订阅不计价,估算值仅作参考': 'Subscription is not billed; the estimate is for reference only',
|
|
47
82
|
'周': 'Week', '月': 'Month', '重置': 'Reset',
|
|
48
83
|
'API 用量': 'API usage', '订阅': 'Subscription', '账户套餐': 'Account plan',
|
|
49
84
|
'预算': 'Budget', '未设置': 'Not set', '今日 {value}': 'Today {value}',
|
|
@@ -59,9 +94,9 @@ window.__ModuleLoader__.load({
|
|
|
59
94
|
'API 用量(按供应商)': 'API usage (by provider)',
|
|
60
95
|
'订阅套餐(按供应商)': 'Subscription plans (by provider)',
|
|
61
96
|
'名称': 'Name', '已用': 'Used', '已用 / 预算': 'Used / budget', '计划余额': 'Planned left', '真实余额': 'Real balance',
|
|
62
|
-
'
|
|
97
|
+
'本月已花': 'Spent this month',
|
|
63
98
|
'今日': 'Today', '总费用': 'Total cost', '重试': 'Retry',
|
|
64
|
-
'模型用量明细': 'Model usage details', '模型': 'Model', '共 {n} 个模型': 'Total {n} models', '窗': 'windows', '数据来源': 'Data source', '数据来自本机会话记录,费用与 Tokens 均在本地估算': 'Estimated locally from local session records', '范围与明细': 'Range & details', '输入': 'In', '缓存': 'Cache', '输出': 'Out', '费用': 'Cost',
|
|
99
|
+
'模型用量明细': 'Model usage details', '模型': 'Model', '共 {n} 个模型': 'Total {n} models', '窗': 'windows', '数据待更新': 'Data may be stale', '探测失败,暂用上次数据': 'Probe failed; showing last data', '数据来源': 'Data source', '数据来自本机会话记录,费用与 Tokens 均在本地估算': 'Estimated locally from local session records', '范围与明细': 'Range & details', '输入': 'In', '缓存': 'Cache', '输出': 'Out', '费用': 'Cost',
|
|
65
100
|
'费用(API 计价估算)': 'Cost (API estimate)',
|
|
66
101
|
'重新检测订阅配额': 'Recheck subscription quota',
|
|
67
102
|
'未检测到已配置供应商': 'No configured provider detected',
|
|
@@ -158,6 +193,8 @@ window.__ModuleLoader__.load({
|
|
|
158
193
|
muted: 'var(--dsw-alias-label-secondary, rgba(235,235,235,.65))',
|
|
159
194
|
weak: 'var(--dsw-alias-label-tertiary, rgba(235,235,235,.45))',
|
|
160
195
|
accent: 'var(--dsw-alias-state-business-primary, #6ea8ff)',
|
|
196
|
+
// 第二条数据序列(双轴图的次币种柱):独立色相,不借用绿/黄/红等状态语义色
|
|
197
|
+
series2: 'var(--td-series-2, #c9a5ff)',
|
|
161
198
|
warn: 'var(--dsw-alias-state-warn-primary, #f0a020)',
|
|
162
199
|
over: 'var(--dsw-alias-state-error-primary, #e05a5a)',
|
|
163
200
|
ok: 'var(--td-state-ok, var(--dsw-alias-state-success-primary, #4ed17e))',
|
|
@@ -243,7 +280,8 @@ window.__ModuleLoader__.load({
|
|
|
243
280
|
}
|
|
244
281
|
|
|
245
282
|
async function getJson(path) {
|
|
246
|
-
|
|
283
|
+
// 10 秒超时:把「一直等」转成可重试的失败态,避免长时间无反馈(REQ-07)
|
|
284
|
+
const res = await fetch(`${API}${path}`, { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(10_000) })
|
|
247
285
|
const body = await res.json().catch(() => ({}))
|
|
248
286
|
if (!res.ok || body?.ok !== true) throw new Error(body?.reason ?? `http-${res.status}`)
|
|
249
287
|
return body.data
|
|
@@ -425,7 +463,11 @@ window.__ModuleLoader__.load({
|
|
|
425
463
|
|
|
426
464
|
const styles = {
|
|
427
465
|
pill: {
|
|
466
|
+
// 字数与行高必须显式声明:pill 挂在宿主底栏,不声明会继承宿主的 16px,
|
|
467
|
+
// 导致前缀/数值/分隔符比标签大出一档,并让同一行出现多条文字基线(P1 排版对齐)。
|
|
468
|
+
// 12px / 16px 行高与 ui-design.md §2.6 的 --td-fs-pill、§3「P1 字号 12px」一致。
|
|
428
469
|
display: 'inline-flex', alignItems: 'center', gap: 6, height: 24, padding: '0 8px',
|
|
470
|
+
boxSizing: 'border-box', fontSize: 12, lineHeight: '16px',
|
|
429
471
|
borderRadius: 999, cursor: 'pointer', maxWidth: '100%', fontVariantNumeric: 'tabular-nums',
|
|
430
472
|
border: `1px solid ${T.border}`, color: T.text,
|
|
431
473
|
},
|
|
@@ -449,6 +491,14 @@ window.__ModuleLoader__.load({
|
|
|
449
491
|
header: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, minHeight: 28 },
|
|
450
492
|
headerTabs: { flex: 1, minWidth: 0 },
|
|
451
493
|
headerActions: { display: 'flex', alignItems: 'center', gap: 4, flex: '0 0 auto' },
|
|
494
|
+
// 失败态「重试」:实红描边而非淡色,与蓝色关闭按钮区分(Issue #4)
|
|
495
|
+
retryChip: {
|
|
496
|
+
display: 'inline-flex', alignItems: 'center', gap: 4, height: 22, minHeight: 22, padding: '0 7px',
|
|
497
|
+
boxSizing: 'border-box', borderRadius: 6, font: 'inherit', fontSize: 11, fontWeight: 600,
|
|
498
|
+
cursor: 'pointer', color: T.over,
|
|
499
|
+
border: `1px solid ${T.over}`,
|
|
500
|
+
background: `color-mix(in srgb, ${T.over} 14%, transparent)`,
|
|
501
|
+
},
|
|
452
502
|
row: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 },
|
|
453
503
|
rowStart: { display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap' },
|
|
454
504
|
muted: { color: T.muted, fontSize: 11 },
|
|
@@ -502,12 +552,18 @@ window.__ModuleLoader__.load({
|
|
|
502
552
|
card: { background: T.section, borderRadius: 10, padding: 10, marginTop: 10, display: 'grid', gap: 8 },
|
|
503
553
|
link: { color: T.text, textDecoration: 'underline', cursor: 'pointer' },
|
|
504
554
|
status: { minHeight: 15, fontSize: 10.5, color: T.muted, textAlign: 'right' },
|
|
505
|
-
|
|
555
|
+
// 三栏制(Issue #2):左(环)贴左 / 中(已用·预算)居中 / 右(真实余额)贴右。
|
|
556
|
+
// 中栏内容最长(¥91.90 / ¥100.00),给 1.5fr 避免换行;左右两栏内容短,1fr 仍可贴边。
|
|
557
|
+
summaryRow: { display: 'grid', gridTemplateColumns: 'minmax(0,1fr) minmax(0,1.5fr) minmax(0,1fr)', alignItems: 'center', columnGap: 12, minHeight: 24, fontSize: 12 },
|
|
506
558
|
summaryCell: { display: 'flex', flexDirection: 'column', gap: 4, minWidth: 0, whiteSpace: 'nowrap' },
|
|
559
|
+
// P1 专用标签:summaryLabel 的 11px 被 P2 共用(ui-design.md §4.3 要求 P2 标签 11px),
|
|
560
|
+
// P1 需要 12px,故单独一份,避免改一处把 P2 也带过去。
|
|
561
|
+
pillLabel: { color: T.muted, fontSize: 12, lineHeight: '16px' },
|
|
507
562
|
summaryLabel: { color: T.muted, fontSize: 11 },
|
|
508
563
|
summaryValue: { fontSize: 12, fontWeight: 600, fontVariantNumeric: 'tabular-nums' },
|
|
509
564
|
providerLine: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, minWidth: 0, minHeight: 24 },
|
|
510
565
|
providerChip: { color: T.muted, fontSize: 12, whiteSpace: 'nowrap' },
|
|
566
|
+
staleBadge: { color: T.weak, fontSize: 11, whiteSpace: 'nowrap', border: `1px solid ${T.border}`, borderRadius: 4, padding: '0 4px' },
|
|
511
567
|
windowRow: { display: 'grid', gridTemplateColumns: 'auto 1fr 1fr 1fr', columnGap: 8, alignItems: 'center', minHeight: 24, fontSize: 13 },
|
|
512
568
|
windowKey: { display: 'inline-flex', alignItems: 'center', gap: 6, whiteSpace: 'nowrap' },
|
|
513
569
|
table: { display: 'grid', minWidth: 0 },
|
|
@@ -565,13 +621,15 @@ window.__ModuleLoader__.load({
|
|
|
565
621
|
display: 'inline-flex', alignItems: 'center', gap: 4, height: 24, padding: '0 8px', borderRadius: 999,
|
|
566
622
|
border: `1px solid ${T.border}`, color: T.text, textDecoration: 'none', fontSize: 12, whiteSpace: 'nowrap',
|
|
567
623
|
},
|
|
624
|
+
// P1 订阅段容器:窗口组之间单侧视觉留白 = gap 4 + 分隔符盒宽/2 − 墨迹宽/2 = 7px(Issue #1)
|
|
625
|
+
pillGroup: { display: 'inline-flex', alignItems: 'center', gap: 4 },
|
|
568
626
|
inlineWrap: { display: 'inline-flex', alignItems: 'center', gap: 4 },
|
|
569
627
|
gaugeMeta: { color: T.muted, fontSize: 11, lineHeight: '15px', whiteSpace: 'nowrap' },
|
|
570
628
|
apiCards: { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: 12 },
|
|
571
629
|
apiCard: {
|
|
572
630
|
flex: '1 1 220px', maxWidth: 260, minWidth: 220, boxSizing: 'border-box',
|
|
573
631
|
border: `1px solid ${T.border}`, borderRadius: 8, background: T.section, padding: 16,
|
|
574
|
-
display: 'grid', gap: 8, alignContent: 'start',
|
|
632
|
+
display: 'grid', gridTemplateColumns: 'minmax(0, 1fr)', gap: 8, alignContent: 'start',
|
|
575
633
|
},
|
|
576
634
|
apiCardHead: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8, minWidth: 0 },
|
|
577
635
|
apiCardName: { fontSize: 13, fontWeight: 600, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' },
|
|
@@ -618,6 +676,13 @@ window.__ModuleLoader__.load({
|
|
|
618
676
|
trendSpacer: { flex: '1 1 auto' },
|
|
619
677
|
// 轴与绘图区两列:轴在绘图区之外(与 CC Switch 的 <YAxis> 同构),绘图区自己承担网格与柱
|
|
620
678
|
trendPlot: { display: 'grid', gridTemplateColumns: 'auto minmax(0, 1fr)', height: 132, minWidth: 0 },
|
|
679
|
+
trendPlotDual: { gridTemplateColumns: 'auto minmax(0, 1fr) auto' },
|
|
680
|
+
trendAxisColRight: { paddingRight: 0, paddingLeft: 6 },
|
|
681
|
+
trendAxisRight: {
|
|
682
|
+
position: 'absolute', left: 0, fontSize: 11, color: T.weak, fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap',
|
|
683
|
+
},
|
|
684
|
+
trendLegend: { display: 'flex', alignItems: 'center', gap: 6, marginRight: 8, fontSize: 11, color: T.muted, whiteSpace: 'nowrap' },
|
|
685
|
+
trendLegendDot: { width: 8, height: 8, borderRadius: 999, display: 'inline-block', marginRight: 3 },
|
|
621
686
|
trendAxisCol: { position: 'relative', minWidth: 0, paddingRight: 6 },
|
|
622
687
|
trendAxis: {
|
|
623
688
|
position: 'absolute', right: 6, color: T.weak, fontSize: 11, lineHeight: '14px',
|
|
@@ -673,13 +738,16 @@ window.__ModuleLoader__.load({
|
|
|
673
738
|
},
|
|
674
739
|
// 空态文案:绘图区内居中(轴已在左列,无需再让出留白)
|
|
675
740
|
trendMsg: { position: 'absolute', inset: '0 0 18px 0', display: 'flex', alignItems: 'center', justifyContent: 'center', color: T.muted, fontSize: 11 },
|
|
676
|
-
|
|
741
|
+
// 订阅卡与按量卡同一套宽度规则:flex 等分 + 上限 260,单家时不再铺满
|
|
742
|
+
quotaCards: { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', alignItems: 'start', gap: 12 },
|
|
677
743
|
quotaCard: {
|
|
678
744
|
border: `1px solid ${T.border}`, borderRadius: 8, background: T.section, padding: 16,
|
|
679
|
-
display: 'grid', gap: 12, alignContent: 'start', minWidth: 0,
|
|
745
|
+
display: 'grid', gridTemplateColumns: 'minmax(0, 1fr)', gap: 12, alignContent: 'start', minWidth: 0,
|
|
746
|
+
flex: '1 1 220px', maxWidth: 260, boxSizing: 'border-box',
|
|
680
747
|
},
|
|
681
748
|
quotaWindows: { display: 'grid', gap: 12, minWidth: 0 },
|
|
682
|
-
|
|
749
|
+
// 说明行可能很长(周期 / 限流 / 渠道用量叠加),必须允许换行,否则会把卡片撑宽(Issue #4)
|
|
750
|
+
cardCaption: { color: T.weak, fontSize: 11, textAlign: 'center', marginTop: 4, whiteSpace: 'normal', overflowWrap: 'anywhere' },
|
|
683
751
|
rangeCapsule: { width: '100%', minWidth: 0 },
|
|
684
752
|
}
|
|
685
753
|
styles.button['--td-accent'] = T.accent
|
|
@@ -692,6 +760,35 @@ window.__ModuleLoader__.load({
|
|
|
692
760
|
styles.ghost = { ...styles.button, color: T.accent, borderColor: 'transparent', background: 'transparent' }
|
|
693
761
|
styles.outline = { ...styles.button, color: T.text, borderColor: T.border, background: 'transparent' }
|
|
694
762
|
|
|
763
|
+
/** 看板渲染兜底:异常时给可读文案 + 重试,而不是整块空白;并把 componentStack 上报落盘。 */
|
|
764
|
+
class BoardBoundary extends React.Component {
|
|
765
|
+
constructor(props) {
|
|
766
|
+
super(props)
|
|
767
|
+
this.state = { failed: false }
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
static getDerivedStateFromError() { return { failed: true } }
|
|
771
|
+
|
|
772
|
+
componentDidCatch(error, info) {
|
|
773
|
+
reportClientLog('board', 'render.error', {
|
|
774
|
+
message: String(error?.message ?? error).slice(0, 200),
|
|
775
|
+
stack: String(info?.componentStack ?? '').slice(0, 400),
|
|
776
|
+
})
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
render() {
|
|
780
|
+
if (this.state.failed) {
|
|
781
|
+
return h('div', { 'data-testid': 'token-dash-board-error', style: styles.cardBox },
|
|
782
|
+
h('div', { style: styles.empty }, tr('数据渲染失败')),
|
|
783
|
+
h('button', {
|
|
784
|
+
type: 'button', style: styles.button,
|
|
785
|
+
onClick: () => this.setState({ failed: false }),
|
|
786
|
+
}, tr('重试')))
|
|
787
|
+
}
|
|
788
|
+
return this.props.children
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
|
|
695
792
|
function Icon({ name, size = 14, style }) {
|
|
696
793
|
const props = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, style }
|
|
697
794
|
if (name === 'info') return h('svg', props, h('circle', { cx: 12, cy: 12, r: 9 }), h('path', { d: 'M12 11v5' }), h('path', { d: 'M12 8h.01' }))
|
|
@@ -891,29 +988,65 @@ window.__ModuleLoader__.load({
|
|
|
891
988
|
}))
|
|
892
989
|
}
|
|
893
990
|
|
|
894
|
-
/**
|
|
895
|
-
|
|
991
|
+
/**
|
|
992
|
+
* 百分比文本:只负责格式化与状态色,不管排版。
|
|
993
|
+
*
|
|
994
|
+
* 垂直对齐(Issue #6):字形盒在行盒里并非垂直居中——上半部分是 ascender 空间,
|
|
995
|
+
* 数字实际贴着基线落在偏上位置。只压 line-height 无改善(实测仍差 1.50px),
|
|
996
|
+
* 因此在底部补 2px:把字形整体顶回环心,实测残差 0.50px(三档尺寸一致)。
|
|
997
|
+
* 这是字形度量的补偿,不是视觉微调;改字号时该值需按 font-size 等比复核。
|
|
998
|
+
*/
|
|
999
|
+
/**
|
|
1000
|
+
* `lineHeight` 只接受带单位的字符串:React 把 lineHeight 视作无单位属性,
|
|
1001
|
+
* 传数字 16 会被当作「16 倍」而算出 16 × fontSize(实测 192px),故入口统一归一化。
|
|
1002
|
+
*/
|
|
1003
|
+
function PercentValue({ percent, state = stateOf(percent), size = 12, lineHeight, testid }) {
|
|
1004
|
+
const lineHeightCss = lineHeight === undefined
|
|
1005
|
+
? undefined
|
|
1006
|
+
: (typeof lineHeight === 'number' ? `${lineHeight}px` : lineHeight)
|
|
1007
|
+
// P1 传 lineHeight 与 pill 的 16px 行盒统一,且此时不再补 paddingBottom:
|
|
1008
|
+
// 全行同字号时补偿会把该文本整体上移 1px,反而与相邻标签错开(实测基线 17→16)。
|
|
896
1009
|
return h('b', {
|
|
897
1010
|
'data-testid': testid,
|
|
898
|
-
style: {
|
|
1011
|
+
style: {
|
|
1012
|
+
fontSize: size, fontWeight: 600, color: stateColor(state),
|
|
1013
|
+
fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap',
|
|
1014
|
+
...(lineHeightCss === undefined ? { lineHeight: 1, paddingBottom: 2 } : { lineHeight: lineHeightCss }),
|
|
1015
|
+
},
|
|
899
1016
|
}, percentText(percent))
|
|
900
1017
|
}
|
|
901
1018
|
|
|
902
|
-
/**
|
|
903
|
-
|
|
1019
|
+
/**
|
|
1020
|
+
* 内联组合:环 + 右侧数字,P1 14/2、P2 20/2.5。
|
|
1021
|
+
*
|
|
1022
|
+
* `fontSize` 是 P1 排版对齐的入参:P1 需要环内数字与 pill 其余文字同为 12px,
|
|
1023
|
+
* 且**不带** PercentValue 那 2px 字形补偿——补偿会把该文本框整体上移 1px,
|
|
1024
|
+
* 在「全行同字号」时反而制造基线差。不传该参数时保持原有行为,P2 / P3 不受影响。
|
|
1025
|
+
*/
|
|
1026
|
+
function InlineRing({ percent, state = stateOf(percent), size = 14, stroke = 2, text, fontSize, percentLineHeight, ringShift, testid, percentTestid }) {
|
|
904
1027
|
return h('span', {
|
|
905
1028
|
'data-testid': testid,
|
|
906
1029
|
style: { display: 'inline-flex', alignItems: 'center', gap: 4, minWidth: 0 },
|
|
907
1030
|
},
|
|
908
|
-
h(
|
|
1031
|
+
h('span', {
|
|
1032
|
+
// P1 传 ringShift:环下移 0.75px,让环墨迹中心对齐同一行文字的墨迹中心(Issue #1)。
|
|
1033
|
+
// 移的是 SVG 图形,不涉及字体度量,换字体后依然成立。
|
|
1034
|
+
style: ringShift === undefined ? undefined : { display: 'inline-flex', position: 'relative', top: ringShift, flex: 'none' },
|
|
1035
|
+
}, h(ProgressRing, { percent, state, size, stroke })),
|
|
909
1036
|
text !== undefined
|
|
910
|
-
? h('b', {
|
|
911
|
-
|
|
1037
|
+
? h('b', {
|
|
1038
|
+
'data-testid': percentTestid,
|
|
1039
|
+
style: {
|
|
1040
|
+
fontWeight: 600, color: stateColor(state), lineHeight: 1,
|
|
1041
|
+
...(fontSize === undefined ? { paddingBottom: 2 } : { fontSize }),
|
|
1042
|
+
},
|
|
1043
|
+
}, text)
|
|
1044
|
+
: h(PercentValue, { percent, state, size: 12, lineHeight: percentLineHeight, testid: percentTestid }))
|
|
912
1045
|
}
|
|
913
1046
|
|
|
914
|
-
/** P3 组合:环心标签 + 环下百分比 +
|
|
915
|
-
function QuotaGauge({ label, percent, state = stateOf(percent),
|
|
916
|
-
const aria = [label, percentText(percent),
|
|
1047
|
+
/** P3 组合:环心标签 + 环下百分比 + 两行辅助(倒计时 / 绝对时间)。 */
|
|
1048
|
+
function QuotaGauge({ label, percent, state = stateOf(percent), relative, absolute, size = 40, stroke = 4, testid, labelTestid, percentTestid, relativeTestid, absoluteTestid }) {
|
|
1049
|
+
const aria = [label, percentText(percent), relative].filter((part) => part !== undefined && part !== null && part !== '').join(',')
|
|
917
1050
|
return h('div', {
|
|
918
1051
|
'data-testid': testid, role: 'group', 'aria-label': aria,
|
|
919
1052
|
style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 4, minWidth: 0 },
|
|
@@ -927,13 +1060,14 @@ window.__ModuleLoader__.load({
|
|
|
927
1060
|
style: {
|
|
928
1061
|
position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
929
1062
|
fontSize: 12, fontWeight: 600, color: T.muted, letterSpacing: '.2px',
|
|
1063
|
+
// 与 PercentValue 同源的字形补偿:字形在行盒内偏上,补 2px 才光学居中(Issue #6)
|
|
1064
|
+
lineHeight: 1, paddingBottom: 2,
|
|
930
1065
|
},
|
|
931
1066
|
}, label)),
|
|
932
1067
|
h('span', {
|
|
933
1068
|
'data-testid': percentTestid,
|
|
934
1069
|
style: { fontSize: 16, fontWeight: 600, lineHeight: '20px', color: stateColor(state), fontVariantNumeric: 'tabular-nums', whiteSpace: 'nowrap' },
|
|
935
1070
|
}, percentText(percent)),
|
|
936
|
-
remaining === undefined ? null : h('span', { 'data-testid': remainingTestid, style: styles.gaugeMeta }, remaining),
|
|
937
1071
|
relative === undefined ? null : h('span', { 'data-testid': relativeTestid, style: styles.gaugeMeta }, relative),
|
|
938
1072
|
absolute === undefined ? null : h('span', { 'data-testid': absoluteTestid, style: styles.gaugeMeta }, absolute))
|
|
939
1073
|
}
|
|
@@ -945,11 +1079,14 @@ window.__ModuleLoader__.load({
|
|
|
945
1079
|
|
|
946
1080
|
function TokenTable({ rows, maxRows = 5, empty = tr('范围内暂无消耗') }) {
|
|
947
1081
|
if (rows.length === 0) return h('div', { style: styles.empty }, empty)
|
|
1082
|
+
// 分隔线不能画在每个 cell 上:grid 的 columnGap 处没有元素,
|
|
1083
|
+
// 逐 cell 画边框会把一条线切成 4 段并各留断口,数据行断口位置又不同,
|
|
1084
|
+
// 表头线与数据线因此不等长(Issue #3)。改由跨列行承载。
|
|
948
1085
|
const headCell = (text, align = 'right') => h('span', {
|
|
949
1086
|
role: 'columnheader',
|
|
950
|
-
style: { color: T.weak, fontSize: 12, textAlign: align, paddingBottom: 6
|
|
1087
|
+
style: { color: T.weak, fontSize: 12, textAlign: align, paddingBottom: 6 },
|
|
951
1088
|
}, text)
|
|
952
|
-
const cell = { padding: '5px 0',
|
|
1089
|
+
const cell = { padding: '5px 0', fontSize: 12, minWidth: 0, borderBottom: TABLE_BORDER }
|
|
953
1090
|
return h('div', {
|
|
954
1091
|
'data-testid': 'token-dash-usage-table',
|
|
955
1092
|
role: 'table',
|
|
@@ -957,6 +1094,11 @@ window.__ModuleLoader__.load({
|
|
|
957
1094
|
},
|
|
958
1095
|
h('div', { style: { display: 'contents' } },
|
|
959
1096
|
headCell(tr('模型'), 'left'), headCell('Tokens'), headCell(tr('请求')), headCell(tr('命中'))),
|
|
1097
|
+
// 表头分隔线:跨满全部列,不受 columnGap 影响
|
|
1098
|
+
h('span', {
|
|
1099
|
+
'aria-hidden': true,
|
|
1100
|
+
style: { gridColumn: '1 / -1', display: 'block', height: 1, background: T.border, marginBottom: 4 },
|
|
1101
|
+
}),
|
|
960
1102
|
...rows.map((row) => h('div', {
|
|
961
1103
|
key: `${row.providerId}:${row.modelId}`,
|
|
962
1104
|
role: 'row',
|
|
@@ -988,8 +1130,9 @@ window.__ModuleLoader__.load({
|
|
|
988
1130
|
/** P3 明细表:八列,费用只对 API 计价行展示。 */
|
|
989
1131
|
function DetailTable({ rows, providerNames = {} }) {
|
|
990
1132
|
if (rows.length === 0) return h('div', { style: styles.empty }, tr('范围内暂无消耗'))
|
|
991
|
-
const headCell = (text, align = 'right') => h('span', {
|
|
1133
|
+
const headCell = (text, align = 'right', title) => h('span', {
|
|
992
1134
|
role: 'columnheader',
|
|
1135
|
+
...(title === undefined ? {} : { title }),
|
|
993
1136
|
style: { color: T.weak, fontSize: 11, textAlign: align, paddingBottom: 8, borderBottom: `1px solid ${T.border}` },
|
|
994
1137
|
}, text)
|
|
995
1138
|
const cell = { padding: '4px 0', borderBottom: TABLE_BORDER, fontSize: 13, minWidth: 0 }
|
|
@@ -998,7 +1141,9 @@ window.__ModuleLoader__.load({
|
|
|
998
1141
|
h('div', { style: { display: 'contents' } },
|
|
999
1142
|
headCell(tr('模型'), 'left'),
|
|
1000
1143
|
headCell('Tokens'), headCell(tr('输入')), headCell(tr('缓存')), headCell(tr('输出')),
|
|
1001
|
-
headCell(tr('请求')), headCell(tr('命中')),
|
|
1144
|
+
headCell(tr('请求')), headCell(tr('命中')),
|
|
1145
|
+
headCell(h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 4 } },
|
|
1146
|
+
tr('费用'), h(Icon, { name: 'info', size: 11 })), 'right', tr('API 与订阅金额均为本地按目录价估算,非供应商账单;缓存读按缓存价计,命中率高的行每 token 成本显著更低,不同行不可直接按 token 数比较;不同币种需自行换算'))),
|
|
1002
1147
|
...rows.map((row) => h('div', {
|
|
1003
1148
|
key: `${row.providerId}:${row.modelId}`,
|
|
1004
1149
|
role: 'row',
|
|
@@ -1027,11 +1172,17 @@ window.__ModuleLoader__.load({
|
|
|
1027
1172
|
: undefined,
|
|
1028
1173
|
style: { ...styles.cellNum, ...cell, textAlign: 'right' },
|
|
1029
1174
|
},
|
|
1030
|
-
row.kind !== 'api'
|
|
1031
|
-
?
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1175
|
+
row.kind !== 'api'
|
|
1176
|
+
? (row.estimatedPriced === undefined
|
|
1177
|
+
? '—'
|
|
1178
|
+
: row.estimatedPriced === true
|
|
1179
|
+
? `≈${fmtMoney(row.estimatedCost, row.currency)}`
|
|
1180
|
+
: tr('未定价'))
|
|
1181
|
+
: row.priced === 'empty'
|
|
1182
|
+
? '—'
|
|
1183
|
+
: row.priced === false
|
|
1184
|
+
? tr('未定价')
|
|
1185
|
+
: `≈${fmtMoney(row.cost, row.currency)}`)))))
|
|
1035
1186
|
}
|
|
1036
1187
|
|
|
1037
1188
|
/** P3 趋势指标:唯一顺序源——四格摘要带与右侧胶囊都从这里派生,禁止各自维护顺序。 */
|
|
@@ -1137,6 +1288,7 @@ window.__ModuleLoader__.load({
|
|
|
1137
1288
|
const costEntries = Object.entries(usage?.totals ?? {})
|
|
1138
1289
|
const total = usage?.total ?? null
|
|
1139
1290
|
const dash = '—'
|
|
1291
|
+
// 总费用含订阅估算:金额不加前缀,口径说明只由副行「含估算」承担(REQ-10,修正 D-04 的一半)
|
|
1140
1292
|
const costNodes = costEntries.map(([currency, value]) => h('span', { key: currency }, fmtMoney(value.cost, currency)))
|
|
1141
1293
|
const tokensLabel = h('span', null,
|
|
1142
1294
|
tr('真实 Tokens'),
|
|
@@ -1157,7 +1309,7 @@ window.__ModuleLoader__.load({
|
|
|
1157
1309
|
label: tr('总费用'),
|
|
1158
1310
|
main: () => h('div', { style: styles.summaryV },
|
|
1159
1311
|
pending ? dash : (costNodes.length === 0 ? dash : costNodes)),
|
|
1160
|
-
sub: () => (pending ? dash : tr('{n} 个币种', { n: costEntries.length })),
|
|
1312
|
+
sub: () => (pending ? dash : `${tr('{n} 个币种', { n: costEntries.length })} · ${tr('含估算')}`),
|
|
1161
1313
|
},
|
|
1162
1314
|
hitRate: {
|
|
1163
1315
|
label: tr('缓存命中率'),
|
|
@@ -1249,16 +1401,25 @@ window.__ModuleLoader__.load({
|
|
|
1249
1401
|
// 悬停态:{ center: 柱位百分比, ratio: 柱高百分比, text: 浮层文案 };null 表示未悬停
|
|
1250
1402
|
const [hover, setHover] = useState(null)
|
|
1251
1403
|
const points = Array.isArray(series) ? series : []
|
|
1252
|
-
|
|
1404
|
+
// 成本指标:两个币种走双轴双柱;>2 仍给出口文案(三轴不可读,明确不做)
|
|
1405
|
+
const currencies = new Set()
|
|
1406
|
+
for (const point of points) for (const currency of Object.keys(point.cost ?? {})) currencies.add(currency)
|
|
1407
|
+
const currencyList = [...currencies]
|
|
1408
|
+
const dual = metric === 'cost' && currencyList.length === 2
|
|
1409
|
+
const primary = currencyList.includes('CNY') ? 'CNY' : (currencyList[0] ?? 'CNY')
|
|
1410
|
+
const secondary = dual ? currencyList.find((code) => code !== primary) : null
|
|
1411
|
+
const costBlocked = metric === 'cost' && currencyList.length > 2
|
|
1412
|
+
const costCurrency = currencyList.length === 1 ? currencyList[0] : primary
|
|
1413
|
+
const costAt = (point, code) => Number(point?.cost?.[code]?.cost) || 0
|
|
1414
|
+
const primaryValues = dual ? points.map((point) => costAt(point, primary)) : null
|
|
1415
|
+
const secondaryValues = dual ? points.map((point) => costAt(point, secondary)) : null
|
|
1416
|
+
const values = dual ? primaryValues : points.map((point) => trendValue(point, metric))
|
|
1253
1417
|
const known = values.filter((value) => value !== null && value > 0)
|
|
1254
1418
|
const max = known.length > 0 ? Math.max(...known) : 0
|
|
1419
|
+
const secondaryKnown = dual ? secondaryValues.filter((value) => value > 0) : []
|
|
1420
|
+
const secondaryMax = secondaryKnown.length > 0 ? Math.max(...secondaryKnown) : 0
|
|
1255
1421
|
const floor = metric === 'hitRate' ? hitRateFloor(values) : 0
|
|
1256
1422
|
const span = Math.max(max - floor, 1e-9)
|
|
1257
|
-
// 多币种时一根柱无法表达,按既定决策禁用成本指标并给出口文案
|
|
1258
|
-
const currencies = new Set()
|
|
1259
|
-
for (const point of points) for (const currency of Object.keys(point.cost ?? {})) currencies.add(currency)
|
|
1260
|
-
const costBlocked = metric === 'cost' && currencies.size > 1
|
|
1261
|
-
const costCurrency = currencies.size === 1 ? [...currencies][0] : 'CNY'
|
|
1262
1423
|
// 换指标 / 换范围后旧的柱位与文案都已失效,直接清空悬停态
|
|
1263
1424
|
useEffect(() => { setHover(null) }, [metric, series])
|
|
1264
1425
|
const tickOf = (dayKey) => {
|
|
@@ -1267,10 +1428,14 @@ window.__ModuleLoader__.load({
|
|
|
1267
1428
|
}
|
|
1268
1429
|
// 日期刻度按可用宽度抽稀,末点必显
|
|
1269
1430
|
const step = Math.max(1, Math.ceil(points.length / 6))
|
|
1431
|
+
const hasData = dual ? (known.length > 0 || secondaryKnown.length > 0) : known.length > 0
|
|
1270
1432
|
const emptyMessage = costBlocked
|
|
1271
1433
|
? tr('多币种下成本柱不可用,请切换其他指标')
|
|
1272
|
-
: (points.length === 0 ||
|
|
1434
|
+
: (points.length === 0 || !hasData ? tr('范围内暂无消耗') : null)
|
|
1273
1435
|
const axisTicks = emptyMessage === null ? trendAxisTicks({ floor, max, metric, currency: costCurrency }) : []
|
|
1436
|
+
const rightTicks = emptyMessage === null && dual
|
|
1437
|
+
? trendAxisTicks({ floor: 0, max: secondaryMax, metric, currency: secondary })
|
|
1438
|
+
: []
|
|
1274
1439
|
// 轴宽随最长刻度自适应:字号 11px 下中文≈11px、其余≈6.2px,再留 6px 与柱体的间距
|
|
1275
1440
|
// 轴列宽度由内容自适应(grid auto 列),这里只保证空态时左侧不塌陷
|
|
1276
1441
|
const axisWidth = axisTicks.reduce((widest, tick) => {
|
|
@@ -1279,17 +1444,28 @@ window.__ModuleLoader__.load({
|
|
|
1279
1444
|
return Math.max(widest, px)
|
|
1280
1445
|
}, 0)
|
|
1281
1446
|
const axisPad = axisTicks.length === 0 ? 34 : Math.max(28, Math.ceil(axisWidth) + 6)
|
|
1447
|
+
const rightWidth = rightTicks.reduce((widest, tick) => {
|
|
1448
|
+
let px = 0
|
|
1449
|
+
for (const char of tick.text) px += /[\u4e00-\u9fff]/.test(char) ? 11 : 6.2
|
|
1450
|
+
return Math.max(widest, px)
|
|
1451
|
+
}, 0)
|
|
1452
|
+
const rightPad = rightTicks.length === 0 ? 0 : Math.max(28, Math.ceil(rightWidth) + 6)
|
|
1282
1453
|
return h('div', { 'data-testid': 'token-dash-trend', 'data-td-trend': metric, style: styles.trendBox },
|
|
1283
1454
|
h('div', { style: styles.trendHead },
|
|
1284
1455
|
h('b', { 'data-testid': 'token-dash-trend-title', style: styles.trendTitle },
|
|
1285
1456
|
tr('使用趋势 · {metric}', { metric: tr(METRIC_LABELS[metric]) })),
|
|
1286
1457
|
h('span', { style: styles.trendHint }, tr(grain === 'month' ? '按月' : '按天')),
|
|
1287
1458
|
h('span', { style: styles.trendSpacer }),
|
|
1459
|
+
dual
|
|
1460
|
+
? h('span', { 'data-testid': 'token-dash-trend-legend', style: styles.trendLegend },
|
|
1461
|
+
h('i', { style: { ...styles.trendLegendDot, background: T.accent } }), fmtMoney(0, primary).slice(0, 1) + primary,
|
|
1462
|
+
h('i', { style: { ...styles.trendLegendDot, background: T.series2, marginLeft: 8 } }), fmtMoney(0, secondary).slice(0, 1) + secondary)
|
|
1463
|
+
: null,
|
|
1288
1464
|
h(Segmented, {
|
|
1289
1465
|
value: metric, onChange: onMetricChange, label: tr('趋势指标'), kind: 'radiogroup', fit: true,
|
|
1290
1466
|
options: TREND_METRICS.map(([value, label]) => ({ value, label: tr(label) })),
|
|
1291
1467
|
})),
|
|
1292
|
-
h('div', { style: styles.trendPlot },
|
|
1468
|
+
h('div', { style: dual ? { ...styles.trendPlot, ...styles.trendPlotDual } : styles.trendPlot },
|
|
1293
1469
|
// 左列:Y 轴(在绘图区之外,与 CC Switch 的 <YAxis> 同构)
|
|
1294
1470
|
h('div', { 'data-testid': 'token-dash-trend-axis-col', style: { ...styles.trendAxisCol, minWidth: axisPad } },
|
|
1295
1471
|
...axisTicks.map((tick) => h('span', {
|
|
@@ -1312,21 +1488,40 @@ window.__ModuleLoader__.load({
|
|
|
1312
1488
|
...points.map((point, index) => {
|
|
1313
1489
|
const value = values[index]
|
|
1314
1490
|
const ratio = value === null || value <= floor ? 0 : Math.max(0, Math.min(100, ((value - floor) / span) * 100))
|
|
1491
|
+
const secondaryRatio = dual && secondaryMax > 0
|
|
1492
|
+
? Math.max(0, Math.min(100, (secondaryValues[index] / secondaryMax) * 100))
|
|
1493
|
+
: 0
|
|
1494
|
+
const hoverText = dual
|
|
1495
|
+
? `${point.dayKey} · ${fmtMoney(value, primary)} · ${fmtMoney(secondaryValues[index], secondary)}`
|
|
1496
|
+
: `${point.dayKey} · ${fmtMetric(value, metric, costCurrency)}`
|
|
1315
1497
|
// 悬停即渲染虚线与浮层:不挂原生 title,避免系统 tooltip 的 ~1s 延迟
|
|
1316
1498
|
return h('span', {
|
|
1317
1499
|
key: point.dayKey,
|
|
1318
1500
|
'data-testid': `token-dash-trend-slot-${point.dayKey}`,
|
|
1319
|
-
style: styles.trendSlot,
|
|
1501
|
+
style: dual ? { ...styles.trendSlot, display: 'flex', alignItems: 'flex-end', gap: 1 } : styles.trendSlot,
|
|
1320
1502
|
onMouseEnter: () => setHover({
|
|
1321
1503
|
center: ((index + 0.5) / points.length) * 100,
|
|
1322
1504
|
ratio,
|
|
1323
|
-
text:
|
|
1505
|
+
text: hoverText,
|
|
1324
1506
|
}),
|
|
1325
1507
|
},
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1508
|
+
dual
|
|
1509
|
+
? [
|
|
1510
|
+
h('span', {
|
|
1511
|
+
key: primary,
|
|
1512
|
+
'data-testid': `token-dash-trend-bar-${point.dayKey}-${primary}`,
|
|
1513
|
+
style: { ...styles.trendBar, flex: '1 1 0', height: `${ratio}%`, background: ratio === 0 ? 'transparent' : T.accent },
|
|
1514
|
+
}),
|
|
1515
|
+
h('span', {
|
|
1516
|
+
key: secondary,
|
|
1517
|
+
'data-testid': `token-dash-trend-bar-${point.dayKey}-${secondary}`,
|
|
1518
|
+
style: { ...styles.trendBar, flex: '1 1 0', height: `${secondaryRatio}%`, background: secondaryRatio === 0 ? 'transparent' : T.series2 },
|
|
1519
|
+
}),
|
|
1520
|
+
]
|
|
1521
|
+
: h('span', {
|
|
1522
|
+
'data-testid': `token-dash-trend-bar-${point.dayKey}`,
|
|
1523
|
+
style: { ...styles.trendBar, height: `${ratio}%`, background: ratio === 0 ? 'transparent' : T.accent },
|
|
1524
|
+
}))
|
|
1330
1525
|
})) : h('div', { 'data-testid': 'token-dash-trend-empty', style: styles.trendMsg }, emptyMessage),
|
|
1331
1526
|
emptyMessage === null && hover !== null ? h('span', {
|
|
1332
1527
|
'data-testid': 'token-dash-trend-hover-line',
|
|
@@ -1348,7 +1543,18 @@ window.__ModuleLoader__.load({
|
|
|
1348
1543
|
emptyMessage === null && hover !== null ? h('div', {
|
|
1349
1544
|
'data-testid': 'token-dash-trend-hover-tip',
|
|
1350
1545
|
style: { ...styles.trendTip, left: `${hover.center}%` },
|
|
1351
|
-
}, hover.text) : null)
|
|
1546
|
+
}, hover.text) : null),
|
|
1547
|
+
dual
|
|
1548
|
+
? h('div', { 'data-testid': 'token-dash-trend-axis-col-right', style: { ...styles.trendAxisCol, ...styles.trendAxisColRight, minWidth: rightPad } },
|
|
1549
|
+
...rightTicks.map((tick) => h('span', {
|
|
1550
|
+
key: tick.key,
|
|
1551
|
+
'data-testid': `token-dash-trend-axis-right-${tick.key}`,
|
|
1552
|
+
'aria-hidden': true,
|
|
1553
|
+
style: tick.top === null
|
|
1554
|
+
? { ...styles.trendAxisRight, bottom: 18 }
|
|
1555
|
+
: { ...styles.trendAxisRight, top: tick.top, transform: tick.top === '50%' ? 'translateY(-50%)' : undefined },
|
|
1556
|
+
}, tick.text)))
|
|
1557
|
+
: null))
|
|
1352
1558
|
}
|
|
1353
1559
|
|
|
1354
1560
|
function MiniBar({ ratio = 0, neutral = false, stretch = false }) {
|
|
@@ -1371,7 +1577,8 @@ window.__ModuleLoader__.load({
|
|
|
1371
1577
|
|
|
1372
1578
|
/** P3 API 卡底部说明:状态 · 比例,再补余额周期 / 限流 / 渠道用量(渠道值只读)。 */
|
|
1373
1579
|
function apiCardCaption(provider, { hasBudget, percent, budget }) {
|
|
1374
|
-
|
|
1580
|
+
// 状态与比例已由环和百分比表达,说明行只留环上放不下的信息(D-07)
|
|
1581
|
+
const parts = []
|
|
1375
1582
|
const periodEnd = Number(provider.balance?.periodEnd)
|
|
1376
1583
|
const period = Number.isFinite(periodEnd) && periodEnd > 0 ? resetParts(periodEnd).absolute : null
|
|
1377
1584
|
if (period !== null) parts.push(tr('周期至 {time}', { time: period }))
|
|
@@ -1389,22 +1596,22 @@ window.__ModuleLoader__.load({
|
|
|
1389
1596
|
return windows.slice(0, 3).map((plan) => `${windowLabel(plan.key)} ${fmtPercent(plan.usedPercent)}`).join(' · ')
|
|
1390
1597
|
}
|
|
1391
1598
|
|
|
1392
|
-
/** P1
|
|
1599
|
+
/** P1 订阅窗口:复用 P0 的 SubWindow(横排、不显示时间),最多三窗。 */
|
|
1393
1600
|
function subscriptionNodes(provider) {
|
|
1394
1601
|
const windows = Array.isArray(provider.windows) ? provider.windows : []
|
|
1395
1602
|
if (windows.length === 0) return [h('span', { key: 'fail', style: styles.muted }, tr('套餐查询失败'))]
|
|
1396
1603
|
const nodes = []
|
|
1397
1604
|
for (const [index, plan] of windows.slice(0, 3).entries()) {
|
|
1398
|
-
|
|
1399
|
-
|
|
1605
|
+
// 分隔符用 U+30FB(片假名中点)且**不带两侧空格**(Issue #1):
|
|
1606
|
+
// 旧写法 ' ・ ' 每侧多出一个半角空格(约 3px 盒宽),会把单侧视觉留白从 7px 撑到 12px,
|
|
1607
|
+
// 三个窗口因此散成三块,看不出同属一条套餐。
|
|
1608
|
+
if (index > 0) nodes.push(h('span', { key: `sep-${plan.key}`, style: { color: T.weak } }, '・'))
|
|
1400
1609
|
nodes.push(h('span', { key: plan.key, style: styles.inlineWrap },
|
|
1401
|
-
h(
|
|
1402
|
-
h(InlineRing, {
|
|
1403
|
-
percent, state: stateOf(percent, SUB_CRITICAL), size: 14, stroke: 2,
|
|
1404
|
-
testid: `token-dash-pill-ring-${plan.key}`,
|
|
1405
|
-
})))
|
|
1610
|
+
h(SubWindow, { plan, showTime: false, ringSize: 14, ringStroke: 2, labelFontSize: 12, ringTestId: `token-dash-pill-ring-${plan.key}` })))
|
|
1406
1611
|
}
|
|
1407
|
-
|
|
1612
|
+
// 包进 gap:4 的容器:窗口组之间的间距由它决定,pill 的 gap:6 只发生一次
|
|
1613
|
+
// (否则每个窗口与分隔符都各吃一份 6px,API 段又会跟着被改成 4)。
|
|
1614
|
+
return [h('span', { key: 'subgroup', style: styles.pillGroup }, nodes)]
|
|
1408
1615
|
}
|
|
1409
1616
|
|
|
1410
1617
|
function LocaleRoot(props) {
|
|
@@ -1420,27 +1627,31 @@ window.__ModuleLoader__.load({
|
|
|
1420
1627
|
? `/summary?active=${encodeURIComponent(active)}`
|
|
1421
1628
|
: '/summary')
|
|
1422
1629
|
|
|
1423
|
-
/**
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1630
|
+
/**
|
|
1631
|
+
* 从投影取「用户当前选中」的 provider。
|
|
1632
|
+
* 宿主 modelSelection 投影对客户端输出的是 { lastUsed, next },其中 next = pending ?? lastUsed
|
|
1633
|
+
* (pending 是「已切换、尚未发出请求」的最新选择),所以必须读 next。
|
|
1634
|
+
* 必须作为 hook 在组件顶层无条件调用:放进 try/条件会破坏 React hook 顺序,更新会被丢弃。
|
|
1635
|
+
*/
|
|
1636
|
+
function useActiveProvider(props) {
|
|
1637
|
+
const projection = typeof props?.useProjection === 'function' ? props.useProjection('modelSelection') : null
|
|
1638
|
+
const picked = projection?.next ?? projection?.lastUsed
|
|
1639
|
+
const raw = typeof picked === 'string' ? picked : (picked?.provider ?? picked?.providerId ?? null)
|
|
1640
|
+
if (typeof raw === 'string' && raw !== '') return raw
|
|
1641
|
+
// 兜底:宿主默认模型(全局配置,非会话选择,仅保证有值可渲染)
|
|
1431
1642
|
try {
|
|
1432
1643
|
const service = typeof ctxRef?.get === 'function' ? ctxRef.get('agentDefaultModel') : null
|
|
1433
|
-
const
|
|
1434
|
-
const
|
|
1435
|
-
if (typeof
|
|
1436
|
-
} catch { /*
|
|
1644
|
+
const fallback = service?.currentSelection?.()
|
|
1645
|
+
const value = typeof fallback === 'string' ? fallback : (fallback?.provider ?? fallback?.providerId ?? null)
|
|
1646
|
+
if (typeof value === 'string' && value !== '') return value
|
|
1647
|
+
} catch { /* ignore */ }
|
|
1437
1648
|
return null
|
|
1438
1649
|
}
|
|
1439
1650
|
|
|
1440
1651
|
function Pill(props) {
|
|
1441
1652
|
const updateState = useUpdateSnapshot()
|
|
1442
1653
|
const hasUpdate = updateState.phase === 'available' || updateState.phase === 'confirm'
|
|
1443
|
-
const active =
|
|
1654
|
+
const active = useActiveProvider(props)
|
|
1444
1655
|
const [summary, setSummary] = useState(null)
|
|
1445
1656
|
const [open, setOpen] = useState(false)
|
|
1446
1657
|
const [error, setError] = useState(false)
|
|
@@ -1458,33 +1669,49 @@ window.__ModuleLoader__.load({
|
|
|
1458
1669
|
try { if (window.localStorage.getItem('token-dash-hint-seen') !== '1') setHint(true) } catch { /* ignore */ }
|
|
1459
1670
|
}, [])
|
|
1460
1671
|
|
|
1672
|
+
// active 放进 ref:轮询定时器只建立一次,回调里读 ref 才能始终拿到最新选中项。
|
|
1673
|
+
// 若把 active 放进 load 的依赖,load 会随切换重建、usePolling 却只依赖 refreshMs 不会重排,
|
|
1674
|
+
// 结果是定时器一直跑旧闭包 —— 这正是此前「切换后不发请求」的成因。
|
|
1675
|
+
const activeRef = useRef(active)
|
|
1676
|
+
activeRef.current = active
|
|
1677
|
+
const refreshMsRef = useRef(refreshMs)
|
|
1678
|
+
refreshMsRef.current = refreshMs
|
|
1679
|
+
|
|
1461
1680
|
const load = useCallback(async () => {
|
|
1462
1681
|
lastLoadAt.current = Date.now()
|
|
1463
1682
|
setRefreshing(true)
|
|
1464
1683
|
try {
|
|
1465
|
-
const data = await getJson(summaryPath(
|
|
1684
|
+
const data = await getJson(summaryPath(activeRef.current))
|
|
1466
1685
|
setSummary(data)
|
|
1467
1686
|
setError(false)
|
|
1468
|
-
if (Number.isFinite(data?.refreshMs) && data.refreshMs !==
|
|
1687
|
+
if (Number.isFinite(data?.refreshMs) && data.refreshMs !== refreshMsRef.current) setRefreshMs(data.refreshMs)
|
|
1469
1688
|
} catch {
|
|
1470
1689
|
setError(true)
|
|
1471
1690
|
} finally {
|
|
1472
1691
|
setRefreshing(false)
|
|
1473
1692
|
}
|
|
1474
|
-
}, [
|
|
1693
|
+
}, [])
|
|
1475
1694
|
|
|
1476
1695
|
useEffect(() => {
|
|
1477
1696
|
getJson('/prefs').then((prefs) => setRefreshMs(prefs.refreshMs ?? 60000)).catch(() => {})
|
|
1478
1697
|
}, [])
|
|
1479
1698
|
|
|
1480
1699
|
useEffect(() => { load() }, [load])
|
|
1481
|
-
//
|
|
1700
|
+
// active 变化时立刻取数(不依赖轮询或投影推送的时序)。
|
|
1701
|
+
// load 本身不再随 active 重建,这里显式监听 active:切换供应商后立即发一次请求。
|
|
1702
|
+
useEffect(() => {
|
|
1703
|
+
if (active === null || active === undefined) return
|
|
1704
|
+
if (Date.now() - lastLoadAt.current < 1500) return
|
|
1705
|
+
void load()
|
|
1706
|
+
}, [active, load])
|
|
1707
|
+
// 展开即刷新:距上次拉取超过 2s 才发,避免开合抖动与 refreshMs 变化引起的重跑
|
|
1482
1708
|
useEffect(() => {
|
|
1483
1709
|
if (!open) return
|
|
1484
1710
|
if (Date.now() - lastLoadAt.current < 2000) return
|
|
1485
1711
|
void load()
|
|
1486
1712
|
}, [open, load])
|
|
1487
|
-
|
|
1713
|
+
// 定时轮询兜底:不依赖投影变化,只要可见就按周期取数(与 dsh-quota 同构)。
|
|
1714
|
+
usePolling(load, { refreshMs: Math.min(open ? 10_000 : 30_000, refreshMs) })
|
|
1488
1715
|
|
|
1489
1716
|
const refreshSubs = useCallback(async () => {
|
|
1490
1717
|
try { await fetch(`${API}/providers/refresh`, { method: 'POST', headers: { accept: 'application/json' } }) } catch { /* fall through */ }
|
|
@@ -1536,7 +1763,13 @@ window.__ModuleLoader__.load({
|
|
|
1536
1763
|
}
|
|
1537
1764
|
|
|
1538
1765
|
const providers = summary?.providers ?? []
|
|
1539
|
-
|
|
1766
|
+
// 选卡三级:服务端归一化的 activeProviderId → 本地 active 直比 → 记账口径 → 首卡。
|
|
1767
|
+
// 宿主的 active 是路由名(deepseek-account),与 providers[].id(deepseek-official)不同字,
|
|
1768
|
+
// 因此优先用服务端回显的归一化值;本地直比只作兜底,不让 UI 承担别名知识。
|
|
1769
|
+
const current = providers.find((provider) => provider.id === summary?.activeProviderId)
|
|
1770
|
+
?? providers.find((provider) => provider.id === active)
|
|
1771
|
+
?? providers.find((provider) => provider.id === summary?.currentProviderId)
|
|
1772
|
+
?? providers[0]
|
|
1540
1773
|
let prefix = '[API]'
|
|
1541
1774
|
let bodyNodes = [h('span', { key: 'unknown' }, '--')]
|
|
1542
1775
|
let ariaText = tr('API 用量')
|
|
@@ -1551,19 +1784,19 @@ window.__ModuleLoader__.load({
|
|
|
1551
1784
|
const balance = balanceText(current, summary?.showBalance)
|
|
1552
1785
|
bodyNodes = [
|
|
1553
1786
|
h('span', { key: 'budget', style: styles.inlineWrap },
|
|
1554
|
-
h('span', { 'data-td-label': true, style: styles.
|
|
1787
|
+
h('span', { 'data-td-label': true, style: styles.pillLabel }, tr('预算')),
|
|
1555
1788
|
h(InlineRing, {
|
|
1556
1789
|
percent: hasBudget ? percent : null,
|
|
1557
1790
|
state: hasBudget ? stateOf(percent, API_CRITICAL) : 'empty',
|
|
1558
|
-
size: 14, stroke: 2,
|
|
1791
|
+
size: 14, stroke: 2, fontSize: 12, ringShift: 0.75,
|
|
1559
1792
|
text: hasBudget ? fmtRatio(budget.percent) : tr('未设置'),
|
|
1560
1793
|
testid: 'token-dash-pill-ring-budget',
|
|
1561
1794
|
})),
|
|
1562
1795
|
h('span', { key: 'today', style: styles.inlineWrap },
|
|
1563
|
-
h('span', { 'data-td-label': true, style: styles.
|
|
1796
|
+
h('span', { 'data-td-label': true, style: styles.pillLabel }, tr('今日')),
|
|
1564
1797
|
h('span', { style: styles.num }, fmtMoney(current.usage?.todayCost, current.currency))),
|
|
1565
1798
|
balance === null ? null : h('span', { key: 'balance', style: styles.inlineWrap },
|
|
1566
|
-
h('span', { 'data-td-label': true, style: styles.
|
|
1799
|
+
h('span', { 'data-td-label': true, style: styles.pillLabel }, tr('余额')),
|
|
1567
1800
|
h('span', { style: styles.num }, balance)),
|
|
1568
1801
|
].filter(Boolean)
|
|
1569
1802
|
ariaText = tr('{name},API 用量,{budget},今日 {today}{balance}', { name: current.name, budget: hasBudget ? tr('预算已用 {value}', { value: fmtRatio(budget.percent) }) : tr('预算未设置'), today: fmtMoney(current.usage?.todayCost, current.currency), balance: balance === null ? '' : `,${tr('余额 {value}', { value: balance })}` })
|
|
@@ -1590,21 +1823,30 @@ window.__ModuleLoader__.load({
|
|
|
1590
1823
|
}) : null)
|
|
1591
1824
|
}
|
|
1592
1825
|
|
|
1593
|
-
|
|
1826
|
+
/**
|
|
1827
|
+
* P2 标题栏:仅刷新与关闭。
|
|
1828
|
+
* 原有一个 ⓘ 提示「支持哪些 API Key」,文案停留在 5 家 provider 的旧表述,
|
|
1829
|
+
* 且对已配置完成的用户无操作价值,故移除(Issue #3)。
|
|
1830
|
+
* headerActions 不再需要 marginInlineEnd 负偏移——那是为抵消 ⓘ 的视觉留白。
|
|
1831
|
+
*/
|
|
1832
|
+
function PanelHeader({ onRefresh, refreshing, onClose, stale = false, failed = false }) {
|
|
1594
1833
|
return h('div', { style: styles.header },
|
|
1595
1834
|
h('b', { style: styles.title }, tr('当前模型')),
|
|
1596
|
-
h('div', { style:
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1835
|
+
h('div', { style: styles.headerActions },
|
|
1836
|
+
// 任一接口失败时,刷新位换成带文字的「重试」:全屏只保留这一个入口,
|
|
1837
|
+
// 表体内不再插入错误条按钮(Issue #4)
|
|
1838
|
+
failed
|
|
1839
|
+
? h('button', {
|
|
1840
|
+
type: 'button', 'data-testid': 'token-dash-retry',
|
|
1841
|
+
style: styles.retryChip, disabled: refreshing, onClick: onRefresh,
|
|
1842
|
+
}, h(Icon, { name: refreshing ? 'loader' : 'refresh', size: 12 }), tr('重试'))
|
|
1843
|
+
: h(IconButton, {
|
|
1844
|
+
label: tr('刷新'), testid: 'token-dash-refresh', disabled: refreshing, onClick: onRefresh, dot: stale,
|
|
1845
|
+
}, h(Icon, { name: refreshing ? 'loader' : 'refresh', size: 13 })),
|
|
1604
1846
|
h(IconButton, { label: tr('关闭'), testid: 'token-dash-close', onClick: onClose }, h(Icon, { name: 'close', size: 13 }))))
|
|
1605
1847
|
}
|
|
1606
1848
|
|
|
1607
|
-
function Panel({ summary, error, refreshing, maxHeight, left = 0, panelRef, onRefresh, onClose }) {
|
|
1849
|
+
function Panel({ summary, error, refreshing, maxHeight, left = 0, active, panelRef, onRefresh, onClose }) {
|
|
1608
1850
|
const [range, setRange] = useState('today')
|
|
1609
1851
|
const [usage, setUsage] = useState(null)
|
|
1610
1852
|
const [usageLoading, setUsageLoading] = useState(false)
|
|
@@ -1623,12 +1865,16 @@ window.__ModuleLoader__.load({
|
|
|
1623
1865
|
} catch {
|
|
1624
1866
|
if (seq !== usageSeq.current) return
|
|
1625
1867
|
setUsageError(true)
|
|
1868
|
+
// 失败即清空:保留旧数据会让用户把陈旧数字当成当前值(Issue #4)
|
|
1869
|
+
setUsage(null)
|
|
1626
1870
|
} finally {
|
|
1627
1871
|
if (seq === usageSeq.current) setUsageLoading(false)
|
|
1628
1872
|
}
|
|
1629
1873
|
}, [range])
|
|
1630
1874
|
|
|
1631
1875
|
useEffect(() => { loadUsage() }, [loadUsage])
|
|
1876
|
+
// P2 展开期 usage 随轮询自动更新(对齐 P3 看板口径),聊天中不再依赖手动点击
|
|
1877
|
+
usePolling(loadUsage, { refreshMs: Math.min(10_000, Number(summary?.refreshMs) || 60_000) })
|
|
1632
1878
|
useEffect(() => { setPanelError(error === true) }, [error])
|
|
1633
1879
|
|
|
1634
1880
|
useEffect(() => {
|
|
@@ -1655,7 +1901,11 @@ window.__ModuleLoader__.load({
|
|
|
1655
1901
|
}, [onRefresh, loadUsage])
|
|
1656
1902
|
|
|
1657
1903
|
const providers = summary?.providers ?? []
|
|
1658
|
-
|
|
1904
|
+
// 与 Pill 同口径:归一化 active 优先,本地直比与记账口径兜底
|
|
1905
|
+
const current = providers.find((provider) => provider.id === summary?.activeProviderId)
|
|
1906
|
+
?? providers.find((provider) => provider.id === active)
|
|
1907
|
+
?? providers.find((provider) => provider.id === summary?.currentProviderId)
|
|
1908
|
+
?? providers[0]
|
|
1659
1909
|
const models = Array.isArray(usage?.models) ? usage.models : []
|
|
1660
1910
|
const scoped = models.filter((model) => model.providerId === current?.id)
|
|
1661
1911
|
const rows = scoped.length > 0 ? scoped : models
|
|
@@ -1665,12 +1915,6 @@ window.__ModuleLoader__.load({
|
|
|
1665
1915
|
// 布局服务可用就显示入口;注册时序可能导致 hasMainPanel 暂时为 false,点击时再判。
|
|
1666
1916
|
const canOpen = layoutRef !== null
|
|
1667
1917
|
|
|
1668
|
-
const strip = error || usageError
|
|
1669
|
-
? h('div', { style: styles.errorStrip, role: 'alert' },
|
|
1670
|
-
h('span', null, error ? tr('刷新失败,保留上次数据') : tr('范围数据加载失败')),
|
|
1671
|
-
h('button', { type: 'button', style: styles.button, onClick: refreshAll }, tr('重试')))
|
|
1672
|
-
: null
|
|
1673
|
-
|
|
1674
1918
|
let body
|
|
1675
1919
|
if (summary === null) {
|
|
1676
1920
|
body = h('div', { style: styles.empty }, panelError ? tr('Token 数据不可用') : tr('Token 数据加载中…'))
|
|
@@ -1688,7 +1932,6 @@ window.__ModuleLoader__.load({
|
|
|
1688
1932
|
value: range, onChange: setRange, label: tr('时间范围'),
|
|
1689
1933
|
options: RANGES.map(([value, label]) => ({ value, label: tr(label) })),
|
|
1690
1934
|
})),
|
|
1691
|
-
strip,
|
|
1692
1935
|
usage === null && usageLoading
|
|
1693
1936
|
? h('div', { style: styles.empty }, tr('范围数据加载中…'))
|
|
1694
1937
|
: h(TokenTable, { rows }),
|
|
@@ -1708,7 +1951,10 @@ window.__ModuleLoader__.load({
|
|
|
1708
1951
|
'data-testid': 'token-dash-panel',
|
|
1709
1952
|
style: { ...styles.panel, maxHeight, left },
|
|
1710
1953
|
},
|
|
1711
|
-
h(PanelHeader, {
|
|
1954
|
+
h(PanelHeader, {
|
|
1955
|
+
onRefresh: refreshAll, refreshing: refreshing || usageLoading, onClose, stale,
|
|
1956
|
+
failed: error || usageError,
|
|
1957
|
+
}),
|
|
1712
1958
|
body)
|
|
1713
1959
|
}
|
|
1714
1960
|
|
|
@@ -1722,6 +1968,16 @@ window.__ModuleLoader__.load({
|
|
|
1722
1968
|
}, suffix === '' ? text : `${text} ${suffix}`)
|
|
1723
1969
|
}
|
|
1724
1970
|
|
|
1971
|
+
/** 降级角标:探测失败但仍有旧快照时提示「数据待更新」。 */
|
|
1972
|
+
function StaleBadge({ provider }) {
|
|
1973
|
+
if (provider?.stale !== true) return null
|
|
1974
|
+
return h('span', {
|
|
1975
|
+
'data-testid': 'token-dash-stale-badge',
|
|
1976
|
+
style: styles.staleBadge,
|
|
1977
|
+
title: tr('探测失败,暂用上次数据'),
|
|
1978
|
+
}, tr('数据待更新'))
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1725
1981
|
/** P2 API 主区:环+% / 已用·预算 / 真实余额;第四格留空。 */
|
|
1726
1982
|
function P2Summary({ provider, summary }) {
|
|
1727
1983
|
const budget = provider.budget
|
|
@@ -1731,16 +1987,18 @@ window.__ModuleLoader__.load({
|
|
|
1731
1987
|
return h(React.Fragment, null,
|
|
1732
1988
|
h('div', { style: styles.providerLine },
|
|
1733
1989
|
h('span', { style: styles.title, title: provider.name }, provider.name),
|
|
1734
|
-
h('span', { style:
|
|
1990
|
+
h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 6, minWidth: 0 } },
|
|
1991
|
+
h(StaleBadge, { provider }),
|
|
1992
|
+
h('span', { style: styles.providerChip }, `[ ${tr('API 用量')} ]`))),
|
|
1735
1993
|
h('div', { 'data-testid': 'token-dash-summary-row', style: styles.summaryRow },
|
|
1736
|
-
h('span', { style: styles.summaryCell },
|
|
1994
|
+
h('span', { style: { ...styles.summaryCell, alignItems: 'flex-start' } },
|
|
1737
1995
|
h(InlineRing, {
|
|
1738
1996
|
percent: hasBudget ? percent : null,
|
|
1739
1997
|
state: hasBudget ? stateOf(percent, API_CRITICAL) : 'empty',
|
|
1740
1998
|
size: 20, stroke: 2.5,
|
|
1741
1999
|
testid: 'token-dash-summary-ring',
|
|
1742
2000
|
})),
|
|
1743
|
-
h('span', { 'data-testid': 'token-dash-summary-used', style: styles.summaryCell },
|
|
2001
|
+
h('span', { 'data-testid': 'token-dash-summary-used', style: { ...styles.summaryCell, alignItems: 'center' } },
|
|
1744
2002
|
h('span', { style: styles.summaryLabel }, tr('已用 / 预算')),
|
|
1745
2003
|
h('span', { style: styles.summaryValue },
|
|
1746
2004
|
hasBudget ? `${fmtMoney(budget.used, provider.currency)} / ${fmtMoney(budget.amount, provider.currency)}` : '—')),
|
|
@@ -1748,8 +2006,7 @@ window.__ModuleLoader__.load({
|
|
|
1748
2006
|
'data-testid': 'token-dash-summary-balance',
|
|
1749
2007
|
style: {
|
|
1750
2008
|
...styles.summaryCell,
|
|
1751
|
-
|
|
1752
|
-
justifySelf: 'end',
|
|
2009
|
+
// 两行共用同一条右缘,与标题行 [ API 用量 ] 对齐(Issue #2)
|
|
1753
2010
|
alignItems: 'flex-end',
|
|
1754
2011
|
textAlign: 'right',
|
|
1755
2012
|
},
|
|
@@ -1759,47 +2016,91 @@ window.__ModuleLoader__.load({
|
|
|
1759
2016
|
h('span', { style: styles.summaryValue }, balance ?? '—'))))
|
|
1760
2017
|
}
|
|
1761
2018
|
|
|
1762
|
-
/**
|
|
1763
|
-
|
|
2019
|
+
/**
|
|
2020
|
+
* P0 公共控件:单个订阅窗口。
|
|
2021
|
+
*
|
|
2022
|
+
* 结构:第一行「标签 + 环 + 数字」作为一个整体横向居中;第二行时间可选,与第一行同心。
|
|
2023
|
+
* 为什么封装:P1 与 P2 曾各写一套,P2 用 position:absolute 把时间钉在圆环中心,
|
|
2024
|
+
* 偏移量随标签字数漂移(标签越长越偏)。这里改用正常流 + align-items:center,
|
|
2025
|
+
* 时间天然与「环 + 数字」这段整体居中对齐,与标签宽度、百分比宽度均无关。
|
|
2026
|
+
*
|
|
2027
|
+
* @param {object} props.plan 订阅窗口(key / usedPercent / resetAt)
|
|
2028
|
+
* @param {boolean} props.showTime 是否显示倒计时(P1 横排不显示,P2 显示)
|
|
2029
|
+
* @param {'start'|'center'|'end'} props.place 控件在所在列内的摆放位置
|
|
2030
|
+
* @param {string} props.testKey 测试 id 后缀
|
|
2031
|
+
* @param {number} props.labelFontSize 周期标签字号;P1 传 12 让标签与 pill 其余文字同字号,
|
|
2032
|
+
* 此时不再使用「锁高 + 上移 0.5px」那套补偿(它只在 P2 的多字号场景下有意义)。不传则沿用旧行为。
|
|
2033
|
+
*/
|
|
2034
|
+
function SubWindow({ plan, showTime = true, place = 'center', testKey, ringTestId, ringSize = 20, ringStroke = 2.5, labelFontSize }) {
|
|
1764
2035
|
const percent = plan === undefined || plan === null ? null : Number(plan.usedPercent)
|
|
1765
2036
|
const hasValue = Number.isFinite(percent)
|
|
1766
2037
|
const parts = hasValue ? resetParts(plan.resetAt) : { relative: null, absolute: null }
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
'data-testid': `token-dash-sub-window-${testKey}`,
|
|
1771
|
-
title: parts.absolute ?? undefined,
|
|
2038
|
+
return h('span', {
|
|
2039
|
+
'data-testid': testKey === undefined ? undefined : `token-dash-sub-window-${testKey}`,
|
|
2040
|
+
title: showTime ? (parts.absolute ?? undefined) : undefined,
|
|
1772
2041
|
style: {
|
|
1773
|
-
display: 'flex', flexDirection: 'column',
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
justifySelf: align === 'right' ? 'end' : align === 'center' ? 'center' : 'start',
|
|
2042
|
+
display: 'inline-flex', flexDirection: 'column', alignItems: 'center', gap: 3,
|
|
2043
|
+
minWidth: 0, whiteSpace: 'nowrap',
|
|
2044
|
+
justifySelf: place === 'end' ? 'end' : place === 'start' ? 'start' : 'center',
|
|
1777
2045
|
},
|
|
1778
2046
|
},
|
|
1779
|
-
|
|
1780
|
-
|
|
2047
|
+
// 第一行:标签 + 环 + 数字,作为整体居中
|
|
2048
|
+
h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 4 } },
|
|
2049
|
+
// 标签对齐(Issue #8,方案 B + D):
|
|
2050
|
+
// B — 行盒高度锁成圆环直径(按 ringSize 取值,P1 14 / P2 20),
|
|
2051
|
+
// 避免行高把标签盒子撑得比环高,align-items 才对得准;
|
|
2052
|
+
// D — 再整体上移 0.5px,补偿汉字字形(周 / 月)相对拉丁(5h)的视觉重心。
|
|
2053
|
+
// 两者不冲突:B 定盒子、D 定位置。不可写死 20px,否则 P1 pill 行高会被撑大。
|
|
2054
|
+
//
|
|
2055
|
+
// P1 例外:传入 labelFontSize 时标签与百分比同为 12px、共用 16px 行高,
|
|
2056
|
+
// 全行单一字号已能自然共基线,再叠加 height/top 补偿反而会造成 1px 偏差,故一并关闭。
|
|
2057
|
+
h('span', {
|
|
2058
|
+
style: labelFontSize === undefined
|
|
2059
|
+
? {
|
|
2060
|
+
...styles.summaryLabel,
|
|
2061
|
+
display: 'inline-flex', alignItems: 'center', lineHeight: 1,
|
|
2062
|
+
height: ringSize, position: 'relative', top: -0.5,
|
|
2063
|
+
}
|
|
2064
|
+
: { color: T.muted, fontSize: labelFontSize, lineHeight: '16px' },
|
|
2065
|
+
}, plan?.key ? windowLabel(plan.key) : '—'),
|
|
1781
2066
|
h(InlineRing, {
|
|
1782
|
-
percent, state: hasValue ? stateOf(percent, SUB_CRITICAL) : 'empty', size:
|
|
2067
|
+
percent, state: hasValue ? stateOf(percent, SUB_CRITICAL) : 'empty', size: ringSize, stroke: ringStroke,
|
|
2068
|
+
// P1:百分比与标签同为 12px / 16px 行盒,且不带 2px 补偿,三者共基线
|
|
2069
|
+
fontSize: labelFontSize === undefined ? undefined : 12,
|
|
2070
|
+
percentLineHeight: labelFontSize === undefined ? undefined : 16,
|
|
2071
|
+
ringShift: labelFontSize === undefined ? undefined : 0.75,
|
|
2072
|
+
testid: ringTestId ?? (testKey === undefined ? undefined : `token-dash-sub-ring-${testKey}`),
|
|
1783
2073
|
})),
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
2074
|
+
// 第二行:时间,与第一行同心(父级 align-items:center 保证)
|
|
2075
|
+
showTime
|
|
2076
|
+
? h('span', {
|
|
2077
|
+
'data-testid': testKey === undefined ? undefined : `token-dash-sub-rel-${testKey}`,
|
|
2078
|
+
style: { fontSize: 11, color: T.muted, lineHeight: '13px' },
|
|
2079
|
+
}, parts.relative ?? '—')
|
|
2080
|
+
: null)
|
|
1791
2081
|
}
|
|
1792
2082
|
|
|
2083
|
+
/** P2 订阅窗口行:复用 P0 的 SubWindow,按 align 决定列内摆放。 */
|
|
2084
|
+
function WindowRow({ providerId, plan, align = 'left' }) {
|
|
2085
|
+
const testKey = `${providerId}-${plan?.key ?? 'none'}`
|
|
2086
|
+
const place = align === 'right' ? 'end' : align === 'center' ? 'center' : 'start'
|
|
2087
|
+
// 传 labelFontSize 走单字号分支(Issue #6):标签与百分比同 12px / 16px 行盒,
|
|
2088
|
+
// 去掉 height:环径 + top:-0.5 的旧补偿,环下移 0.75px 贴合文字墨迹。
|
|
2089
|
+
return h(SubWindow, { plan, showTime: true, place, testKey, labelFontSize: 12 })
|
|
2090
|
+
}
|
|
1793
2091
|
function P2Windows({ provider }) {
|
|
1794
2092
|
const windows = Array.isArray(provider.windows) ? provider.windows : []
|
|
1795
2093
|
return h(React.Fragment, null,
|
|
1796
2094
|
h('div', { style: styles.providerLine },
|
|
1797
2095
|
h('span', { style: styles.title, title: provider.name }, provider.name),
|
|
1798
|
-
h('span', { style:
|
|
2096
|
+
h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: 6, minWidth: 0 } },
|
|
2097
|
+
h(StaleBadge, { provider }),
|
|
2098
|
+
h('span', { style: styles.providerChip }, `[ ${tr('订阅')} ]`))),
|
|
1799
2099
|
windows.length === 0
|
|
1800
2100
|
? h('div', { style: styles.empty }, tr('套餐查询失败'))
|
|
2101
|
+
// 倒计时是绝对定位(锚在圆环下方),故用 grid 分列 + 底部内边距容纳第二行
|
|
1801
2102
|
: h('div', {
|
|
1802
|
-
style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 12 },
|
|
2103
|
+
style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(0, 1fr))', gap: 12, paddingBottom: 15 },
|
|
1803
2104
|
}, windows.map((plan, index) => h(WindowRow, {
|
|
1804
2105
|
key: plan.key,
|
|
1805
2106
|
providerId: provider.id,
|
|
@@ -2112,7 +2413,9 @@ window.__ModuleLoader__.load({
|
|
|
2112
2413
|
style: { fontSize: 10, color: T.weak },
|
|
2113
2414
|
}, tr('按社区表 CNY'))
|
|
2114
2415
|
: null)),
|
|
2115
|
-
|
|
2416
|
+
apiCardCaption(provider, { hasBudget, percent, budget }) === ''
|
|
2417
|
+
? null
|
|
2418
|
+
: h('div', { style: styles.cardCaption }, apiCardCaption(provider, { hasBudget, percent, budget })))
|
|
2116
2419
|
}))
|
|
2117
2420
|
}
|
|
2118
2421
|
|
|
@@ -2120,17 +2423,15 @@ window.__ModuleLoader__.load({
|
|
|
2120
2423
|
const percent = plan === undefined ? null : Number(plan.usedPercent)
|
|
2121
2424
|
const hasValue = Number.isFinite(percent)
|
|
2122
2425
|
const parts = hasValue ? resetParts(plan.resetAt) : { relative: null, absolute: null }
|
|
2123
|
-
const left = hasValue ? Math.max(0, Math.round(100 - percent)) : null
|
|
2124
2426
|
const testKey = `${providerId}-${windowKey}`
|
|
2427
|
+
// 不传 remaining:「剩余 x%」与环上已用百分比互为补数,可自行推算(与 P2 同口径)
|
|
2125
2428
|
return h(QuotaGauge, {
|
|
2126
2429
|
testid: `token-dash-quota-cell-${testKey}`,
|
|
2127
2430
|
label: windowLabel(windowKey),
|
|
2128
2431
|
percent,
|
|
2129
2432
|
state: hasValue ? stateOf(percent, SUB_CRITICAL) : 'empty',
|
|
2130
|
-
remaining: left === null ? '—' : tr('剩余 {value}', { value: `${left}%` }),
|
|
2131
2433
|
relative: parts.relative ?? '—',
|
|
2132
2434
|
absolute: parts.absolute ?? '—',
|
|
2133
|
-
remainingTestid: `token-dash-quota-left-${testKey}`,
|
|
2134
2435
|
relativeTestid: `token-dash-quota-rel-${testKey}`,
|
|
2135
2436
|
absoluteTestid: `token-dash-quota-abs-${testKey}`,
|
|
2136
2437
|
})
|
|
@@ -2154,7 +2455,10 @@ window.__ModuleLoader__.load({
|
|
|
2154
2455
|
}, windows.map((plan) => h(QuotaCell, {
|
|
2155
2456
|
key: plan.key, providerId: provider.id, plan, windowKey: plan.key,
|
|
2156
2457
|
}))),
|
|
2157
|
-
|
|
2458
|
+
Number(provider.usage?.monthEstimate) > 0
|
|
2459
|
+
? h('div', { style: styles.cardCaption },
|
|
2460
|
+
`${tr('本月估算')} ${fmtMoney(provider.usage.monthEstimate, provider.currency)}`)
|
|
2461
|
+
: null)
|
|
2158
2462
|
}))
|
|
2159
2463
|
}
|
|
2160
2464
|
|
|
@@ -2197,7 +2501,7 @@ window.__ModuleLoader__.load({
|
|
|
2197
2501
|
|
|
2198
2502
|
/** P3 整页:自取数 + 两表 + 两格卡与明细 + 底部设置胶囊。 */
|
|
2199
2503
|
function BoardPage(props) {
|
|
2200
|
-
const active =
|
|
2504
|
+
const active = useActiveProvider(props)
|
|
2201
2505
|
const [summary, setSummary] = useState(null)
|
|
2202
2506
|
const [usage, setUsage] = useState(null)
|
|
2203
2507
|
const [prefs, setPrefs] = useState(null)
|
|
@@ -2216,6 +2520,7 @@ window.__ModuleLoader__.load({
|
|
|
2216
2520
|
setError(false)
|
|
2217
2521
|
} catch {
|
|
2218
2522
|
setError(true)
|
|
2523
|
+
reportClientLog('board', 'fetch.error', { path: 'summary' })
|
|
2219
2524
|
}
|
|
2220
2525
|
}, [active])
|
|
2221
2526
|
|
|
@@ -2230,6 +2535,7 @@ window.__ModuleLoader__.load({
|
|
|
2230
2535
|
setUsageError(false)
|
|
2231
2536
|
} catch {
|
|
2232
2537
|
setUsageError(true)
|
|
2538
|
+
reportClientLog('board', 'fetch.error', { path: 'usage' })
|
|
2233
2539
|
}
|
|
2234
2540
|
}, [range])
|
|
2235
2541
|
|
|
@@ -2271,9 +2577,20 @@ window.__ModuleLoader__.load({
|
|
|
2271
2577
|
const models = Array.isArray(usage?.models) ? usage.models : []
|
|
2272
2578
|
const updated = fmtClock(summary?.updatedAt)
|
|
2273
2579
|
const degraded = summary?.providersDegraded === true
|
|
2580
|
+
// 无数据时只给一行加载提示,避免重挂载后长时间空白(REQ-07)
|
|
2581
|
+
if (summary === null) {
|
|
2582
|
+
return h('div', { 'data-testid': 'token-dash-board-loading', style: styles.empty }, tr('数据加载中'))
|
|
2583
|
+
}
|
|
2274
2584
|
const emptyAll = summary !== null && api.length === 0 && subscriptions.length === 0
|
|
2585
|
+
// 空态快照:确认「空白」到底是走了空态分支还是渲染异常
|
|
2586
|
+
if (api.length + subscriptions.length === 0) {
|
|
2587
|
+
reportClientLog('board', 'render.empty', {
|
|
2588
|
+
api: api.length, subscriptions: subscriptions.length,
|
|
2589
|
+
degraded, emptyAll, hasSummary: summary !== null,
|
|
2590
|
+
})
|
|
2591
|
+
}
|
|
2275
2592
|
|
|
2276
|
-
return h('div', { 'data-testid': 'token-dash-board-scroll', style: styles.boardScroll },
|
|
2593
|
+
return h(BoardBoundary, null, h('div', { 'data-testid': 'token-dash-board-scroll', style: styles.boardScroll },
|
|
2277
2594
|
h('div', { 'data-testid': 'token-dash-board', style: styles.board },
|
|
2278
2595
|
h('style', null, TD_CSS),
|
|
2279
2596
|
h('div', { style: styles.boardHead },
|
|
@@ -2344,7 +2661,7 @@ window.__ModuleLoader__.load({
|
|
|
2344
2661
|
style: styles.settingsHint,
|
|
2345
2662
|
}, tr('语言跟随DSH设置;当前仅中文/English,其他语言回退英文')))),
|
|
2346
2663
|
h('div', { 'data-testid': 'token-dash-settings-capsule', style: styles.capsule },
|
|
2347
|
-
h(SettingsRows, { summary, prefs, onSave: savePrefs, onRefresh: refresh, onRefreshSubs: refreshSubs })))))
|
|
2664
|
+
h(SettingsRows, { summary, prefs, onSave: savePrefs, onRefresh: refresh, onRefreshSubs: refreshSubs }))))))
|
|
2348
2665
|
}
|
|
2349
2666
|
|
|
2350
2667
|
return {
|