@lls-a1/dsh-balance-float 0.0.0-stage → 1.1.42
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.md +90 -2
- package/client.js +1761 -0
- package/cordis.patch.yml +16 -0
- package/icon.svg +13 -0
- package/index.js +284 -0
- package/lib/ledger.js +180 -0
- package/lib/pricing.js +138 -0
- package/lib/snapshot.js +213 -0
- package/lib/tracker.js +203 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +50 -4
package/client.js
ADDED
|
@@ -0,0 +1,1761 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of the balance-float bundle.
|
|
3
|
+
*
|
|
4
|
+
* Registers one entry in the frame-wide `shell.overlay` slot: a small floating
|
|
5
|
+
* pill that reads `/balance-float/snapshot` (the Host's view of the local usage
|
|
6
|
+
* ledger) and expands into a card with today's spend, the peak/valley window
|
|
7
|
+
* and the official per-million-token prices.
|
|
8
|
+
*
|
|
9
|
+
* The pill is draggable; its position is remembered per browser profile. Text
|
|
10
|
+
* goes through the Client locale service so it follows the active language.
|
|
11
|
+
*/
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: 'dsh-balance-float',
|
|
14
|
+
factory(require) {
|
|
15
|
+
const React = require('react');
|
|
16
|
+
// The shell keeps `shell.overlay` in a z-index:20 stacking context, so the
|
|
17
|
+
// app header can still paint over us. A portal on <body> escapes it; if
|
|
18
|
+
// react-dom is not exposed to plugin bundles, fall back to in-place render.
|
|
19
|
+
let createPortal = null;
|
|
20
|
+
try {
|
|
21
|
+
const dom = require('react-dom');
|
|
22
|
+
if (dom !== null && typeof dom.createPortal === 'function') createPortal = dom.createPortal;
|
|
23
|
+
} catch {
|
|
24
|
+
createPortal = null;
|
|
25
|
+
}
|
|
26
|
+
const { useCallback, useEffect, useRef, useState } = React;
|
|
27
|
+
const h = React.createElement;
|
|
28
|
+
|
|
29
|
+
const NS = 'balance-float';
|
|
30
|
+
const ROUTE = '/balance-float/snapshot';
|
|
31
|
+
const ROUTE_UPDATE = '/balance-float/update';
|
|
32
|
+
const NPM_SPEC = '@lls-a1/dsh-balance-float';
|
|
33
|
+
const REPO_URL = 'https://github.com/ming-a1/dsh-balance-float';
|
|
34
|
+
const POS_KEY = 'dsh.balance-float.position';
|
|
35
|
+
// Official top-up page; the balance endpoint has no recharge API, so the
|
|
36
|
+
// panel only links out to the console.
|
|
37
|
+
const TOPUP_URL = 'https://platform.deepseek.com/top_up';
|
|
38
|
+
const APPEARANCE_KEY = 'dsh.balance-float.appearance';
|
|
39
|
+
// `bg` is transparency in percent (0 = solid token background), the other
|
|
40
|
+
// two are strength in percent.
|
|
41
|
+
// 16 is the pill's half height: the default keeps the capsule look while the
|
|
42
|
+
// card gets a matching 16px corner.
|
|
43
|
+
const DEFAULT_APPEARANCE = { bg: 0, border: 100, shadow: 100, radius: 16, dockBall: true };
|
|
44
|
+
// Docking: a drop this close to a side edge turns the pill into a square
|
|
45
|
+
// ball pinned DOCK_MARGIN px from that edge.
|
|
46
|
+
const DOCK_ZONE = 56;
|
|
47
|
+
const DOCK_MARGIN = 8;
|
|
48
|
+
const DOCK_KEY = 'dsh.balance-float.dock';
|
|
49
|
+
// A 4s cadence keeps the balance and today's spend close to the ledger; the
|
|
50
|
+
// route only reads a cached local file, so this stays cheap.
|
|
51
|
+
const POLL_MS = 4000;
|
|
52
|
+
const DEFAULT_CORNER = { right: 18, bottom: 84 };
|
|
53
|
+
const CARD_WIDTH = 316;
|
|
54
|
+
// The composer occupies the bottom band of the Web UI; a bubble needs this
|
|
55
|
+
// much clear space underneath before it is allowed to open downward.
|
|
56
|
+
const BUBBLE_MIN_ROOM = 120;
|
|
57
|
+
|
|
58
|
+
const ZH = {
|
|
59
|
+
title: 'API 余额',
|
|
60
|
+
'pill.balance': '余额',
|
|
61
|
+
'pill.today': '今日',
|
|
62
|
+
balance: '余额',
|
|
63
|
+
topup: '充值 ↗',
|
|
64
|
+
'topup.title': '打开 DeepSeek 官方充值页',
|
|
65
|
+
'balance.note': '今日 0 点 {start},已用 {used}',
|
|
66
|
+
'balance.noStart': '今日已用 {used}',
|
|
67
|
+
today: '今日已用',
|
|
68
|
+
turn: '本轮消耗',
|
|
69
|
+
'turn.label': '本轮',
|
|
70
|
+
'turn.meta': '{time} · {tokens} tokens',
|
|
71
|
+
'turn.empty': '本轮暂无记录',
|
|
72
|
+
'today.calls': '{count} 次调用 · {tokens} tokens',
|
|
73
|
+
'today.split': '峰段 {peak} · 谷段 {valley}',
|
|
74
|
+
peak: '峰谷',
|
|
75
|
+
'peak.label': '峰价',
|
|
76
|
+
valley: '谷价',
|
|
77
|
+
'schedule.title': '峰谷计费时段',
|
|
78
|
+
'tokens.title': 'Token 用量',
|
|
79
|
+
'tokens.input': '未缓存输入',
|
|
80
|
+
'tokens.cache': '缓存读取',
|
|
81
|
+
'tokens.output': '输出',
|
|
82
|
+
'tokens.total': '合计',
|
|
83
|
+
'tokens.hit': '缓存命中',
|
|
84
|
+
'tokens.empty': '分项从本版本开始记录,历史事件只有合计',
|
|
85
|
+
'day.back': '← 返回',
|
|
86
|
+
'day.title': '当日明细',
|
|
87
|
+
'day.calls': '{count} 次调用 · {tokens} tokens',
|
|
88
|
+
'day.empty': '该日没有可用明细(事件已滚动出账本)',
|
|
89
|
+
'weekday.0': '周日',
|
|
90
|
+
'weekday.1': '周一',
|
|
91
|
+
'weekday.2': '周二',
|
|
92
|
+
'weekday.3': '周三',
|
|
93
|
+
'weekday.4': '周四',
|
|
94
|
+
'weekday.5': '周五',
|
|
95
|
+
'weekday.6': '周六',
|
|
96
|
+
'rule.peak': '峰',
|
|
97
|
+
'rule.valley': '谷',
|
|
98
|
+
'schedule.peak': '周一至周五 09:00–12:00、14:00–18:00',
|
|
99
|
+
'schedule.valley': '工作日 12:00–14:00、18:00–次日 09:00,及周末与法定节假日全天',
|
|
100
|
+
'peak.now': '当前按{label}计费',
|
|
101
|
+
'peak.next': '距切到{label} {time}',
|
|
102
|
+
'peak.noNext': '12 天内无切换',
|
|
103
|
+
prices: '单价 元/百万 tokens',
|
|
104
|
+
'prices.off': '谷',
|
|
105
|
+
'prices.peak': '峰',
|
|
106
|
+
'price.hit': '命中',
|
|
107
|
+
'price.miss': '未命中',
|
|
108
|
+
'price.out': '输出',
|
|
109
|
+
history: '近 7 天',
|
|
110
|
+
'history.total': '合计 {total}',
|
|
111
|
+
version: '版本',
|
|
112
|
+
'update.check': '点击检查更新',
|
|
113
|
+
'update.title': '检查更新',
|
|
114
|
+
'update.checking': '正在检查…',
|
|
115
|
+
'update.failed': '检查失败',
|
|
116
|
+
'update.found': '发现新版本 v{latest}(当前 v{current})',
|
|
117
|
+
'update.latest': '最新版本 v{current}',
|
|
118
|
+
'update.label.current': '当前',
|
|
119
|
+
'update.label.latest': '最新',
|
|
120
|
+
'update.now': '更新',
|
|
121
|
+
'update.copy': '复制安装链接',
|
|
122
|
+
'update.copied': '已复制 ✓',
|
|
123
|
+
'update.installHint': '复制后到插件管理器里粘贴安装(npm 包名,安装源可选用国内镜像)。',
|
|
124
|
+
'update.later': '稍后',
|
|
125
|
+
'update.retry': '点击重试',
|
|
126
|
+
source: '本地账本',
|
|
127
|
+
updated: '更新于 {time}',
|
|
128
|
+
refresh: '刷新',
|
|
129
|
+
loading: '读取中…',
|
|
130
|
+
retry: '重试',
|
|
131
|
+
collapse: '收起',
|
|
132
|
+
expand: '展开',
|
|
133
|
+
'error.noLedger': '未找到本地用量账本',
|
|
134
|
+
'error.fetch': '读取失败',
|
|
135
|
+
'error.stale': '账本不是今天的数据',
|
|
136
|
+
settings: '外观设置',
|
|
137
|
+
'settings.open': '外观设置',
|
|
138
|
+
'settings.bg': '背景透明度',
|
|
139
|
+
'settings.border': '边框',
|
|
140
|
+
'settings.shadow': '阴影',
|
|
141
|
+
'settings.radius': '圆角',
|
|
142
|
+
'settings.dock': '贴边收成小球',
|
|
143
|
+
'settings.dock.hint': '拖到左右边缘时收成方形小球,只显示余额',
|
|
144
|
+
'settings.reset': '恢复默认',
|
|
145
|
+
'settings.hint': '只保存在本机浏览器,刷新后仍然生效。',
|
|
146
|
+
'settings.done': '完成',
|
|
147
|
+
close: '关闭',
|
|
148
|
+
'unit.h': '小时',
|
|
149
|
+
'unit.m': '分',
|
|
150
|
+
'unit.d': '天',
|
|
151
|
+
'unit.s': '秒',
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const EN = {
|
|
155
|
+
title: 'API balance',
|
|
156
|
+
'pill.balance': 'Balance',
|
|
157
|
+
'pill.today': 'Today',
|
|
158
|
+
balance: 'Balance',
|
|
159
|
+
topup: 'Top up ↗',
|
|
160
|
+
'topup.title': 'Open the official DeepSeek top-up page',
|
|
161
|
+
'balance.note': 'Started today at {start}, used {used}',
|
|
162
|
+
'balance.noStart': 'Used today {used}',
|
|
163
|
+
today: 'Used today',
|
|
164
|
+
turn: 'Last turn',
|
|
165
|
+
'turn.label': 'Turn',
|
|
166
|
+
'turn.meta': '{time} · {tokens} tokens',
|
|
167
|
+
'turn.empty': 'No turn recorded yet',
|
|
168
|
+
'today.calls': '{count} calls · {tokens} tokens',
|
|
169
|
+
'today.split': 'Peak {peak} · Off-peak {valley}',
|
|
170
|
+
peak: 'Pricing',
|
|
171
|
+
'peak.label': 'Peak',
|
|
172
|
+
valley: 'Off-peak',
|
|
173
|
+
'schedule.title': 'Peak and off-peak windows',
|
|
174
|
+
'tokens.title': 'Token usage',
|
|
175
|
+
'tokens.input': 'Uncached input',
|
|
176
|
+
'tokens.cache': 'Cache read',
|
|
177
|
+
'tokens.output': 'Output',
|
|
178
|
+
'tokens.total': 'Total',
|
|
179
|
+
'tokens.hit': 'Cache hit',
|
|
180
|
+
'tokens.empty': 'Buckets are recorded from this version on; older events only carry the total',
|
|
181
|
+
'day.back': '← Back',
|
|
182
|
+
'day.title': 'Day detail',
|
|
183
|
+
'day.calls': '{count} calls · {tokens} tokens',
|
|
184
|
+
'day.empty': 'No detail for this day (events rolled out of the ledger)',
|
|
185
|
+
'weekday.0': 'Sun',
|
|
186
|
+
'weekday.1': 'Mon',
|
|
187
|
+
'weekday.2': 'Tue',
|
|
188
|
+
'weekday.3': 'Wed',
|
|
189
|
+
'weekday.4': 'Thu',
|
|
190
|
+
'weekday.5': 'Fri',
|
|
191
|
+
'weekday.6': 'Sat',
|
|
192
|
+
'rule.peak': 'Peak',
|
|
193
|
+
'rule.valley': 'Off-peak',
|
|
194
|
+
'schedule.peak': 'Mon–Fri 09:00–12:00, 14:00–18:00',
|
|
195
|
+
'schedule.valley': 'Weekdays 12:00–14:00 and 18:00–09:00 next day, plus weekends and public holidays',
|
|
196
|
+
'peak.now': 'Billed at {label} price now',
|
|
197
|
+
'peak.next': 'Switches to {label} in {time}',
|
|
198
|
+
'peak.noNext': 'No switch within 12 days',
|
|
199
|
+
prices: 'CNY per million tokens',
|
|
200
|
+
'prices.off': 'off-peak',
|
|
201
|
+
'prices.peak': 'peak',
|
|
202
|
+
'price.hit': 'Hit',
|
|
203
|
+
'price.miss': 'Miss',
|
|
204
|
+
'price.out': 'Out',
|
|
205
|
+
history: 'Last 7 days',
|
|
206
|
+
'history.total': 'Total {total}',
|
|
207
|
+
version: 'Version',
|
|
208
|
+
'update.check': 'Click to check for updates',
|
|
209
|
+
'update.title': 'Check for updates',
|
|
210
|
+
'update.checking': 'Checking…',
|
|
211
|
+
'update.failed': 'Update check failed',
|
|
212
|
+
'update.found': 'v{latest} is available (you have v{current})',
|
|
213
|
+
'update.latest': 'Latest version v{current}',
|
|
214
|
+
'update.label.current': 'Current',
|
|
215
|
+
'update.label.latest': 'Latest',
|
|
216
|
+
'update.now': 'Update',
|
|
217
|
+
'update.copy': 'Copy install spec',
|
|
218
|
+
'update.copied': 'Copied ✓',
|
|
219
|
+
'update.installHint': 'Paste this npm package name into the plugin manager to upgrade.',
|
|
220
|
+
'update.later': 'Later',
|
|
221
|
+
'update.retry': 'click to retry',
|
|
222
|
+
source: 'Local ledger',
|
|
223
|
+
updated: 'Updated {time}',
|
|
224
|
+
refresh: 'Refresh',
|
|
225
|
+
loading: 'Reading…',
|
|
226
|
+
retry: 'Retry',
|
|
227
|
+
collapse: 'Collapse',
|
|
228
|
+
expand: 'Expand',
|
|
229
|
+
'error.noLedger': 'No local usage ledger found',
|
|
230
|
+
'error.fetch': 'Could not load the snapshot',
|
|
231
|
+
'error.stale': 'The ledger is not from today',
|
|
232
|
+
settings: 'Appearance',
|
|
233
|
+
'settings.open': 'Appearance',
|
|
234
|
+
'settings.bg': 'Background transparency',
|
|
235
|
+
'settings.border': 'Border',
|
|
236
|
+
'settings.shadow': 'Shadow',
|
|
237
|
+
'settings.radius': 'Corner radius',
|
|
238
|
+
'settings.dock': 'Ball at the edge',
|
|
239
|
+
'settings.dock.hint': 'Dropping on the left or right edge shrinks it to a ball that shows only the balance',
|
|
240
|
+
'settings.reset': 'Reset',
|
|
241
|
+
'settings.hint': 'Saved in this browser only; it survives reloads.',
|
|
242
|
+
'settings.done': 'Done',
|
|
243
|
+
close: 'Close',
|
|
244
|
+
'unit.h': 'h',
|
|
245
|
+
'unit.m': 'm',
|
|
246
|
+
'unit.d': 'd',
|
|
247
|
+
'unit.s': 's',
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const STYLE = [
|
|
251
|
+
'.dbf-root{position:fixed;z-index:200;pointer-events:auto;font-family:inherit;color:var(--dsw-alias-label-primary)}',
|
|
252
|
+
'.dbf-surface{position:relative;isolation:isolate}',
|
|
253
|
+
'.dbf-surface::before{content:"";position:absolute;inset:0;border-radius:inherit;background:var(--dsw-alias-bg-overlay);opacity:var(--dbf-bg,1);z-index:-1;pointer-events:none}',
|
|
254
|
+
'.dbf-surface::after{content:"";position:absolute;inset:0;border-radius:inherit;border:.5px solid var(--dsw-alias-border-l1);opacity:var(--dbf-border,1);pointer-events:none}',
|
|
255
|
+
'@keyframes dbf-open-down{from{opacity:0;transform:translateY(-14px)}to{opacity:1;transform:translateY(0)}}',
|
|
256
|
+
'@keyframes dbf-open-up{from{opacity:0;transform:translateY(14px)}to{opacity:1;transform:translateY(0)}}',
|
|
257
|
+
'@keyframes dbf-collapse-down{to{opacity:0;transform:translateY(-14px)}}',
|
|
258
|
+
'@keyframes dbf-collapse-up{to{opacity:0;transform:translateY(14px)}}',
|
|
259
|
+
'@keyframes dbf-fade{from{opacity:0}to{opacity:1}}',
|
|
260
|
+
'.dbf-card.dbf-leaving,.dbf-pill.dbf-leaving{animation:dbf-collapse-down .18s cubic-bezier(.4,0,.7,.6) forwards !important;pointer-events:none;will-change:transform,opacity}',
|
|
261
|
+
'.dbf-dir-up .dbf-card.dbf-leaving,.dbf-dir-up .dbf-pill.dbf-leaving{animation-name:dbf-collapse-up !important}',
|
|
262
|
+
'.dbf-pill{display:flex;align-items:center;gap:9px;padding:6px 12px;border-radius:calc(var(--dbf-radius,16) * 1px);background:transparent;box-shadow:0 6px 22px rgba(0,0,0,calc(.16 * var(--dbf-shadow,1)));font-size:12px;line-height:20px;white-space:nowrap;cursor:grab;user-select:none;touch-action:none;animation:dbf-fade .16s ease-out both}',
|
|
263
|
+
'.dbf-dir-up .dbf-pill{animation-name:dbf-fade}',
|
|
264
|
+
'.dbf-pill:active{cursor:grabbing}',
|
|
265
|
+
'.dbf-pill-sep{width:.5px;height:12px;background:var(--dsw-alias-border-l2)}',
|
|
266
|
+
'.dbf-label{color:var(--dsw-alias-label-secondary)}',
|
|
267
|
+
'.dbf-num{font-variant-numeric:tabular-nums;font-weight:600}',
|
|
268
|
+
'.dbf-note{color:var(--dsw-alias-label-secondary);font-weight:400}',
|
|
269
|
+
'.dbf-dot{width:7px;height:7px;border-radius:50%;flex:0 0 auto}',
|
|
270
|
+
'.dbf-dot.is-peak{background:var(--dsw-alias-state-error-primary)}',
|
|
271
|
+
'.dbf-dot.is-valley{background:var(--dsw-alias-state-success-primary)}',
|
|
272
|
+
'.dbf-badge{display:inline-flex;align-items:center;gap:5px;padding:1px 7px;border-radius:999px;border:.5px solid var(--dsw-alias-border-l1);background:transparent;font-size:11px;line-height:18px;color:var(--dsw-alias-label-secondary)}',
|
|
273
|
+
'.dbf-card{width:316px;border-radius:calc(var(--dbf-radius,16) * 1px);background:transparent;box-shadow:0 12px 36px rgba(0,0,0,calc(.22 * var(--dbf-shadow,1)));overflow:hidden;font-size:12px;line-height:18px;animation:dbf-open-down .24s cubic-bezier(.22,.61,.36,1) both;display:flex;flex-direction:column;will-change:transform,opacity}',
|
|
274
|
+
'.dbf-dir-up .dbf-card{animation-name:dbf-open-up}',
|
|
275
|
+
'.dbf-head{display:flex;align-items:center;gap:8px;padding:9px 11px;background:transparent;border-bottom:.5px solid var(--dsw-alias-border-l1);cursor:grab;user-select:none;touch-action:none}',
|
|
276
|
+
'.dbf-head.is-static{cursor:default}',
|
|
277
|
+
'.dbf-head:active{cursor:grabbing}',
|
|
278
|
+
'.dbf-head-title{flex:1;min-width:0;font-weight:600}',
|
|
279
|
+
'.dbf-icon{display:inline-flex;align-items:center;justify-content:center;width:22px;height:22px;padding:0;border:0;border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary);cursor:pointer;line-height:1;transition:background .12s ease,color .12s ease}',
|
|
280
|
+
'.dbf-icon svg{display:block}',
|
|
281
|
+
'.dbf-icon:disabled{cursor:default;opacity:.6}',
|
|
282
|
+
'.dbf-spinning svg{animation:dbf-spin .9s linear infinite}',
|
|
283
|
+
'@keyframes dbf-spin{to{transform:rotate(360deg)}}',
|
|
284
|
+
'.dbf-icon:hover{background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary)}',
|
|
285
|
+
'.dbf-body{display:flex;flex-direction:column;gap:0;flex:1 1 auto;min-height:0;max-height:62vh;overflow:auto;scrollbar-width:none;-ms-overflow-style:none;touch-action:auto;overscroll-behavior:contain}',
|
|
286
|
+
'.dbf-body::-webkit-scrollbar{width:0;height:0;display:none}',
|
|
287
|
+
'.dbf-balance-row{display:flex;align-items:center;justify-content:space-between;gap:8px}',
|
|
288
|
+
'.dbf-pill.is-ball{width:46px;height:46px;box-sizing:border-box;padding:0 2px;justify-content:center;gap:0;font-size:13px;line-height:16px;overflow:hidden}',
|
|
289
|
+
'.dbf-pill.is-ball .dbf-num{font-size:13px;font-weight:600;letter-spacing:-.01em}',
|
|
290
|
+
'.dbf-pill.is-ball .dbf-num.is-compact{font-size:11px}',
|
|
291
|
+
'.dbf-bubble.is-side{animation-name:dbf-flash-side}',
|
|
292
|
+
'.dbf-bubble.is-right-of{top:50%;left:calc(100% + 8px);right:auto;bottom:auto;transform:translateY(-50%)}',
|
|
293
|
+
'.dbf-bubble.is-left-of{top:50%;right:calc(100% + 8px);left:auto;bottom:auto;transform:translateY(-50%)}',
|
|
294
|
+
'.dbf-bubble.is-right-of .dbf-bubble-caret{top:50%;left:-4px;margin-top:-4px;transform:rotate(-45deg)}',
|
|
295
|
+
'.dbf-bubble.is-left-of .dbf-bubble-caret{top:50%;right:-4px;left:auto;margin-top:-4px;transform:rotate(135deg)}',
|
|
296
|
+
'@keyframes dbf-flash-side{0%{opacity:0}12%{opacity:1}84%{opacity:1}100%{opacity:0}}',
|
|
297
|
+
'.dbf-switch{flex:none;width:34px;height:20px;padding:0;border-radius:999px;border:1px solid var(--dsw-alias-border-l3,var(--dsw-alias-border-l2));background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-overlay));position:relative;cursor:pointer;transition:background-color .15s ease,border-color .15s ease}',
|
|
298
|
+
'.dbf-switch.is-on{background:var(--dsw-alias-brand-primary,#4d6bfe);border-color:transparent}',
|
|
299
|
+
'.dbf-switch-knob{position:absolute;top:2px;left:2px;width:14px;height:14px;border-radius:50%;background:#fff;transition:transform .15s ease}',
|
|
300
|
+
'.dbf-switch.is-on .dbf-switch-knob{transform:translateX(14px)}',
|
|
301
|
+
'.dbf-topup{flex:none;padding:3px 10px;border-radius:999px;border:1px solid var(--dsw-alias-border-l3,var(--dsw-alias-border-l2,currentColor));background:var(--dsw-alias-button-primary-dimmed,var(--dsw-alias-bg-layer-2));color:var(--dsw-alias-label-primary);font:inherit;font-size:11px;font-weight:600;line-height:16px;cursor:pointer;transition:background-color .15s ease,border-color .15s ease}',
|
|
302
|
+
'.dbf-topup:hover{border-color:var(--dsw-alias-brand-primary,#4d6bfe)}',
|
|
303
|
+
'.dbf-topup:active{transform:translateY(.5px)}',
|
|
304
|
+
'.dbf-countdown{margin-top:8px;margin-bottom:4px;font-size:15px;line-height:22px;letter-spacing:.01em}',
|
|
305
|
+
'.dbf-version{display:inline-flex;align-items:center;gap:4px;margin-left:-5px;padding:1px 6px;border:1px solid transparent;border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary);font:inherit;font-size:11px;line-height:16px;cursor:pointer;transition:background-color .15s ease,border-color .15s ease,color .15s ease}',
|
|
306
|
+
'.dbf-version:hover{border-color:var(--dsw-alias-border-l2,var(--dsw-alias-border-l1));background:color-mix(in srgb,var(--dsw-alias-label-primary) 9%,transparent);color:var(--dsw-alias-label-primary)}',
|
|
307
|
+
'.dbf-version-glyph{font-size:10px;opacity:.75}',
|
|
308
|
+
'.dbf-version.has-update{color:var(--dsw-alias-state-error-primary)}',
|
|
309
|
+
'.dbf-version.has-update .dbf-version-glyph{opacity:1}',
|
|
310
|
+
'.dbf-modal{position:fixed;top:0;right:0;bottom:0;left:0;height:100dvh;z-index:10;display:flex;align-items:center;justify-content:center;padding:calc(12px + env(safe-area-inset-top,0px)) 12px calc(12px + env(safe-area-inset-bottom,0px));background:rgba(0,0,0,.45);overscroll-behavior:contain;touch-action:none}',
|
|
311
|
+
'.dbf-modal-card{width:min(308px,100%);max-height:100%;overflow:auto;touch-action:auto;overscroll-behavior:contain;padding:16px;border-radius:14px;box-shadow:0 18px 48px rgba(0,0,0,calc(.34 * var(--dbf-shadow,1)));background:var(--dsw-alias-bg-layer-1,Canvas);color:var(--dsw-alias-label-primary,CanvasText)}',
|
|
312
|
+
'.dbf-modal-head{display:flex;align-items:center;gap:8px;margin-bottom:10px}',
|
|
313
|
+
'.dbf-modal-icon{width:22px;height:22px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-overlay));color:var(--dsw-alias-brand-primary,#4d6bfe);font-size:12px;line-height:1}',
|
|
314
|
+
'.dbf-modal-title{font-size:13px;font-weight:600}',
|
|
315
|
+
'.dbf-modal-body{font-size:12px;line-height:18px}',
|
|
316
|
+
'.dbf-modal-note{margin-top:6px;font-size:11px;line-height:16px;color:var(--dsw-alias-label-secondary,currentColor);opacity:.85}',
|
|
317
|
+
'.dbf-modal-versions{display:flex;align-items:center;gap:8px;margin-top:8px}',
|
|
318
|
+
'.dbf-modal-version{display:flex;flex-direction:column;gap:1px;padding:4px 8px;border-radius:8px;background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-overlay))}',
|
|
319
|
+
'.dbf-modal-version.is-new{background:color-mix(in srgb,var(--dsw-alias-brand-primary,#4d6bfe) 14%,transparent)}',
|
|
320
|
+
'.dbf-modal-version-label{font-size:10px;line-height:13px;color:var(--dsw-alias-label-secondary,currentColor);opacity:.8}',
|
|
321
|
+
'.dbf-modal-version-value{font-size:12px;line-height:17px;font-weight:600;font-variant-numeric:tabular-nums}',
|
|
322
|
+
'.dbf-modal-arrow{color:var(--dsw-alias-label-tertiary)}',
|
|
323
|
+
'.dbf-btn-primary{padding:4px 12px;border:0;border-radius:8px;background:var(--dsw-alias-label-primary,CanvasText);color:var(--dsw-alias-bg-base,Canvas);font:inherit;font-size:12px;font-weight:600;line-height:18px;cursor:pointer}',
|
|
324
|
+
'.dbf-btn-primary:hover{filter:brightness(1.06)}',
|
|
325
|
+
'.dbf-modal-actions{display:flex;justify-content:flex-end;gap:8px;margin-top:12px}',
|
|
326
|
+
'.dbf-fold-head{cursor:pointer;user-select:none}',
|
|
327
|
+
'.dbf-fold-arrow{display:inline-block;width:10px;margin-right:3px;color:var(--dsw-alias-label-tertiary);font-size:9px}',
|
|
328
|
+
'.dbf-fold-head:hover .dbf-row-title{color:var(--dsw-alias-label-primary)}',
|
|
329
|
+
'.dbf-token-row{display:flex;align-items:baseline;justify-content:space-between;gap:8px;padding:1px 0}',
|
|
330
|
+
'.dbf-models{display:flex;flex-direction:column;gap:2px;margin-top:6px}',
|
|
331
|
+
'.dbf-model-row{display:flex;align-items:baseline;gap:8px}',
|
|
332
|
+
'.dbf-model-name{flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:11px}',
|
|
333
|
+
'.dbf-bar{cursor:pointer}',
|
|
334
|
+
'.dbf-help{flex:none;display:inline-flex;align-items:center;justify-content:center;width:15px;height:15px;margin-left:5px;padding:0;border-radius:50%;border:1px solid var(--dsw-alias-border-l3,var(--dsw-alias-border-l2));background:transparent;color:var(--dsw-alias-label-secondary);font-size:10px;line-height:1;font-weight:600;cursor:pointer}',
|
|
335
|
+
'.dbf-help:hover,.dbf-help.is-open{color:var(--dsw-alias-label-primary);border-color:var(--dsw-alias-brand-primary,#4d6bfe)}',
|
|
336
|
+
'.dbf-hint{margin-top:6px;padding:7px 9px;border-radius:8px;background:var(--dsw-alias-bg-layer-2,var(--dsw-alias-bg-overlay));display:flex;flex-direction:column;gap:3px}',
|
|
337
|
+
'.dbf-rules{margin-top:4px;display:flex;flex-direction:column;gap:2px}',
|
|
338
|
+
'.dbf-rule{display:flex;align-items:flex-start;gap:6px}',
|
|
339
|
+
'.dbf-rule-tag{flex:none;font-weight:600;font-size:11px;line-height:16px;min-width:2.2em}',
|
|
340
|
+
'.dbf-rule-text{color:var(--dsw-alias-label-tertiary);font-size:11px;line-height:16px}',
|
|
341
|
+
'.dbf-block{padding:11px;border-bottom:.5px solid var(--dsw-alias-border-l1)}',
|
|
342
|
+
'.dbf-block:last-child{border-bottom:0}',
|
|
343
|
+
'.dbf-balance{font-size:26px;line-height:32px;font-variant-numeric:tabular-nums;font-weight:650;letter-spacing:-.01em}',
|
|
344
|
+
'.dbf-sub{margin-top:3px;color:var(--dsw-alias-label-secondary)}',
|
|
345
|
+
'.dbf-row{display:flex;align-items:center;gap:8px;justify-content:space-between}',
|
|
346
|
+
'.dbf-row-title{display:flex;align-items:center;gap:7px;font-weight:600}',
|
|
347
|
+
'.dbf-grid{display:grid;grid-template-columns:64px 1fr;gap:4px 8px;margin-top:7px;color:var(--dsw-alias-label-secondary)}',
|
|
348
|
+
'.dbf-grid b{font-weight:600;color:var(--dsw-alias-label-primary);font-variant-numeric:tabular-nums;text-align:right}',
|
|
349
|
+
'.dbf-table{width:100%;margin-top:7px;border-collapse:collapse;font-size:11px;line-height:16px}',
|
|
350
|
+
'.dbf-table th{font-weight:400;color:var(--dsw-alias-label-secondary);text-align:right;padding:0 0 3px}',
|
|
351
|
+
'.dbf-table th:first-child{text-align:left}',
|
|
352
|
+
'.dbf-table td{padding:3px 0;text-align:right;font-variant-numeric:tabular-nums;border-top:.5px solid var(--dsw-alias-border-l1)}',
|
|
353
|
+
'.dbf-table td:first-child{text-align:left;color:var(--dsw-alias-label-secondary)}',
|
|
354
|
+
'.dbf-now{font-weight:600}',
|
|
355
|
+
'.dbf-peak{color:var(--dsw-alias-state-error-primary)}',
|
|
356
|
+
'.dbf-valley{color:var(--dsw-alias-state-success-primary)}',
|
|
357
|
+
'.dbf-badge.dbf-peak{border-color:currentColor}',
|
|
358
|
+
'.dbf-badge.dbf-valley{border-color:currentColor}',
|
|
359
|
+
'.dbf-bars{display:flex;align-items:flex-end;gap:5px;height:46px;margin-top:9px}',
|
|
360
|
+
'.dbf-bar{flex:1;min-width:0;display:flex;flex-direction:column;justify-content:flex-end;align-items:center;gap:3px}',
|
|
361
|
+
'.dbf-bar-fill{width:100%;min-height:2px;border-radius:3px 3px 0 0;background:var(--dsw-alias-border-l2)}',
|
|
362
|
+
'.dbf-bar.is-today .dbf-bar-fill{background:var(--dsw-alias-brand-primary)}',
|
|
363
|
+
'.dbf-bar-day{color:var(--dsw-alias-label-secondary);font-size:9.5px;line-height:12px;font-variant-numeric:tabular-nums}',
|
|
364
|
+
'.dbf-foot{display:flex;align-items:center;justify-content:space-between;gap:6px;padding:7px 11px;background:transparent;border-top:.5px solid var(--dsw-alias-border-l1);color:var(--dsw-alias-label-secondary);font-size:10.5px;line-height:14px}',
|
|
365
|
+
'.dbf-foot-path{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}',
|
|
366
|
+
'.dbf-foot-time{margin-left:auto;white-space:nowrap}',
|
|
367
|
+
'.dbf-bubble{position:absolute;top:calc(100% + 8px);left:4px;display:flex;align-items:baseline;gap:6px;padding:5px 12px;border-radius:10px;box-shadow:0 8px 22px rgba(0,0,0,calc(.18 * var(--dbf-shadow,1)));font-size:13px;line-height:19px;white-space:nowrap;pointer-events:none;animation:dbf-flash 3s ease forwards}',
|
|
368
|
+
'.dbf-bubble-label{color:var(--dsw-alias-label-secondary)}',
|
|
369
|
+
'.dbf-bubble-amount{font-weight:600;font-variant-numeric:tabular-nums}',
|
|
370
|
+
'.dbf-bubble.is-up{top:auto;bottom:calc(100% + 8px);animation-name:dbf-flash-up}',
|
|
371
|
+
'.dbf-bubble-caret{position:absolute;top:-4px;left:14px;width:8px;height:8px;transform:rotate(45deg);background:var(--dsw-alias-bg-overlay);opacity:var(--dbf-bg,1);border-left:.5px solid var(--dsw-alias-border-l1);border-top:.5px solid var(--dsw-alias-border-l1);pointer-events:none}',
|
|
372
|
+
'.dbf-bubble.is-up .dbf-bubble-caret{top:auto;bottom:-4px;border-left:0;border-top:0;border-right:.5px solid var(--dsw-alias-border-l1);border-bottom:.5px solid var(--dsw-alias-border-l1)}',
|
|
373
|
+
'@keyframes dbf-flash{0%{opacity:0;transform:translateY(-4px)}12%{opacity:1;transform:translateY(0)}84%{opacity:1;transform:translateY(0)}100%{opacity:0;transform:translateY(-2px)}}',
|
|
374
|
+
'@keyframes dbf-flash-up{0%{opacity:0;transform:translateY(4px)}12%{opacity:1;transform:translateY(0)}84%{opacity:1;transform:translateY(0)}100%{opacity:0;transform:translateY(2px)}}',
|
|
375
|
+
'.dbf-setting{padding:8px 11px;border-bottom:.5px solid var(--dsw-alias-border-l1)}',
|
|
376
|
+
'.dbf-setting:last-child{border-bottom:0}',
|
|
377
|
+
'.dbf-track{position:relative;height:26px;margin:2px 0 0;cursor:pointer;touch-action:none;border-radius:6px}',
|
|
378
|
+
'.dbf-track:focus-visible{outline:2px solid var(--dsw-alias-brand-primary,#4d6bfe);outline-offset:2px}',
|
|
379
|
+
'.dbf-track-rail{position:absolute;left:0;right:0;top:50%;height:4px;transform:translateY(-50%);border-radius:2px;background:var(--dsw-alias-border-l1)}',
|
|
380
|
+
'.dbf-track-fill{position:absolute;left:0;top:50%;height:4px;transform:translateY(-50%);border-radius:2px;background:var(--dsw-alias-brand-primary,#4d6bfe)}',
|
|
381
|
+
'.dbf-track-thumb{position:absolute;top:50%;width:14px;height:14px;margin-left:-7px;transform:translateY(-50%);border-radius:50%;background:var(--dsw-alias-bg-overlay);border:2px solid var(--dsw-alias-brand-primary,#4d6bfe);box-shadow:0 1px 3px rgba(0,0,0,calc(.3 * var(--dbf-shadow,1)))}',
|
|
382
|
+
'.dbf-setting-val{font-variant-numeric:tabular-nums;color:var(--dsw-alias-label-secondary)}',
|
|
383
|
+
'.dbf-textbtn{padding:1px 9px;border:.5px solid var(--dsw-alias-border-l1);border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary);cursor:pointer;font:inherit;line-height:18px}',
|
|
384
|
+
'.dbf-textbtn:hover{background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary)}',
|
|
385
|
+
'.dbf-error{color:var(--dsw-alias-state-error-primary)}',
|
|
386
|
+
'.dbf-skel{color:var(--dsw-alias-label-secondary)}',
|
|
387
|
+
].join('');
|
|
388
|
+
|
|
389
|
+
/** Tabular money text without a currency glyph. */
|
|
390
|
+
function money(value, digits) {
|
|
391
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—';
|
|
392
|
+
return value.toFixed(digits === undefined ? 2 : digits);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Unit price: keep small values readable and drop trailing zeros. */
|
|
396
|
+
function price(value) {
|
|
397
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '—';
|
|
398
|
+
if (value === 0) return '0';
|
|
399
|
+
const fixed = value >= 10 ? value.toFixed(1) : value >= 1 ? value.toFixed(2) : value.toFixed(3);
|
|
400
|
+
return fixed.replace(/\.?0+$/, '');
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Compact token count. */
|
|
404
|
+
function tokens(value) {
|
|
405
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return '0';
|
|
406
|
+
if (value >= 1e6) return (value / 1e6).toFixed(2) + 'M';
|
|
407
|
+
if (value >= 1e3) return (value / 1e3).toFixed(1) + 'K';
|
|
408
|
+
return String(Math.round(value));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Beijing wall-clock HH:mm for an epoch millisecond value. */
|
|
412
|
+
function clock(ms) {
|
|
413
|
+
if (typeof ms !== 'number' || !Number.isFinite(ms)) return '—';
|
|
414
|
+
const date = new Date(ms + 8 * 3600 * 1000);
|
|
415
|
+
const hh = String(date.getUTCHours()).padStart(2, '0');
|
|
416
|
+
const mm = String(date.getUTCMinutes()).padStart(2, '0');
|
|
417
|
+
return hh + ':' + mm;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** Countdown text localized through the dictionary; seconds are always shown. */
|
|
421
|
+
function duration(t, ms) {
|
|
422
|
+
const total = Math.max(0, Math.round(ms / 1000));
|
|
423
|
+
const days = Math.floor(total / 86400);
|
|
424
|
+
const hours = Math.floor((total % 86400) / 3600);
|
|
425
|
+
const minutes = Math.floor((total % 3600) / 60);
|
|
426
|
+
const seconds = total % 60;
|
|
427
|
+
// Two-digit minutes/seconds: single-digit values made the pill change
|
|
428
|
+
// width every few seconds, which read as the widget jumping around.
|
|
429
|
+
const pad = (value) => (value < 10 ? '0' + value : String(value));
|
|
430
|
+
const parts = [];
|
|
431
|
+
if (days > 0) parts.push(days + t('unit.d'));
|
|
432
|
+
if (days > 0 || hours > 0) parts.push(hours + t('unit.h'));
|
|
433
|
+
if (days > 0 || hours > 0 || minutes > 0) parts.push(pad(minutes) + t('unit.m'));
|
|
434
|
+
parts.push(pad(seconds) + t('unit.s'));
|
|
435
|
+
return parts.join('');
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** One `off-peak / peak` price cell with the active side emphasized. */
|
|
439
|
+
function pair(values, peakActive) {
|
|
440
|
+
const off = Array.isArray(values) ? values[0] : undefined;
|
|
441
|
+
const peak = Array.isArray(values) ? values[1] : undefined;
|
|
442
|
+
return h(
|
|
443
|
+
'span',
|
|
444
|
+
null,
|
|
445
|
+
h('span', { className: peakActive ? '' : 'dbf-now dbf-valley' }, price(off)),
|
|
446
|
+
' / ',
|
|
447
|
+
h('span', { className: peakActive ? 'dbf-now dbf-peak' : '' }, price(peak)),
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/** Circular-arrow refresh glyph; the arc spins while a fetch is in flight. */
|
|
452
|
+
function RefreshIcon() {
|
|
453
|
+
return h(
|
|
454
|
+
'svg',
|
|
455
|
+
{
|
|
456
|
+
viewBox: '0 0 24 24', width: 14, height: 14, fill: 'none',
|
|
457
|
+
stroke: 'currentColor', strokeWidth: 2,
|
|
458
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
459
|
+
'aria-hidden': true, focusable: 'false',
|
|
460
|
+
},
|
|
461
|
+
h('path', { d: 'M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8' }),
|
|
462
|
+
h('path', { d: 'M21 3v5h-5' }),
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Two-slider glyph: opens the appearance panel. */
|
|
467
|
+
function SettingsIcon() {
|
|
468
|
+
return h(
|
|
469
|
+
'svg',
|
|
470
|
+
{
|
|
471
|
+
viewBox: '0 0 24 24', width: 14, height: 14, fill: 'none',
|
|
472
|
+
stroke: 'currentColor', strokeWidth: 2,
|
|
473
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
474
|
+
'aria-hidden': true, focusable: 'false',
|
|
475
|
+
},
|
|
476
|
+
h('path', { d: 'M4 7h9' }),
|
|
477
|
+
h('path', { d: 'M17 7h3' }),
|
|
478
|
+
h('circle', { cx: 15, cy: 7, r: 2 }),
|
|
479
|
+
h('path', { d: 'M4 17h3' }),
|
|
480
|
+
h('path', { d: 'M11 17h9' }),
|
|
481
|
+
h('circle', { cx: 9, cy: 17, r: 2 }),
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** Short model name for the price table. */
|
|
486
|
+
function modelLabel(model) {
|
|
487
|
+
const key = String(model || '').toLowerCase();
|
|
488
|
+
if (key.indexOf('pro') !== -1) return 'Pro';
|
|
489
|
+
if (key.indexOf('flash') !== -1) return 'Flash';
|
|
490
|
+
return String(model || '—');
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Persisted appearance, clamped to the sliders' own ranges. */
|
|
494
|
+
function readAppearance() {
|
|
495
|
+
try {
|
|
496
|
+
const raw = window.localStorage.getItem(APPEARANCE_KEY);
|
|
497
|
+
if (!raw) return { ...DEFAULT_APPEARANCE };
|
|
498
|
+
const parsed = JSON.parse(raw);
|
|
499
|
+
const pick = (value, min, max, fallback) => {
|
|
500
|
+
const n = Number(value);
|
|
501
|
+
if (!Number.isFinite(n)) return fallback;
|
|
502
|
+
return Math.min(max, Math.max(min, Math.round(n / 5) * 5));
|
|
503
|
+
};
|
|
504
|
+
return {
|
|
505
|
+
bg: pick(parsed?.bg, 0, 90, DEFAULT_APPEARANCE.bg),
|
|
506
|
+
border: pick(parsed?.border, 0, 100, DEFAULT_APPEARANCE.border),
|
|
507
|
+
shadow: pick(parsed?.shadow, 0, 100, DEFAULT_APPEARANCE.shadow),
|
|
508
|
+
radius: pick(parsed?.radius, 0, 20, DEFAULT_APPEARANCE.radius),
|
|
509
|
+
dockBall: parsed?.dockBall !== false,
|
|
510
|
+
};
|
|
511
|
+
} catch {
|
|
512
|
+
return { ...DEFAULT_APPEARANCE };
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** Which edge the pill is docked to: 'left' | 'right' | ''. */
|
|
517
|
+
function readDock() {
|
|
518
|
+
try {
|
|
519
|
+
const raw = window.localStorage.getItem(DOCK_KEY);
|
|
520
|
+
return raw === 'left' || raw === 'right' ? raw : '';
|
|
521
|
+
} catch {
|
|
522
|
+
return '';
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function persistDock(side) {
|
|
527
|
+
try {
|
|
528
|
+
window.localStorage.setItem(DOCK_KEY, side);
|
|
529
|
+
} catch {
|
|
530
|
+
// Private mode: docking lasts for this page view only.
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** Persisted drag position, if it is still inside the viewport. */
|
|
535
|
+
function readPosition() {
|
|
536
|
+
try {
|
|
537
|
+
const raw = window.localStorage.getItem(POS_KEY);
|
|
538
|
+
if (!raw) return null;
|
|
539
|
+
const parsed = JSON.parse(raw);
|
|
540
|
+
if (!parsed || !Number.isFinite(parsed.left) || !Number.isFinite(parsed.top)) return null;
|
|
541
|
+
if (parsed.left < -40 || parsed.top < -40) return null;
|
|
542
|
+
if (parsed.left > window.innerWidth - 40 || parsed.top > window.innerHeight - 40) return null;
|
|
543
|
+
return { left: parsed.left, top: parsed.top };
|
|
544
|
+
} catch {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** Translate a frame ({left|right} + {top|bottom}) into CSS. */
|
|
550
|
+
function frameStyle(frame) {
|
|
551
|
+
const out = {};
|
|
552
|
+
if (Number.isFinite(frame.right)) out.right = frame.right + 'px';
|
|
553
|
+
else out.left = frame.left + 'px';
|
|
554
|
+
if (Number.isFinite(frame.bottom)) out.bottom = frame.bottom + 'px';
|
|
555
|
+
else out.top = frame.top + 'px';
|
|
556
|
+
return out;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** Clamp a number. */
|
|
560
|
+
function clamp(value, min, max) {
|
|
561
|
+
return Math.min(Math.max(value, min), max);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// The shell can re-mount the overlay when a run starts or stops. Component
|
|
565
|
+
// state is lost then, so the live position lives at module scope too: a
|
|
566
|
+
// remount must not snap the widget back to the stored default.
|
|
567
|
+
const shared = { frame: null, dir: 'up', dock: '', update: null, updateOpen: false };
|
|
568
|
+
|
|
569
|
+
/** The floating pill and its expanded card. */
|
|
570
|
+
function BalanceFloat(props) {
|
|
571
|
+
const t = props.t;
|
|
572
|
+
const [snapshot, setSnapshot] = useState(null);
|
|
573
|
+
const [failure, setFailure] = useState('');
|
|
574
|
+
const [busy, setBusy] = useState(false);
|
|
575
|
+
const [open, setOpen] = useState(false);
|
|
576
|
+
const [settingsOpen, setSettingsOpen] = useState(false);
|
|
577
|
+
const [leaving, setLeaving] = useState(false);
|
|
578
|
+
const [appearance, setAppearance] = useState(readAppearance);
|
|
579
|
+
const [frame, setFrame] = useState(() => shared.frame ?? readPosition());
|
|
580
|
+
const [dir, setDir] = useState(shared.dir);
|
|
581
|
+
const [dock, setDock] = useState(() => shared.dock || readDock());
|
|
582
|
+
const rootRef = useRef(null);
|
|
583
|
+
const dragRef = useRef(null);
|
|
584
|
+
const positionRef = useRef(null);
|
|
585
|
+
const aliveRef = useRef(true);
|
|
586
|
+
// The per-turn bubble: one flash per settled turn, then it fades itself out.
|
|
587
|
+
const [flash, setFlash] = useState(false);
|
|
588
|
+
// The bubble sits under the pill; near the bottom edge it flips above.
|
|
589
|
+
const [bubbleUp, setBubbleUp] = useState(false);
|
|
590
|
+
// «?» next to the current billing period: reveals the peak/valley windows.
|
|
591
|
+
const [hintOpen, setHintOpen] = useState(false);
|
|
592
|
+
// A bar of the 7-day chart opens that day's detail view.
|
|
593
|
+
const [selectedDay, setSelectedDay] = useState(null);
|
|
594
|
+
// Token buckets are collapsed until asked for.
|
|
595
|
+
const [tokensOpen, setTokensOpen] = useState(false);
|
|
596
|
+
// Update check: null | {phase, latest, current, hasUpdate, error}
|
|
597
|
+
const [update, setUpdate] = useState(shared.update);
|
|
598
|
+
const [updateOpen, setUpdateOpen] = useState(shared.updateOpen);
|
|
599
|
+
const updateCheckedAt = useRef(0);
|
|
600
|
+
const [copied, setCopied] = useState(false);
|
|
601
|
+
|
|
602
|
+
/** Copy the install spec so the user can update from the plugin manager. */
|
|
603
|
+
const copyInstallSpec = useCallback(async () => {
|
|
604
|
+
const spec = NPM_SPEC;
|
|
605
|
+
try {
|
|
606
|
+
await navigator.clipboard.writeText(spec);
|
|
607
|
+
} catch {
|
|
608
|
+
const area = document.createElement('textarea');
|
|
609
|
+
area.value = spec;
|
|
610
|
+
area.style.position = 'fixed';
|
|
611
|
+
area.style.opacity = '0';
|
|
612
|
+
document.body.appendChild(area);
|
|
613
|
+
area.select();
|
|
614
|
+
try {
|
|
615
|
+
document.execCommand('copy');
|
|
616
|
+
} finally {
|
|
617
|
+
document.body.removeChild(area);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
setCopied(true);
|
|
621
|
+
window.setTimeout(() => {
|
|
622
|
+
if (aliveRef.current) setCopied(false);
|
|
623
|
+
}, 2200);
|
|
624
|
+
}, [update]);
|
|
625
|
+
|
|
626
|
+
const flashAtRef = useRef(null);
|
|
627
|
+
const flashTimerRef = useRef(null);
|
|
628
|
+
const leaveTimerRef = useRef(null);
|
|
629
|
+
// Mirror the frame/direction out of React state for the next mount.
|
|
630
|
+
useEffect(() => {
|
|
631
|
+
shared.frame = frame;
|
|
632
|
+
}, [frame]);
|
|
633
|
+
useEffect(() => {
|
|
634
|
+
shared.dir = dir;
|
|
635
|
+
}, [dir]);
|
|
636
|
+
useEffect(() => {
|
|
637
|
+
shared.dock = dock;
|
|
638
|
+
}, [dock]);
|
|
639
|
+
useEffect(() => {
|
|
640
|
+
shared.update = update;
|
|
641
|
+
shared.updateOpen = updateOpen;
|
|
642
|
+
}, [update, updateOpen]);
|
|
643
|
+
|
|
644
|
+
// Forces a re-render so the countdown text follows the clock.
|
|
645
|
+
const [, setTick] = useState(0);
|
|
646
|
+
// Host/browser clock offset, so the switch countdown follows the Host.
|
|
647
|
+
const [offset, setOffset] = useState(0);
|
|
648
|
+
|
|
649
|
+
useEffect(() => {
|
|
650
|
+
aliveRef.current = true;
|
|
651
|
+
return () => {
|
|
652
|
+
aliveRef.current = false;
|
|
653
|
+
if (flashTimerRef.current !== null) window.clearTimeout(flashTimerRef.current);
|
|
654
|
+
if (leaveTimerRef.current !== null) window.clearTimeout(leaveTimerRef.current);
|
|
655
|
+
};
|
|
656
|
+
}, []);
|
|
657
|
+
|
|
658
|
+
useEffect(() => {
|
|
659
|
+
const id = window.setInterval(() => setTick((value) => value + 1), 1000);
|
|
660
|
+
return () => window.clearInterval(id);
|
|
661
|
+
}, []);
|
|
662
|
+
|
|
663
|
+
// A stored dock always hugs its edge, even if the saved top pixel drifted
|
|
664
|
+
// (window resized between sessions).
|
|
665
|
+
useEffect(() => {
|
|
666
|
+
if (dock === '') return;
|
|
667
|
+
const current = shared.frame ?? readPosition();
|
|
668
|
+
const top = current !== null && Number.isFinite(current.top) ? current.top : null;
|
|
669
|
+
if (top === null) return;
|
|
670
|
+
const snapped = dock === 'left' ? { left: DOCK_MARGIN, top } : { right: DOCK_MARGIN, top };
|
|
671
|
+
shared.frame = snapped;
|
|
672
|
+
setFrame(snapped);
|
|
673
|
+
// Only on mount: later changes come from the drag itself.
|
|
674
|
+
}, []);
|
|
675
|
+
|
|
676
|
+
useEffect(() => {
|
|
677
|
+
if (appearance.dockBall || dock === '') return;
|
|
678
|
+
shared.dock = '';
|
|
679
|
+
setDock('');
|
|
680
|
+
persistDock('');
|
|
681
|
+
}, [appearance.dockBall, dock]);
|
|
682
|
+
|
|
683
|
+
// Flip the turn bubble above the pill when the space below is too small
|
|
684
|
+
// (the composer sits there). Re-measure on view changes only: doing it on
|
|
685
|
+
// every drag frame would force a layout read per pointer move.
|
|
686
|
+
useEffect(() => {
|
|
687
|
+
if (!flash || open || settingsOpen) return;
|
|
688
|
+
const rect = rootRef.current?.getBoundingClientRect();
|
|
689
|
+
if (!rect) return;
|
|
690
|
+
setBubbleUp(window.innerHeight - rect.bottom < BUBBLE_MIN_ROOM);
|
|
691
|
+
}, [flash, open, settingsOpen]);
|
|
692
|
+
|
|
693
|
+
// Appearance lives in this browser profile only.
|
|
694
|
+
useEffect(() => {
|
|
695
|
+
try {
|
|
696
|
+
window.localStorage.setItem(APPEARANCE_KEY, JSON.stringify(appearance));
|
|
697
|
+
} catch {
|
|
698
|
+
// Private mode: the panel still works for this page view.
|
|
699
|
+
}
|
|
700
|
+
}, [appearance]);
|
|
701
|
+
|
|
702
|
+
const changeAppearance = useCallback((key, value) => {
|
|
703
|
+
setAppearance((current) => ({ ...current, [key]: value }));
|
|
704
|
+
}, []);
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Show the card, cancelling any in-flight collapse.
|
|
708
|
+
*
|
|
709
|
+
* The panel prefers to unfold downward from the pill; when the pill sits
|
|
710
|
+
* near the bottom edge the free space is above it, so it unfolds upward
|
|
711
|
+
* and the widget keeps its bottom edge in place.
|
|
712
|
+
*/
|
|
713
|
+
const openView = useCallback(() => {
|
|
714
|
+
if (leaveTimerRef.current !== null) {
|
|
715
|
+
window.clearTimeout(leaveTimerRef.current);
|
|
716
|
+
leaveTimerRef.current = null;
|
|
717
|
+
}
|
|
718
|
+
const rect = rootRef.current?.getBoundingClientRect();
|
|
719
|
+
if (rect) {
|
|
720
|
+
const spaceBelow = window.innerHeight - rect.bottom - 12;
|
|
721
|
+
const spaceAbove = rect.top - 12;
|
|
722
|
+
const downward = spaceBelow >= 340 || (spaceBelow >= 220 && spaceBelow >= spaceAbove);
|
|
723
|
+
// The card is wider than the pill: keep the same visual corner by
|
|
724
|
+
// pinning the right edge when the pill sits near the right border.
|
|
725
|
+
const rightGap = window.innerWidth - rect.right;
|
|
726
|
+
const horizontal = rightGap <= CARD_WIDTH + 12 ? { right: rightGap } : { left: rect.left };
|
|
727
|
+
setDir(downward ? 'down' : 'up');
|
|
728
|
+
setFrame({
|
|
729
|
+
...horizontal,
|
|
730
|
+
...(downward ? { top: rect.top } : { bottom: window.innerHeight - rect.bottom }),
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
setLeaving(false);
|
|
734
|
+
setOpen(true);
|
|
735
|
+
}, []);
|
|
736
|
+
|
|
737
|
+
/** Play the collapse animation, then unmount the card. */
|
|
738
|
+
const closeView = useCallback(() => {
|
|
739
|
+
if (leaveTimerRef.current !== null) return;
|
|
740
|
+
setLeaving(true);
|
|
741
|
+
setHintOpen(false);
|
|
742
|
+
setSelectedDay(null);
|
|
743
|
+
setTokensOpen(false);
|
|
744
|
+
leaveTimerRef.current = window.setTimeout(() => {
|
|
745
|
+
leaveTimerRef.current = null;
|
|
746
|
+
setLeaving(false);
|
|
747
|
+
setOpen(false);
|
|
748
|
+
setSettingsOpen(false);
|
|
749
|
+
}, 200);
|
|
750
|
+
}, []);
|
|
751
|
+
|
|
752
|
+
/** Ask the Host whether a newer published version exists. */
|
|
753
|
+
const checkUpdate = useCallback(async (showModal) => {
|
|
754
|
+
const manual = showModal === true;
|
|
755
|
+
if (showModal) setUpdateOpen(true);
|
|
756
|
+
setUpdate({ phase: 'checking', latest: '', current: '', hasUpdate: false, error: '' });
|
|
757
|
+
updateCheckedAt.current = Date.now();
|
|
758
|
+
try {
|
|
759
|
+
const response = await fetch(ROUTE_UPDATE + (manual ? '?fresh=1' : ''), { headers: { accept: 'application/json' }, cache: 'no-store' });
|
|
760
|
+
const body = await response.json();
|
|
761
|
+
if (!aliveRef.current) return;
|
|
762
|
+
if (!response.ok || body?.ok === false) {
|
|
763
|
+
setUpdate({ phase: 'error', latest: '', current: body?.current ?? '', hasUpdate: false, error: body?.error ?? 'HTTP ' + response.status });
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
setUpdate({
|
|
767
|
+
phase: 'done',
|
|
768
|
+
latest: typeof body.latest === 'string' ? body.latest : '',
|
|
769
|
+
current: typeof body.current === 'string' ? body.current : '',
|
|
770
|
+
hasUpdate: body.hasUpdate === true,
|
|
771
|
+
error: '',
|
|
772
|
+
});
|
|
773
|
+
} catch (error) {
|
|
774
|
+
if (!aliveRef.current) return;
|
|
775
|
+
setUpdate({ phase: 'error', latest: '', current: '', hasUpdate: false, error: error instanceof Error ? error.message : String(error) });
|
|
776
|
+
}
|
|
777
|
+
}, []);
|
|
778
|
+
|
|
779
|
+
// Check quietly when the card opens so the hover text already knows, and
|
|
780
|
+
// re-check whenever the local version changed since the last answer.
|
|
781
|
+
useEffect(() => {
|
|
782
|
+
if (!open) return;
|
|
783
|
+
if (update !== null && update.phase === 'checking') return;
|
|
784
|
+
const version = snapshot?.version ?? '';
|
|
785
|
+
const answered = update !== null && update.phase !== 'checking' && (update.current || '') === version;
|
|
786
|
+
if (answered && Date.now() - updateCheckedAt.current < 5 * 60 * 1000) return;
|
|
787
|
+
void checkUpdate(false);
|
|
788
|
+
}, [open, update, snapshot?.version, checkUpdate]);
|
|
789
|
+
|
|
790
|
+
const load = useCallback(async (manual) => {
|
|
791
|
+
if (manual) setBusy(true);
|
|
792
|
+
try {
|
|
793
|
+
const response = await fetch(ROUTE, { headers: { accept: 'application/json' }, cache: 'no-store' });
|
|
794
|
+
const body = await response.json();
|
|
795
|
+
if (!aliveRef.current) return;
|
|
796
|
+
if (!response.ok) throw new Error(typeof body?.error === 'string' && body.error !== '' ? body.error : 'HTTP ' + response.status);
|
|
797
|
+
setSnapshot(body);
|
|
798
|
+
setOffset(typeof body?.serverNow === 'number' ? body.serverNow - Date.now() : 0);
|
|
799
|
+
setFailure('');
|
|
800
|
+
// A new settled turn (new `turn.at`) gets one bubble; the same turn never re-flashes.
|
|
801
|
+
const turnAt = typeof body?.turn?.at === 'number' ? body.turn.at : null;
|
|
802
|
+
if (turnAt !== null && turnAt !== flashAtRef.current) {
|
|
803
|
+
flashAtRef.current = turnAt;
|
|
804
|
+
setFlash(true);
|
|
805
|
+
if (flashTimerRef.current !== null) window.clearTimeout(flashTimerRef.current);
|
|
806
|
+
flashTimerRef.current = window.setTimeout(() => {
|
|
807
|
+
flashTimerRef.current = null;
|
|
808
|
+
if (aliveRef.current) setFlash(false);
|
|
809
|
+
}, 3100);
|
|
810
|
+
}
|
|
811
|
+
} catch (error) {
|
|
812
|
+
if (!aliveRef.current) return;
|
|
813
|
+
setFailure(error instanceof Error && error.message !== '' ? error.message : String(error));
|
|
814
|
+
} finally {
|
|
815
|
+
if (manual && aliveRef.current) setBusy(false);
|
|
816
|
+
}
|
|
817
|
+
}, []);
|
|
818
|
+
|
|
819
|
+
useEffect(() => {
|
|
820
|
+
load();
|
|
821
|
+
const id = window.setInterval(() => {
|
|
822
|
+
if (document.visibilityState !== 'hidden') load();
|
|
823
|
+
}, POLL_MS);
|
|
824
|
+
const onVisible = () => {
|
|
825
|
+
if (document.visibilityState === 'visible') load();
|
|
826
|
+
};
|
|
827
|
+
document.addEventListener('visibilitychange', onVisible);
|
|
828
|
+
window.addEventListener('focus', onVisible);
|
|
829
|
+
return () => {
|
|
830
|
+
window.clearInterval(id);
|
|
831
|
+
document.removeEventListener('visibilitychange', onVisible);
|
|
832
|
+
window.removeEventListener('focus', onVisible);
|
|
833
|
+
};
|
|
834
|
+
}, [load]);
|
|
835
|
+
|
|
836
|
+
// Expanding the card is also a request for the freshest numbers.
|
|
837
|
+
useEffect(() => {
|
|
838
|
+
if (open) load();
|
|
839
|
+
}, [open, load]);
|
|
840
|
+
|
|
841
|
+
const persist = useCallback((next) => {
|
|
842
|
+
try {
|
|
843
|
+
window.localStorage.setItem(POS_KEY, JSON.stringify(next));
|
|
844
|
+
} catch {
|
|
845
|
+
// Private mode: the position lasts for this page view only.
|
|
846
|
+
}
|
|
847
|
+
}, []);
|
|
848
|
+
|
|
849
|
+
// localStorage writes are synchronous: during a drag keep them rare.
|
|
850
|
+
const lastPersistRef = useRef(0);
|
|
851
|
+
const persistThrottled = useCallback(
|
|
852
|
+
(next) => {
|
|
853
|
+
const now = Date.now();
|
|
854
|
+
if (now - lastPersistRef.current < 250) return;
|
|
855
|
+
lastPersistRef.current = now;
|
|
856
|
+
persist(next);
|
|
857
|
+
},
|
|
858
|
+
[persist],
|
|
859
|
+
);
|
|
860
|
+
|
|
861
|
+
// A drag writes straight to the DOM instead of React state, and owns the
|
|
862
|
+
// pointer at the window capture stage: the shell's drawer gesture listens
|
|
863
|
+
// on document capture, which would otherwise see (and act on) the swipe.
|
|
864
|
+
const liveRef = useRef(null);
|
|
865
|
+
const detachRef = useRef(null);
|
|
866
|
+
|
|
867
|
+
const commitDrag = useCallback(() => {
|
|
868
|
+
const last = liveRef.current;
|
|
869
|
+
if (last === null) return;
|
|
870
|
+
shared.frame = { left: last.left, top: last.top };
|
|
871
|
+
setFrame({ left: last.left, top: last.top });
|
|
872
|
+
persist(last);
|
|
873
|
+
}, [persist]);
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Appearance slider. A native <input type=range> loses the gesture to the
|
|
877
|
+
* shell (the horizontal drag pans the frame and opens the sidebar), so the
|
|
878
|
+
* track computes the value itself and blocks the swipe at window capture.
|
|
879
|
+
*/
|
|
880
|
+
const startSlider = useCallback(
|
|
881
|
+
(key, max, event) => {
|
|
882
|
+
const track = event.currentTarget;
|
|
883
|
+
const rect = track.getBoundingClientRect();
|
|
884
|
+
const apply = (clientX) => {
|
|
885
|
+
const ratio = clamp((clientX - rect.left) / Math.max(1, rect.width), 0, 1);
|
|
886
|
+
changeAppearance(key, clamp(Math.round((ratio * max) / 5) * 5, 0, max));
|
|
887
|
+
};
|
|
888
|
+
apply(event.clientX);
|
|
889
|
+
track.setPointerCapture?.(event.pointerId);
|
|
890
|
+
const onMove = (moveEvent) => {
|
|
891
|
+
if (moveEvent.pointerId !== event.pointerId) return;
|
|
892
|
+
moveEvent.stopImmediatePropagation();
|
|
893
|
+
if (moveEvent.cancelable) moveEvent.preventDefault();
|
|
894
|
+
apply(moveEvent.clientX);
|
|
895
|
+
};
|
|
896
|
+
const onTouch = (touchEvent) => {
|
|
897
|
+
touchEvent.stopImmediatePropagation();
|
|
898
|
+
if (touchEvent.cancelable) touchEvent.preventDefault();
|
|
899
|
+
};
|
|
900
|
+
const detach = () => {
|
|
901
|
+
window.removeEventListener('pointermove', onMove, true);
|
|
902
|
+
window.removeEventListener('pointerup', onEnd, true);
|
|
903
|
+
window.removeEventListener('pointercancel', onEnd, true);
|
|
904
|
+
window.removeEventListener('touchmove', onTouch, true);
|
|
905
|
+
};
|
|
906
|
+
const onEnd = (endEvent) => {
|
|
907
|
+
if (endEvent.pointerId !== event.pointerId) return;
|
|
908
|
+
endEvent.stopImmediatePropagation();
|
|
909
|
+
detach();
|
|
910
|
+
};
|
|
911
|
+
window.addEventListener('pointermove', onMove, true);
|
|
912
|
+
window.addEventListener('pointerup', onEnd, true);
|
|
913
|
+
window.addEventListener('pointercancel', onEnd, true);
|
|
914
|
+
window.addEventListener('touchmove', onTouch, { capture: true, passive: false });
|
|
915
|
+
},
|
|
916
|
+
[changeAppearance],
|
|
917
|
+
);
|
|
918
|
+
|
|
919
|
+
const onPointerDown = useCallback((event) => {
|
|
920
|
+
if (event.button !== 0) return;
|
|
921
|
+
// The header also carries buttons: a click there is not a drag/collapse.
|
|
922
|
+
if (event.target instanceof Element && event.target.closest('button') !== null) return;
|
|
923
|
+
const rect = rootRef.current?.getBoundingClientRect();
|
|
924
|
+
if (!rect) return;
|
|
925
|
+
if (detachRef.current !== null) detachRef.current();
|
|
926
|
+
const drag = {
|
|
927
|
+
id: event.pointerId,
|
|
928
|
+
x: event.clientX,
|
|
929
|
+
y: event.clientY,
|
|
930
|
+
left: rect.left,
|
|
931
|
+
top: rect.top,
|
|
932
|
+
width: rect.width,
|
|
933
|
+
height: rect.height,
|
|
934
|
+
moved: false,
|
|
935
|
+
};
|
|
936
|
+
dragRef.current = drag;
|
|
937
|
+
liveRef.current = { left: rect.left, top: rect.top };
|
|
938
|
+
event.currentTarget.setPointerCapture?.(event.pointerId);
|
|
939
|
+
|
|
940
|
+
const onMove = (moveEvent) => {
|
|
941
|
+
if (moveEvent.pointerId !== drag.id) return;
|
|
942
|
+
if (moveEvent.cancelable) moveEvent.preventDefault();
|
|
943
|
+
moveEvent.stopImmediatePropagation();
|
|
944
|
+
const dx = moveEvent.clientX - drag.x;
|
|
945
|
+
const dy = moveEvent.clientY - drag.y;
|
|
946
|
+
if (!drag.moved && Math.abs(dx) + Math.abs(dy) < 5) return;
|
|
947
|
+
drag.moved = true;
|
|
948
|
+
const width = drag.width > 0 ? drag.width : 120;
|
|
949
|
+
const height = drag.height > 0 ? drag.height : 40;
|
|
950
|
+
const next = {
|
|
951
|
+
left: clamp(drag.left + dx, 4, Math.max(4, window.innerWidth - width - 4)),
|
|
952
|
+
top: clamp(drag.top + dy, 4, Math.max(4, window.innerHeight - height - 4)),
|
|
953
|
+
};
|
|
954
|
+
positionRef.current = next;
|
|
955
|
+
liveRef.current = next;
|
|
956
|
+
shared.frame = { left: next.left, top: next.top };
|
|
957
|
+
const node = rootRef.current;
|
|
958
|
+
if (node !== null) {
|
|
959
|
+
node.style.left = next.left + 'px';
|
|
960
|
+
node.style.top = next.top + 'px';
|
|
961
|
+
node.style.right = 'auto';
|
|
962
|
+
node.style.bottom = 'auto';
|
|
963
|
+
}
|
|
964
|
+
persistThrottled(next);
|
|
965
|
+
};
|
|
966
|
+
// Some WebViews still deliver TouchEvents first: cancel those too, or
|
|
967
|
+
// the page pans and the shell treats the swipe as "open the drawer".
|
|
968
|
+
const onTouch = (touchEvent) => {
|
|
969
|
+
if (touchEvent.cancelable) touchEvent.preventDefault();
|
|
970
|
+
touchEvent.stopImmediatePropagation();
|
|
971
|
+
};
|
|
972
|
+
const detach = () => {
|
|
973
|
+
window.removeEventListener('pointermove', onMove, true);
|
|
974
|
+
window.removeEventListener('pointerup', onEnd, true);
|
|
975
|
+
window.removeEventListener('pointercancel', onEnd, true);
|
|
976
|
+
window.removeEventListener('touchmove', onTouch, true);
|
|
977
|
+
detachRef.current = null;
|
|
978
|
+
};
|
|
979
|
+
const onEnd = (endEvent) => {
|
|
980
|
+
if (endEvent.pointerId !== drag.id) return;
|
|
981
|
+
endEvent.stopImmediatePropagation();
|
|
982
|
+
detach();
|
|
983
|
+
dragRef.current = null;
|
|
984
|
+
if (drag.moved) {
|
|
985
|
+
commitDrag();
|
|
986
|
+
const node = rootRef.current;
|
|
987
|
+
if (node !== null && appearance.dockBall) {
|
|
988
|
+
const rect = node.getBoundingClientRect();
|
|
989
|
+
const side = rect.left <= DOCK_ZONE ? 'left' : window.innerWidth - rect.right <= DOCK_ZONE ? 'right' : '';
|
|
990
|
+
if (side !== dock) {
|
|
991
|
+
shared.dock = side;
|
|
992
|
+
setDock(side);
|
|
993
|
+
persistDock(side);
|
|
994
|
+
}
|
|
995
|
+
if (side !== '') {
|
|
996
|
+
const snapped = side === 'left'
|
|
997
|
+
? { left: DOCK_MARGIN, top: rect.top }
|
|
998
|
+
: { right: DOCK_MARGIN, top: rect.top };
|
|
999
|
+
shared.frame = snapped;
|
|
1000
|
+
setFrame(snapped);
|
|
1001
|
+
persist(side === 'left' ? { left: DOCK_MARGIN, top: rect.top } : { left: Math.max(4, window.innerWidth - 46 - DOCK_MARGIN), top: rect.top });
|
|
1002
|
+
}
|
|
1003
|
+
} else if (node !== null && dock !== '') {
|
|
1004
|
+
shared.dock = '';
|
|
1005
|
+
setDock('');
|
|
1006
|
+
persistDock('');
|
|
1007
|
+
}
|
|
1008
|
+
return;
|
|
1009
|
+
}
|
|
1010
|
+
if (open) closeView();
|
|
1011
|
+
else openView();
|
|
1012
|
+
};
|
|
1013
|
+
detachRef.current = detach;
|
|
1014
|
+
window.addEventListener('pointermove', onMove, true);
|
|
1015
|
+
window.addEventListener('pointerup', onEnd, true);
|
|
1016
|
+
window.addEventListener('pointercancel', onEnd, true);
|
|
1017
|
+
window.addEventListener('touchmove', onTouch, { capture: true, passive: false });
|
|
1018
|
+
}, [persistThrottled, commitDrag, open, closeView, openView, appearance.dockBall, dock]);
|
|
1019
|
+
|
|
1020
|
+
useEffect(() => () => {
|
|
1021
|
+
if (detachRef.current !== null) detachRef.current();
|
|
1022
|
+
dragRef.current = null;
|
|
1023
|
+
}, []);
|
|
1024
|
+
|
|
1025
|
+
const now = Date.now() + offset;
|
|
1026
|
+
const peakActive = snapshot?.peak?.active === true;
|
|
1027
|
+
const periodLabel = peakActive ? t('peak.label') : t('valley');
|
|
1028
|
+
const periodClass = peakActive ? 'dbf-peak' : 'dbf-valley';
|
|
1029
|
+
const nextAt = snapshot?.peak?.nextChangeAt;
|
|
1030
|
+
const countdown = typeof nextAt === 'number' ? duration(t, nextAt - now) : '';
|
|
1031
|
+
const stale = typeof snapshot?.today === 'string' && snapshot.today !== new Date(now + 8 * 3600 * 1000).toISOString().slice(0, 10);
|
|
1032
|
+
const prices = Array.isArray(snapshot?.prices) ? snapshot.prices : [];
|
|
1033
|
+
const history = Array.isArray(snapshot?.history) ? snapshot.history : [];
|
|
1034
|
+
const historyMax = typeof snapshot?.historyMax === 'number' && snapshot.historyMax > 0 ? snapshot.historyMax : 0;
|
|
1035
|
+
const models = Array.isArray(snapshot?.models) ? snapshot.models : [];
|
|
1036
|
+
const turn = snapshot?.turn ?? null;
|
|
1037
|
+
|
|
1038
|
+
const liveFrame = dragRef.current !== null && liveRef.current !== null ? liveRef.current : frame;
|
|
1039
|
+
const style = liveFrame ? frameStyle(liveFrame) : { right: DEFAULT_CORNER.right + 'px', bottom: DEFAULT_CORNER.bottom + 'px' };
|
|
1040
|
+
// Unfolding downward must not run off the viewport: cap the card and let
|
|
1041
|
+
// its body scroll.
|
|
1042
|
+
const cardStyle =
|
|
1043
|
+
dir === 'down' && frame !== null && Number.isFinite(frame.top)
|
|
1044
|
+
? { maxHeight: Math.max(180, window.innerHeight - frame.top - 16) + 'px' }
|
|
1045
|
+
: undefined;
|
|
1046
|
+
const surfaceStyle = {
|
|
1047
|
+
...style,
|
|
1048
|
+
'--dbf-origin': dir === 'down' ? '50% 0%' : '50% 100%',
|
|
1049
|
+
'--dbf-bg': String((100 - appearance.bg) / 100),
|
|
1050
|
+
'--dbf-border': String(appearance.border / 100),
|
|
1051
|
+
'--dbf-shadow': String(appearance.shadow / 100),
|
|
1052
|
+
'--dbf-radius': String(appearance.radius),
|
|
1053
|
+
};
|
|
1054
|
+
|
|
1055
|
+
const dot = h('span', { className: 'dbf-dot ' + (peakActive ? 'is-peak' : 'is-valley'), 'aria-hidden': true });
|
|
1056
|
+
|
|
1057
|
+
// The version control's hover text reports the last check's outcome.
|
|
1058
|
+
const updateTitle =
|
|
1059
|
+
update === null
|
|
1060
|
+
? t('update.check')
|
|
1061
|
+
: update.phase === 'checking'
|
|
1062
|
+
? t('update.checking')
|
|
1063
|
+
: update.phase === 'error'
|
|
1064
|
+
? t('update.failed') + ' · ' + t('update.retry')
|
|
1065
|
+
: update.hasUpdate
|
|
1066
|
+
? t('update.found', { latest: update.latest, current: update.current }) + ' · ' + t('update.now')
|
|
1067
|
+
: t('update.latest', { current: update.current || snapshot?.version || '—' });
|
|
1068
|
+
|
|
1069
|
+
// Docked to a side edge: a square ball that only shows the balance.
|
|
1070
|
+
const docked = appearance.dockBall && dock !== '';
|
|
1071
|
+
const balanceText = '¥' + money(snapshot?.balance);
|
|
1072
|
+
const pillProps = {
|
|
1073
|
+
key: 'pill',
|
|
1074
|
+
className: 'dbf-pill dbf-surface' + (docked ? ' is-ball' : ''),
|
|
1075
|
+
role: 'button',
|
|
1076
|
+
tabIndex: 0,
|
|
1077
|
+
'aria-label': t('title'),
|
|
1078
|
+
title: t('title'),
|
|
1079
|
+
onPointerDown,
|
|
1080
|
+
onKeyDown: (event) => {
|
|
1081
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
1082
|
+
event.preventDefault();
|
|
1083
|
+
if (open) closeView();
|
|
1084
|
+
else openView();
|
|
1085
|
+
}
|
|
1086
|
+
},
|
|
1087
|
+
};
|
|
1088
|
+
const pill = docked
|
|
1089
|
+
? h('div', pillProps, h('span', { className: 'dbf-num' + (balanceText.length > 6 ? ' is-compact' : '') }, balanceText))
|
|
1090
|
+
: h(
|
|
1091
|
+
'div',
|
|
1092
|
+
pillProps,
|
|
1093
|
+
dot,
|
|
1094
|
+
h('span', { className: 'dbf-label' }, t('pill.balance')),
|
|
1095
|
+
h('span', { className: 'dbf-num' }, '¥' + money(snapshot?.balance)),
|
|
1096
|
+
h('span', { className: 'dbf-pill-sep' }),
|
|
1097
|
+
h('span', { className: 'dbf-label' }, t('pill.today')),
|
|
1098
|
+
h('span', { className: 'dbf-num' }, '¥' + money(snapshot?.todayUsed)),
|
|
1099
|
+
h('span', { className: 'dbf-pill-sep' }),
|
|
1100
|
+
h('span', { className: 'dbf-label ' + periodClass }, periodLabel),
|
|
1101
|
+
countdown !== '' ? h('span', { className: 'dbf-num' }, countdown) : null,
|
|
1102
|
+
);
|
|
1103
|
+
|
|
1104
|
+
// The card subtree is only built when it can be seen: rebuilding ~150
|
|
1105
|
+
// elements on every 1s tick / pointer move is avoidable work.
|
|
1106
|
+
let card = null;
|
|
1107
|
+
if (open || leaving) {
|
|
1108
|
+
const header = h(
|
|
1109
|
+
'div',
|
|
1110
|
+
{
|
|
1111
|
+
className: 'dbf-head',
|
|
1112
|
+
title: t('collapse'),
|
|
1113
|
+
onPointerDown,
|
|
1114
|
+
},
|
|
1115
|
+
dot,
|
|
1116
|
+
h('span', { className: 'dbf-head-title' }, t('title')),
|
|
1117
|
+
h(
|
|
1118
|
+
'button',
|
|
1119
|
+
{
|
|
1120
|
+
type: 'button',
|
|
1121
|
+
className: 'dbf-icon',
|
|
1122
|
+
title: t('settings.open'),
|
|
1123
|
+
'aria-label': t('settings.open'),
|
|
1124
|
+
onClick: () => setSettingsOpen(true),
|
|
1125
|
+
},
|
|
1126
|
+
h(SettingsIcon),
|
|
1127
|
+
),
|
|
1128
|
+
h(
|
|
1129
|
+
'button',
|
|
1130
|
+
{
|
|
1131
|
+
type: 'button',
|
|
1132
|
+
className: busy ? 'dbf-icon dbf-spinning' : 'dbf-icon',
|
|
1133
|
+
title: t('refresh'),
|
|
1134
|
+
'aria-label': t('refresh'),
|
|
1135
|
+
onClick: () => load(true),
|
|
1136
|
+
disabled: busy,
|
|
1137
|
+
},
|
|
1138
|
+
h(RefreshIcon),
|
|
1139
|
+
),
|
|
1140
|
+
);
|
|
1141
|
+
|
|
1142
|
+
const balanceBlock = h(
|
|
1143
|
+
'div',
|
|
1144
|
+
{ className: 'dbf-block' },
|
|
1145
|
+
h(
|
|
1146
|
+
'div',
|
|
1147
|
+
{ className: 'dbf-balance-row' },
|
|
1148
|
+
h('div', { className: 'dbf-balance' }, '¥' + money(snapshot?.balance)),
|
|
1149
|
+
h(
|
|
1150
|
+
'button',
|
|
1151
|
+
{
|
|
1152
|
+
type: 'button',
|
|
1153
|
+
className: 'dbf-topup',
|
|
1154
|
+
title: t('topup.title'),
|
|
1155
|
+
onClick: () => window.open(TOPUP_URL, '_blank', 'noopener,noreferrer'),
|
|
1156
|
+
},
|
|
1157
|
+
t('topup'),
|
|
1158
|
+
),
|
|
1159
|
+
),
|
|
1160
|
+
h(
|
|
1161
|
+
'div',
|
|
1162
|
+
{ className: 'dbf-sub' },
|
|
1163
|
+
typeof snapshot?.dayStart === 'number'
|
|
1164
|
+
? t('balance.note', { start: '¥' + money(snapshot.dayStart), used: '¥' + money(snapshot.todayUsed) })
|
|
1165
|
+
: t('balance.noStart', { used: '¥' + money(snapshot?.todayUsed) }),
|
|
1166
|
+
),
|
|
1167
|
+
h(
|
|
1168
|
+
'div',
|
|
1169
|
+
{ className: 'dbf-row', style: { marginTop: '7px' } },
|
|
1170
|
+
h('span', { className: 'dbf-label' }, t('today')),
|
|
1171
|
+
h('span', { className: 'dbf-num' }, '¥' + money(snapshot?.todayUsed)),
|
|
1172
|
+
),
|
|
1173
|
+
h(
|
|
1174
|
+
'div',
|
|
1175
|
+
{ className: 'dbf-sub', style: { marginTop: '1px' } },
|
|
1176
|
+
t('today.calls', { count: snapshot?.todayCalls ?? 0, tokens: tokens(snapshot?.todayTokens ?? 0) }),
|
|
1177
|
+
),
|
|
1178
|
+
h(
|
|
1179
|
+
'div',
|
|
1180
|
+
{ className: 'dbf-sub', style: { marginTop: '1px' } },
|
|
1181
|
+
t('today.split', { peak: '¥' + money(snapshot?.todayPeakCost), valley: '¥' + money(snapshot?.todayValleyCost) }),
|
|
1182
|
+
),
|
|
1183
|
+
h(
|
|
1184
|
+
'div',
|
|
1185
|
+
{ className: 'dbf-row', style: { marginTop: '7px', paddingTop: '7px', borderTop: '.5px solid var(--dsw-alias-border-l1)' } },
|
|
1186
|
+
h('span', { className: 'dbf-label' }, t('turn')),
|
|
1187
|
+
h('span', { className: 'dbf-num' }, '¥' + money(turn?.cost)),
|
|
1188
|
+
),
|
|
1189
|
+
h(
|
|
1190
|
+
'div',
|
|
1191
|
+
{ className: 'dbf-sub', style: { marginTop: '1px' } },
|
|
1192
|
+
turn === null
|
|
1193
|
+
? t('turn.empty')
|
|
1194
|
+
: t('turn.meta', { time: clock(turn.at), tokens: tokens(turn.tokens) }),
|
|
1195
|
+
),
|
|
1196
|
+
stale ? h('div', { className: 'dbf-error', style: { marginTop: '4px' } }, t('error.stale')) : null,
|
|
1197
|
+
);
|
|
1198
|
+
|
|
1199
|
+
const inputTokens = typeof snapshot?.todayInput === 'number' ? snapshot.todayInput : 0;
|
|
1200
|
+
const cacheTokens = typeof snapshot?.todayCacheRead === 'number' ? snapshot.todayCacheRead : 0;
|
|
1201
|
+
const outputTokens = typeof snapshot?.todayOutput === 'number' ? snapshot.todayOutput : 0;
|
|
1202
|
+
const tokenSum = inputTokens + cacheTokens + outputTokens;
|
|
1203
|
+
const promptSum = inputTokens + cacheTokens;
|
|
1204
|
+
const hitRate = promptSum > 0 ? (cacheTokens / promptSum) * 100 : null;
|
|
1205
|
+
const tokenLine = (label, value) =>
|
|
1206
|
+
h(
|
|
1207
|
+
'div',
|
|
1208
|
+
{ className: 'dbf-token-row' },
|
|
1209
|
+
h('span', { className: 'dbf-sub' }, label),
|
|
1210
|
+
h('span', { className: 'dbf-num' }, value),
|
|
1211
|
+
);
|
|
1212
|
+
const tokenBlock = h(
|
|
1213
|
+
'div',
|
|
1214
|
+
{ className: 'dbf-block' },
|
|
1215
|
+
h(
|
|
1216
|
+
'div',
|
|
1217
|
+
{
|
|
1218
|
+
className: 'dbf-row dbf-fold-head',
|
|
1219
|
+
role: 'button',
|
|
1220
|
+
tabIndex: 0,
|
|
1221
|
+
'aria-expanded': tokensOpen ? 'true' : 'false',
|
|
1222
|
+
onClick: () => setTokensOpen((value) => !value),
|
|
1223
|
+
onKeyDown: (event) => {
|
|
1224
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
1225
|
+
event.preventDefault();
|
|
1226
|
+
setTokensOpen((value) => !value);
|
|
1227
|
+
}
|
|
1228
|
+
},
|
|
1229
|
+
},
|
|
1230
|
+
h(
|
|
1231
|
+
'span',
|
|
1232
|
+
{ className: 'dbf-row-title' },
|
|
1233
|
+
h('span', { className: 'dbf-fold-arrow', 'aria-hidden': true }, tokensOpen ? '▾' : '▸'),
|
|
1234
|
+
t('tokens.title'),
|
|
1235
|
+
),
|
|
1236
|
+
h('span', { className: 'dbf-sub' }, tokens(tokenSum)),
|
|
1237
|
+
),
|
|
1238
|
+
tokensOpen
|
|
1239
|
+
? h(
|
|
1240
|
+
'div',
|
|
1241
|
+
null,
|
|
1242
|
+
tokenLine(t('tokens.hit'), hitRate === null ? '—' : hitRate.toFixed(1) + '%'),
|
|
1243
|
+
tokenLine(t('tokens.input'), tokens(inputTokens)),
|
|
1244
|
+
tokenLine(t('tokens.cache'), tokens(cacheTokens)),
|
|
1245
|
+
tokenLine(t('tokens.output'), tokens(outputTokens)),
|
|
1246
|
+
tokenSum > 0 ? null : h('div', { className: 'dbf-sub', style: { marginTop: '2px' } }, t('tokens.empty')),
|
|
1247
|
+
)
|
|
1248
|
+
: null,
|
|
1249
|
+
);
|
|
1250
|
+
|
|
1251
|
+
const peakBlock = h(
|
|
1252
|
+
'div',
|
|
1253
|
+
{ className: 'dbf-block' },
|
|
1254
|
+
h(
|
|
1255
|
+
'div',
|
|
1256
|
+
{ className: 'dbf-row' },
|
|
1257
|
+
h(
|
|
1258
|
+
'span',
|
|
1259
|
+
{ className: 'dbf-row-title ' + periodClass },
|
|
1260
|
+
dot,
|
|
1261
|
+
t('peak.now', { label: periodLabel }),
|
|
1262
|
+
h(
|
|
1263
|
+
'button',
|
|
1264
|
+
{
|
|
1265
|
+
type: 'button',
|
|
1266
|
+
className: 'dbf-help' + (hintOpen ? ' is-open' : ''),
|
|
1267
|
+
title: t('schedule.title'),
|
|
1268
|
+
'aria-label': t('schedule.title'),
|
|
1269
|
+
'aria-expanded': hintOpen ? 'true' : 'false',
|
|
1270
|
+
onClick: () => setHintOpen((value) => !value),
|
|
1271
|
+
},
|
|
1272
|
+
'?',
|
|
1273
|
+
),
|
|
1274
|
+
),
|
|
1275
|
+
h('span', { className: 'dbf-badge ' + periodClass }, periodLabel),
|
|
1276
|
+
),
|
|
1277
|
+
h(
|
|
1278
|
+
'div',
|
|
1279
|
+
{ className: 'dbf-sub dbf-countdown' },
|
|
1280
|
+
countdown !== ''
|
|
1281
|
+
? t('peak.next', { label: peakActive ? t('valley') : t('peak.label'), time: countdown })
|
|
1282
|
+
: t('peak.noNext'),
|
|
1283
|
+
),
|
|
1284
|
+
hintOpen
|
|
1285
|
+
? h(
|
|
1286
|
+
'div',
|
|
1287
|
+
{ className: 'dbf-hint', role: 'note' },
|
|
1288
|
+
h(
|
|
1289
|
+
'div',
|
|
1290
|
+
{ className: 'dbf-rule' },
|
|
1291
|
+
h('span', { className: 'dbf-rule-tag dbf-peak' }, t('rule.peak')),
|
|
1292
|
+
h('span', { className: 'dbf-rule-text' }, t('schedule.peak')),
|
|
1293
|
+
),
|
|
1294
|
+
h(
|
|
1295
|
+
'div',
|
|
1296
|
+
{ className: 'dbf-rule' },
|
|
1297
|
+
h('span', { className: 'dbf-rule-tag dbf-valley' }, t('rule.valley')),
|
|
1298
|
+
h('span', { className: 'dbf-rule-text' }, t('schedule.valley')),
|
|
1299
|
+
),
|
|
1300
|
+
)
|
|
1301
|
+
: null,
|
|
1302
|
+
);
|
|
1303
|
+
|
|
1304
|
+
const priceTable = h(
|
|
1305
|
+
'div',
|
|
1306
|
+
{ className: 'dbf-block' },
|
|
1307
|
+
h(
|
|
1308
|
+
'div',
|
|
1309
|
+
{ className: 'dbf-row-title' },
|
|
1310
|
+
t('prices'),
|
|
1311
|
+
h(
|
|
1312
|
+
'span',
|
|
1313
|
+
{ className: 'dbf-sub', style: { marginLeft: '6px', fontWeight: 400 } },
|
|
1314
|
+
'(',
|
|
1315
|
+
h('span', { className: 'dbf-valley' }, t('prices.off')),
|
|
1316
|
+
' / ',
|
|
1317
|
+
h('span', { className: 'dbf-peak' }, t('prices.peak')),
|
|
1318
|
+
')',
|
|
1319
|
+
),
|
|
1320
|
+
),
|
|
1321
|
+
prices.length === 0
|
|
1322
|
+
? null
|
|
1323
|
+
: h(
|
|
1324
|
+
'table',
|
|
1325
|
+
{ className: 'dbf-table' },
|
|
1326
|
+
h(
|
|
1327
|
+
'thead',
|
|
1328
|
+
null,
|
|
1329
|
+
h(
|
|
1330
|
+
'tr',
|
|
1331
|
+
null,
|
|
1332
|
+
h('th', null, ''),
|
|
1333
|
+
h('th', null, t('price.hit')),
|
|
1334
|
+
h('th', null, t('price.miss')),
|
|
1335
|
+
h('th', null, t('price.out')),
|
|
1336
|
+
),
|
|
1337
|
+
),
|
|
1338
|
+
h(
|
|
1339
|
+
'tbody',
|
|
1340
|
+
null,
|
|
1341
|
+
prices.map((row) =>
|
|
1342
|
+
h(
|
|
1343
|
+
'tr',
|
|
1344
|
+
{ key: row.key },
|
|
1345
|
+
h('td', null, modelLabel(row.model)),
|
|
1346
|
+
h('td', null, pair(row.hit, peakActive)),
|
|
1347
|
+
h('td', null, pair(row.miss, peakActive)),
|
|
1348
|
+
h('td', null, pair(row.out, peakActive)),
|
|
1349
|
+
),
|
|
1350
|
+
),
|
|
1351
|
+
),
|
|
1352
|
+
),
|
|
1353
|
+
models.length > 0
|
|
1354
|
+
? h(
|
|
1355
|
+
'div',
|
|
1356
|
+
{ className: 'dbf-sub', style: { marginTop: '6px' } },
|
|
1357
|
+
models.map((row) => modelLabel(row.model) + ' ¥' + money(row.cost)).join(' · '),
|
|
1358
|
+
)
|
|
1359
|
+
: null,
|
|
1360
|
+
);
|
|
1361
|
+
|
|
1362
|
+
const historyBlock = h(
|
|
1363
|
+
'div',
|
|
1364
|
+
{ className: 'dbf-block' },
|
|
1365
|
+
h(
|
|
1366
|
+
'div',
|
|
1367
|
+
{ className: 'dbf-row' },
|
|
1368
|
+
h('span', { className: 'dbf-row-title' }, t('history')),
|
|
1369
|
+
h('span', { className: 'dbf-sub' }, t('history.total', { total: '¥' + money(snapshot?.historyTotal) })),
|
|
1370
|
+
),
|
|
1371
|
+
h(
|
|
1372
|
+
'div',
|
|
1373
|
+
{ className: 'dbf-bars' },
|
|
1374
|
+
history.map((row) => {
|
|
1375
|
+
const value = typeof row.cost === 'number' ? row.cost : 0;
|
|
1376
|
+
const ratio = historyMax > 0 ? Math.max(2, Math.round((value / historyMax) * 38)) : 2;
|
|
1377
|
+
const isToday = typeof snapshot?.today === 'string' && row.day === snapshot.today;
|
|
1378
|
+
return h(
|
|
1379
|
+
'div',
|
|
1380
|
+
{
|
|
1381
|
+
key: row.day,
|
|
1382
|
+
className: 'dbf-bar' + (isToday ? ' is-today' : ''),
|
|
1383
|
+
role: 'button',
|
|
1384
|
+
tabIndex: 0,
|
|
1385
|
+
title: row.day + ' ¥' + money(value),
|
|
1386
|
+
onClick: () => setSelectedDay(row.day),
|
|
1387
|
+
onKeyDown: (event) => {
|
|
1388
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
1389
|
+
event.preventDefault();
|
|
1390
|
+
setSelectedDay(row.day);
|
|
1391
|
+
}
|
|
1392
|
+
},
|
|
1393
|
+
},
|
|
1394
|
+
h('div', { className: 'dbf-bar-fill', style: { height: ratio + 'px' } }),
|
|
1395
|
+
h('div', { className: 'dbf-bar-day' }, row.day.slice(5)),
|
|
1396
|
+
);
|
|
1397
|
+
}),
|
|
1398
|
+
),
|
|
1399
|
+
);
|
|
1400
|
+
|
|
1401
|
+
const selected = selectedDay === null ? null : history.find((row) => row.day === selectedDay) ?? null;
|
|
1402
|
+
const dayBlock =
|
|
1403
|
+
selected === null
|
|
1404
|
+
? null
|
|
1405
|
+
: h(
|
|
1406
|
+
'div',
|
|
1407
|
+
{ className: 'dbf-block' },
|
|
1408
|
+
h(
|
|
1409
|
+
'div',
|
|
1410
|
+
{ className: 'dbf-row' },
|
|
1411
|
+
h(
|
|
1412
|
+
'button',
|
|
1413
|
+
{ type: 'button', className: 'dbf-textbtn', onClick: () => setSelectedDay(null) },
|
|
1414
|
+
t('day.back'),
|
|
1415
|
+
),
|
|
1416
|
+
h('span', { className: 'dbf-sub' }, t('day.title')),
|
|
1417
|
+
),
|
|
1418
|
+
h('div', { className: 'dbf-balance', style: { marginTop: '4px' } }, '¥' + money(selected.cost)),
|
|
1419
|
+
h(
|
|
1420
|
+
'div',
|
|
1421
|
+
{ className: 'dbf-sub' },
|
|
1422
|
+
selected.day + ' ' + t('weekday.' + new Date(selected.day + 'T00:00:00Z').getUTCDay()),
|
|
1423
|
+
),
|
|
1424
|
+
h(
|
|
1425
|
+
'div',
|
|
1426
|
+
{ className: 'dbf-sub', style: { marginTop: '2px' } },
|
|
1427
|
+
t('day.calls', { count: selected.calls ?? 0, tokens: tokens(selected.tokens ?? 0) }),
|
|
1428
|
+
),
|
|
1429
|
+
typeof selected.peakCost === 'number' || typeof selected.valleyCost === 'number'
|
|
1430
|
+
? h(
|
|
1431
|
+
'div',
|
|
1432
|
+
{ className: 'dbf-sub', style: { marginTop: '1px' } },
|
|
1433
|
+
t('today.split', { peak: '¥' + money(selected.peakCost), valley: '¥' + money(selected.valleyCost) }),
|
|
1434
|
+
)
|
|
1435
|
+
: null,
|
|
1436
|
+
h(
|
|
1437
|
+
'div',
|
|
1438
|
+
{ className: 'dbf-token-row', style: { marginTop: '6px', borderTop: '0.5px solid var(--dsw-alias-border-l1)', paddingTop: '5px' } },
|
|
1439
|
+
h('span', { className: 'dbf-sub' }, t('tokens.hit')),
|
|
1440
|
+
h(
|
|
1441
|
+
'span',
|
|
1442
|
+
{ className: 'dbf-num' },
|
|
1443
|
+
(Number(selected.input) || 0) + (Number(selected.cacheRead) || 0) > 0
|
|
1444
|
+
? (((Number(selected.cacheRead) || 0) / ((Number(selected.input) || 0) + (Number(selected.cacheRead) || 0))) * 100).toFixed(1) + '%'
|
|
1445
|
+
: '—',
|
|
1446
|
+
),
|
|
1447
|
+
),
|
|
1448
|
+
h(
|
|
1449
|
+
'div',
|
|
1450
|
+
{ className: 'dbf-token-row' },
|
|
1451
|
+
h('span', { className: 'dbf-sub' }, t('tokens.input')),
|
|
1452
|
+
h('span', { className: 'dbf-num' }, tokens(selected.input ?? 0)),
|
|
1453
|
+
),
|
|
1454
|
+
h('div', { className: 'dbf-token-row' }, h('span', { className: 'dbf-sub' }, t('tokens.cache')), h('span', { className: 'dbf-num' }, tokens(selected.cacheRead ?? 0))),
|
|
1455
|
+
h('div', { className: 'dbf-token-row' }, h('span', { className: 'dbf-sub' }, t('tokens.output')), h('span', { className: 'dbf-num' }, tokens(selected.output ?? 0))),
|
|
1456
|
+
(selected.models ?? []).length === 0
|
|
1457
|
+
? h('div', { className: 'dbf-sub', style: { marginTop: '6px' } }, t('day.empty'))
|
|
1458
|
+
: h(
|
|
1459
|
+
'div',
|
|
1460
|
+
{ className: 'dbf-models' },
|
|
1461
|
+
(selected.models ?? []).map((model) =>
|
|
1462
|
+
h(
|
|
1463
|
+
'div',
|
|
1464
|
+
{ key: model.model, className: 'dbf-model-row' },
|
|
1465
|
+
h('span', { className: 'dbf-model-name', title: model.model }, model.model),
|
|
1466
|
+
h('span', { className: 'dbf-sub' }, tokens(model.tokens)),
|
|
1467
|
+
h('span', { className: 'dbf-num' }, '¥' + money(model.cost)),
|
|
1468
|
+
),
|
|
1469
|
+
),
|
|
1470
|
+
),
|
|
1471
|
+
);
|
|
1472
|
+
|
|
1473
|
+
const footer = h(
|
|
1474
|
+
'div',
|
|
1475
|
+
{ className: 'dbf-foot' },
|
|
1476
|
+
h(
|
|
1477
|
+
'button',
|
|
1478
|
+
{
|
|
1479
|
+
type: 'button',
|
|
1480
|
+
className: 'dbf-version' + (update !== null && update.phase === 'done' && update.hasUpdate ? ' has-update' : ''),
|
|
1481
|
+
title: updateTitle,
|
|
1482
|
+
onClick: () => void checkUpdate(true),
|
|
1483
|
+
},
|
|
1484
|
+
t('version') + ' v' + (snapshot?.version || '—'),
|
|
1485
|
+
h('span', { className: 'dbf-version-glyph', 'aria-hidden': true }, '↻'),
|
|
1486
|
+
),
|
|
1487
|
+
h('span', { className: 'dbf-foot-time' }, typeof snapshot?.source?.updatedAt === 'string' ? t('updated', { time: clock(Date.parse(snapshot.source.updatedAt)) }) : ''),
|
|
1488
|
+
);
|
|
1489
|
+
|
|
1490
|
+
const body = [];
|
|
1491
|
+
if (failure !== '' && snapshot === null) {
|
|
1492
|
+
body.push(
|
|
1493
|
+
h(
|
|
1494
|
+
'div',
|
|
1495
|
+
{ className: 'dbf-block' },
|
|
1496
|
+
h('div', { className: 'dbf-error' }, t('error.fetch') + ': ' + failure),
|
|
1497
|
+
h(
|
|
1498
|
+
'button',
|
|
1499
|
+
{ type: 'button', className: 'dbf-icon', onClick: () => load(true), style: { width: 'auto', padding: '0 8px', marginTop: '6px' } },
|
|
1500
|
+
t('retry'),
|
|
1501
|
+
),
|
|
1502
|
+
),
|
|
1503
|
+
);
|
|
1504
|
+
} else if (snapshot !== null && snapshot.ok === false) {
|
|
1505
|
+
body.push(h('div', { className: 'dbf-block dbf-error' }, t('error.noLedger')));
|
|
1506
|
+
} else if (dayBlock !== null) {
|
|
1507
|
+
body.push(dayBlock);
|
|
1508
|
+
} else {
|
|
1509
|
+
body.push(balanceBlock, peakBlock, tokenBlock, priceTable, historyBlock);
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
card = h(
|
|
1513
|
+
'div',
|
|
1514
|
+
{ key: 'card', className: 'dbf-card dbf-surface' + (leaving ? ' dbf-leaving' : ''), style: cardStyle, role: 'dialog', 'aria-label': t('title') },
|
|
1515
|
+
header,
|
|
1516
|
+
h('div', { className: 'dbf-body' }, body),
|
|
1517
|
+
footer,
|
|
1518
|
+
);
|
|
1519
|
+
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
let settingsPanel = null;
|
|
1523
|
+
if (settingsOpen || leaving) {
|
|
1524
|
+
const settingsRow = (key, label, value, max, unit) => {
|
|
1525
|
+
const percent = (value / max) * 100;
|
|
1526
|
+
return h(
|
|
1527
|
+
'div',
|
|
1528
|
+
{ className: 'dbf-setting' },
|
|
1529
|
+
h(
|
|
1530
|
+
'div',
|
|
1531
|
+
{ className: 'dbf-row' },
|
|
1532
|
+
h('span', { className: 'dbf-label' }, label),
|
|
1533
|
+
h('span', { className: 'dbf-setting-val' }, value + unit),
|
|
1534
|
+
),
|
|
1535
|
+
h(
|
|
1536
|
+
'div',
|
|
1537
|
+
{
|
|
1538
|
+
className: 'dbf-track',
|
|
1539
|
+
role: 'slider',
|
|
1540
|
+
tabIndex: 0,
|
|
1541
|
+
'aria-label': label,
|
|
1542
|
+
'aria-valuemin': 0,
|
|
1543
|
+
'aria-valuemax': max,
|
|
1544
|
+
'aria-valuenow': value,
|
|
1545
|
+
onPointerDown: (event) => {
|
|
1546
|
+
if (event.button === 0) startSlider(key, max, event);
|
|
1547
|
+
},
|
|
1548
|
+
onKeyDown: (event) => {
|
|
1549
|
+
if (event.key === 'ArrowLeft' || event.key === 'ArrowDown') {
|
|
1550
|
+
event.preventDefault();
|
|
1551
|
+
changeAppearance(key, clamp(value - 5, 0, max));
|
|
1552
|
+
} else if (event.key === 'ArrowRight' || event.key === 'ArrowUp') {
|
|
1553
|
+
event.preventDefault();
|
|
1554
|
+
changeAppearance(key, clamp(value + 5, 0, max));
|
|
1555
|
+
}
|
|
1556
|
+
},
|
|
1557
|
+
},
|
|
1558
|
+
h('div', { className: 'dbf-track-rail' }),
|
|
1559
|
+
h('div', { className: 'dbf-track-fill', style: { width: percent + '%' } }),
|
|
1560
|
+
h('div', { className: 'dbf-track-thumb', style: { left: percent + '%' } }),
|
|
1561
|
+
),
|
|
1562
|
+
);
|
|
1563
|
+
};
|
|
1564
|
+
|
|
1565
|
+
settingsPanel = h(
|
|
1566
|
+
'div',
|
|
1567
|
+
{ key: 'settings', className: 'dbf-card dbf-surface' + (leaving ? ' dbf-leaving' : ''), style: cardStyle, role: 'dialog', 'aria-label': t('settings') },
|
|
1568
|
+
h('div', { className: 'dbf-head is-static' }, h('span', { className: 'dbf-head-title' }, t('settings'))),
|
|
1569
|
+
h(
|
|
1570
|
+
'div',
|
|
1571
|
+
{ className: 'dbf-body' },
|
|
1572
|
+
settingsRow('bg', t('settings.bg'), appearance.bg, 90, '%'),
|
|
1573
|
+
settingsRow('border', t('settings.border'), appearance.border, 100, '%'),
|
|
1574
|
+
settingsRow('shadow', t('settings.shadow'), appearance.shadow, 100, '%'),
|
|
1575
|
+
settingsRow('radius', t('settings.radius'), appearance.radius, 20, 'px'),
|
|
1576
|
+
h(
|
|
1577
|
+
'div',
|
|
1578
|
+
{ className: 'dbf-setting' },
|
|
1579
|
+
h(
|
|
1580
|
+
'div',
|
|
1581
|
+
{ className: 'dbf-row' },
|
|
1582
|
+
h('span', { className: 'dbf-label' }, t('settings.dock')),
|
|
1583
|
+
h(
|
|
1584
|
+
'button',
|
|
1585
|
+
{
|
|
1586
|
+
type: 'button',
|
|
1587
|
+
className: 'dbf-switch' + (appearance.dockBall ? ' is-on' : ''),
|
|
1588
|
+
role: 'switch',
|
|
1589
|
+
'aria-checked': appearance.dockBall ? 'true' : 'false',
|
|
1590
|
+
'aria-label': t('settings.dock'),
|
|
1591
|
+
onClick: () => changeAppearance('dockBall', !appearance.dockBall),
|
|
1592
|
+
},
|
|
1593
|
+
h('span', { className: 'dbf-switch-knob' }),
|
|
1594
|
+
),
|
|
1595
|
+
),
|
|
1596
|
+
h('div', { className: 'dbf-sub', style: { marginTop: '2px' } }, t('settings.dock.hint')),
|
|
1597
|
+
),
|
|
1598
|
+
h(
|
|
1599
|
+
'div',
|
|
1600
|
+
{ className: 'dbf-block', style: { display: 'flex', flexDirection: 'column', gap: '8px' } },
|
|
1601
|
+
h('span', { className: 'dbf-sub', style: { marginTop: 0 } }, t('settings.hint')),
|
|
1602
|
+
h(
|
|
1603
|
+
'div',
|
|
1604
|
+
{ style: { display: 'flex', justifyContent: 'flex-end', gap: '8px' } },
|
|
1605
|
+
h('button', { type: 'button', className: 'dbf-textbtn', onClick: () => setAppearance({ ...DEFAULT_APPEARANCE }) }, t('settings.reset')),
|
|
1606
|
+
h(
|
|
1607
|
+
'button',
|
|
1608
|
+
{ type: 'button', className: 'dbf-textbtn', onClick: closeView },
|
|
1609
|
+
t('settings.done'),
|
|
1610
|
+
),
|
|
1611
|
+
),
|
|
1612
|
+
),
|
|
1613
|
+
),
|
|
1614
|
+
);
|
|
1615
|
+
|
|
1616
|
+
}
|
|
1617
|
+
const bubble =
|
|
1618
|
+
!open && !settingsOpen && flash && turn !== null
|
|
1619
|
+
? h(
|
|
1620
|
+
'div',
|
|
1621
|
+
{
|
|
1622
|
+
key: 'bubble',
|
|
1623
|
+
className:
|
|
1624
|
+
'dbf-bubble dbf-surface' +
|
|
1625
|
+
(docked ? (dock === 'left' ? ' is-side is-right-of' : ' is-side is-left-of') : bubbleUp ? ' is-up' : ''),
|
|
1626
|
+
'aria-live': 'polite',
|
|
1627
|
+
},
|
|
1628
|
+
h('span', { className: 'dbf-bubble-caret', 'aria-hidden': true }),
|
|
1629
|
+
h('span', { className: 'dbf-bubble-label' }, t('turn.label')),
|
|
1630
|
+
h('span', { className: 'dbf-bubble-amount' }, '¥' + money(turn.cost)),
|
|
1631
|
+
)
|
|
1632
|
+
: null;
|
|
1633
|
+
|
|
1634
|
+
const updateModal =
|
|
1635
|
+
update === null || !updateOpen
|
|
1636
|
+
? null
|
|
1637
|
+
: h(
|
|
1638
|
+
'div',
|
|
1639
|
+
{
|
|
1640
|
+
className: 'dbf-modal',
|
|
1641
|
+
role: 'dialog',
|
|
1642
|
+
'aria-modal': 'true',
|
|
1643
|
+
'aria-label': t('update.title'),
|
|
1644
|
+
onClick: (event) => {
|
|
1645
|
+
if (event.target === event.currentTarget) setUpdateOpen(false);
|
|
1646
|
+
},
|
|
1647
|
+
},
|
|
1648
|
+
h(
|
|
1649
|
+
'div',
|
|
1650
|
+
{ className: 'dbf-modal-card dbf-surface' },
|
|
1651
|
+
h(
|
|
1652
|
+
'div',
|
|
1653
|
+
{ className: 'dbf-modal-head' },
|
|
1654
|
+
h('span', { className: 'dbf-modal-icon', 'aria-hidden': true }, '⟳'),
|
|
1655
|
+
h('span', { className: 'dbf-modal-title' }, t('update.title')),
|
|
1656
|
+
),
|
|
1657
|
+
update.phase === 'checking'
|
|
1658
|
+
? h('div', { className: 'dbf-modal-body' }, t('update.checking'))
|
|
1659
|
+
: update.phase === 'error'
|
|
1660
|
+
? h(
|
|
1661
|
+
'div',
|
|
1662
|
+
null,
|
|
1663
|
+
h('div', { className: 'dbf-modal-body dbf-error' }, t('update.failed') + ': ' + update.error),
|
|
1664
|
+
h(
|
|
1665
|
+
'div',
|
|
1666
|
+
{ className: 'dbf-modal-actions' },
|
|
1667
|
+
h(
|
|
1668
|
+
'button',
|
|
1669
|
+
{ type: 'button', className: 'dbf-btn-primary', onClick: () => void checkUpdate(true) },
|
|
1670
|
+
t('update.retry'),
|
|
1671
|
+
),
|
|
1672
|
+
),
|
|
1673
|
+
)
|
|
1674
|
+
: update.hasUpdate
|
|
1675
|
+
? h(
|
|
1676
|
+
'div',
|
|
1677
|
+
null,
|
|
1678
|
+
h('div', { className: 'dbf-modal-body' }, t('update.found', { latest: update.latest, current: update.current })),
|
|
1679
|
+
h(
|
|
1680
|
+
'div',
|
|
1681
|
+
{ className: 'dbf-modal-versions' },
|
|
1682
|
+
h(
|
|
1683
|
+
'span',
|
|
1684
|
+
{ className: 'dbf-modal-version' },
|
|
1685
|
+
h('span', { className: 'dbf-modal-version-label' }, t('update.label.current')),
|
|
1686
|
+
h('span', { className: 'dbf-modal-version-value' }, 'v' + (update.current || '—')),
|
|
1687
|
+
),
|
|
1688
|
+
h('span', { className: 'dbf-modal-arrow', 'aria-hidden': true }, '→'),
|
|
1689
|
+
h(
|
|
1690
|
+
'span',
|
|
1691
|
+
{ className: 'dbf-modal-version is-new' },
|
|
1692
|
+
h('span', { className: 'dbf-modal-version-label' }, t('update.label.latest')),
|
|
1693
|
+
h('span', { className: 'dbf-modal-version-value' }, 'v' + update.latest),
|
|
1694
|
+
),
|
|
1695
|
+
),
|
|
1696
|
+
h('div', { className: 'dbf-modal-note' }, t('update.installHint')),
|
|
1697
|
+
h(
|
|
1698
|
+
'div',
|
|
1699
|
+
{ className: 'dbf-modal-actions' },
|
|
1700
|
+
h(
|
|
1701
|
+
'button',
|
|
1702
|
+
{ type: 'button', className: 'dbf-textbtn', onClick: () => setUpdateOpen(false) },
|
|
1703
|
+
t('update.later'),
|
|
1704
|
+
),
|
|
1705
|
+
h(
|
|
1706
|
+
'button',
|
|
1707
|
+
{ type: 'button', className: 'dbf-btn-primary', onClick: () => void copyInstallSpec() },
|
|
1708
|
+
copied ? t('update.copied') : t('update.copy'),
|
|
1709
|
+
),
|
|
1710
|
+
),
|
|
1711
|
+
)
|
|
1712
|
+
: h(
|
|
1713
|
+
'div',
|
|
1714
|
+
null,
|
|
1715
|
+
h('div', { className: 'dbf-modal-body' }, t('update.latest', { current: update.current || snapshot?.version || '—' })),
|
|
1716
|
+
h(
|
|
1717
|
+
'div',
|
|
1718
|
+
{ className: 'dbf-modal-actions' },
|
|
1719
|
+
h(
|
|
1720
|
+
'button',
|
|
1721
|
+
{ type: 'button', className: 'dbf-btn-primary', onClick: () => setUpdateOpen(false) },
|
|
1722
|
+
t('settings.done'),
|
|
1723
|
+
),
|
|
1724
|
+
),
|
|
1725
|
+
),
|
|
1726
|
+
),
|
|
1727
|
+
);
|
|
1728
|
+
|
|
1729
|
+
const surface = settingsOpen ? settingsPanel : open ? card : pill;
|
|
1730
|
+
|
|
1731
|
+
const tree = h(
|
|
1732
|
+
'div',
|
|
1733
|
+
{ ref: rootRef, className: 'dbf-root dbf-dir-' + dir, style: surfaceStyle },
|
|
1734
|
+
h('style', { key: 'css' }, STYLE),
|
|
1735
|
+
surface,
|
|
1736
|
+
bubble,
|
|
1737
|
+
updateModal,
|
|
1738
|
+
);
|
|
1739
|
+
return createPortal !== null && typeof document !== 'undefined' ? createPortal(tree, document.body) : tree;
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
// Only `t` is consumed from the slot props: skip the re-renders the shell
|
|
1743
|
+
// pushes on every streamed token while a session is running.
|
|
1744
|
+
const MemoBalanceFloat = React.memo(BalanceFloat, (prev, next) => prev.t === next.t);
|
|
1745
|
+
|
|
1746
|
+
function apply(ctx) {
|
|
1747
|
+
ctx.effect(() => ctx.locale.register(NS, { zh: ZH, en: EN }), 'balance-float: dictionaries');
|
|
1748
|
+
const t = ctx.locale.bind(NS);
|
|
1749
|
+
ctx.slots.inject('shell.overlay', () =>
|
|
1750
|
+
ctx.slots.register({ name: 'shell.overlay', id: 'balance-float', order: 60, locale: NS }, (props) =>
|
|
1751
|
+
h(MemoBalanceFloat, { ...props, t }),
|
|
1752
|
+
),
|
|
1753
|
+
);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
return {
|
|
1757
|
+
inject: ['slots', 'locale'],
|
|
1758
|
+
apply,
|
|
1759
|
+
};
|
|
1760
|
+
},
|
|
1761
|
+
});
|