diff --git a/sidecars/panel/app.js b/sidecars/panel/app.js index e9a6cde..c903e24 100644 --- a/sidecars/panel/app.js +++ b/sidecars/panel/app.js @@ -1883,6 +1883,13 @@ const I18N_MESSAGES_ZH_CN = Object.freeze({ 'settings.account.passwordRateLimited': '密码连续输错次数过多,请稍候再试。', 'settings.account.vaultDecryptFailed': '凭证解密校验失败,请确认密码或重置凭证库。', 'settings.account.invalidActionNonce': '安全校验令牌已过期,请重试。', + 'settings.account.riskNotAccepted': '请先确认并接受非官方能力风险提示。', + 'settings.account.riskBody': 'Google 官方并未授权在 Antigravity App / IDE 中通过第三方插件导入、保存、切换账号凭证或后台查询多账号配额。擅自启用可能导致 Antigravity 或 GCP 服务被禁用,甚至触发账号封禁;启用该功能即默认由你自行承担全部风险。你最好清楚自己在做什么!', + 'settings.account.trendTitle': '额度变化', + 'settings.account.trendWinAria': '配额窗口切换', + 'settings.account.win5h': '5 小时', + 'settings.account.winWeekly': '每周', + 'settings.account.trendEmpty': '当前时间范围内暂无配额记录。', 'settings.saving': '保存中…', 'settings.saved': '已保存', 'settings.saveFailed': '保存失败', @@ -2862,6 +2869,13 @@ const I18N_MESSAGES_EN_US = Object.freeze({ 'settings.account.passwordRateLimited': 'Too many failed attempts. Wait a moment and try again.', 'settings.account.vaultDecryptFailed': 'Vault decryption check failed.', 'settings.account.invalidActionNonce': 'Security token expired. Please try again.', + 'settings.account.riskNotAccepted': 'Acknowledge the unofficial feature risk warning first.', + 'settings.account.riskBody': 'Google has not authorized importing, storing, or switching Antigravity credentials or polling multi-account quotas in Antigravity App / IDE. Enabling multi-account management may get your Antigravity access, GCP services, or Google account disabled or banned; enabling this feature means you assume all associated risks. Make sure you know what you are doing!', + 'settings.account.trendTitle': 'Quota Trend', + 'settings.account.trendWinAria': 'Quota window selector', + 'settings.account.win5h': '5 Hours', + 'settings.account.winWeekly': 'Weekly', + 'settings.account.trendEmpty': 'No quota history recorded in the selected time range.', 'settings.subTab.aria': 'Preferences category tabs', 'settings.saving': 'Saving…', 'settings.saved': 'Saved', @@ -9802,6 +9816,9 @@ function isEquivalentTelemetry(prev, next) { ) { return false; } + if (Boolean(pSet?.accountSwitchRiskAccepted) !== Boolean(nSet?.accountSwitchRiskAccepted)) { + return false; + } if (!sameArr(pSet?.topbarCapsuleSlots, nSet?.topbarCapsuleSlots)) return false; if (!sameArr(pSet?.hoverModules, nSet?.hoverModules)) return false; if (!sameArr(pSet?.overviewModules, nSet?.overviewModules)) return false; @@ -11066,6 +11083,9 @@ async function saveSettingsPatch(patch) { safePatch.accountQuotaRefreshMinutes ); } + if (Object.prototype.hasOwnProperty.call(safePatch, 'accountSwitchRiskAccepted')) { + safePatch.accountSwitchRiskAccepted = Boolean(safePatch.accountSwitchRiskAccepted); + } if (Object.prototype.hasOwnProperty.call(safePatch, 'topbarCapsuleSlots')) { safePatch.topbarCapsuleSlots = normalizeClientTopbarSlots(safePatch.topbarCapsuleSlots); } @@ -20959,6 +20979,1080 @@ const QuotaPoolsWidget = defineComponent({ return { buildQuotaPoolItems, createPoolQuotaRow, formatQuotaSummaryFooter, syncPoolQuotaRow, quotaPoolRowSig, formatAccountTierBadge, maskAccountLabel, QuotaPoolsWidget }; })(); +// --- src/components/widgets/AccountQuotaTrendCard.js --- +const { resolveQuotaWindowBounds, buildQuotaWindowSeries, AccountQuotaTrendCard } = (() => { +/** + * @fileoverview Per-account quota remaining-% trajectory chart (`AccountQuotaTrendCard.js`). + * Full-bleed edge-to-edge plot (`x = 1 .. viewW - 1`), X-axis clamped to earliest recorded + * sample when history is shorter than the selected preset, and adaptive integer Y-scale so + * real quota movements never compress into a tiny corner with wasted whitespace. + */ + + +const POOL_COLORS = Object.freeze({ + Gemini: 'var(--accent-primary)', + 'Claude & GPT': 'var(--metric-ok)', + Argon: 'var(--ctx-thinking)', + Carbon: 'var(--metric-warn)', +}); + +const POOL_ORDER = ['Gemini', 'Argon', 'Carbon', 'Claude & GPT']; + +let quotaChartUid = 0; + +function floorMinute(ms) { + const n = Number(ms); + if (!(n > 0)) return 0; + const d = new Date(n); + d.setSeconds(0, 0); + return d.getTime(); +} + +function ceilMinute(ms) { + const n = Number(ms); + if (!(n > 0)) return 0; + const d = new Date(n); + d.setSeconds(59, 999); + return d.getTime(); +} + +function pad2(n) { + return String(n).padStart(2, '0'); +} + +function formatAxisTickTime(ms, spanMs) { + const d = new Date(ms); + const hm = `${pad2(d.getHours())}:${pad2(d.getMinutes())}`; + if (spanMs <= 36 * 3600_000) { + return hm; + } + const md = `${d.getMonth() + 1}/${pad2(d.getDate())}`; + if (spanMs <= 7 * 86400_000) { + return `${md} ${pad2(d.getHours())}h`; + } + return md; +} + +function formatHoverTimestamp(ms) { + const d = new Date(ms); + return `${d.getMonth() + 1}/${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`; +} + +function buildHorizontalTangentSpline(pts) { + if (pts.length === 0) return ''; + if (pts.length === 1) return `M ${pts[0].x.toFixed(1)} ${pts[0].y.toFixed(1)}`; + let d = `M ${pts[0].x.toFixed(1)} ${pts[0].y.toFixed(1)}`; + for (let i = 0; i < pts.length - 1; i++) { + const p0 = pts[i]; + const p1 = pts[i + 1]; + const dx = (p1.x - p0.x) * 0.38; + d += ` C ${(p0.x + dx).toFixed(1)} ${p0.y.toFixed(1)}, ${(p1.x - dx).toFixed(1)} ${p1.y.toFixed(1)}, ${p1.x.toFixed(1)} ${p1.y.toFixed(1)}`; + } + return d; +} + +/** + * Computes tight, integer-aligned `[yMin, yMid, yMax]` bounds (`0..100%`) around + * observed `[dataMin, dataMax]` so small-to-moderate quota changes use the full + * vertical canvas instead of leaving 80%+ empty bottom space. + */ +function computeAdaptiveQuotaScale(dataMin, dataMax) { + const lo = Math.max(0, Math.min(100, Number.isFinite(dataMin) ? dataMin : 0)); + const hi = Math.max(lo, Math.min(100, Number.isFinite(dataMax) ? dataMax : 100)); + const rawSpan = hi - lo; + const pad = Math.max(1.5, rawSpan * 0.14); + + let yMax = hi >= 98.5 ? 100 : Math.min(100, Math.ceil((hi + pad) / 10) * 10); + let yMin = lo <= 2 ? 0 : Math.max(0, Math.floor((lo - pad) / 10) * 10); + + if (yMax - yMin < 10) { + if (yMax >= 100) { + yMax = 100; + yMin = 90; + } else if (yMin <= 0) { + yMin = 0; + yMax = 10; + } else { + yMin = Math.max(0, yMax - 10); + } + } + // Ensure `(yMax - yMin)` is even so midpoint tick is always an exact integer + if ((yMax - yMin) % 2 !== 0) { + if (yMin > 0) yMin -= 1; + else if (yMax < 100) yMax += 1; + } + const yMid = Math.round((yMin + yMax) / 2); + return { yMin, yMid, yMax, span: Math.max(1, yMax - yMin) }; +} + +const IMPULSE_GLITCH_MAX_SPAN_MS = 20 * 60_000; +const IMPULSE_GLITCH_MIN_JUMP_PCT = 3; +const IMPULSE_GLITCH_MAX_NEIGHBOR_DIFF_PCT = 1.2; +const CYCLE_RESET_JUMP_MIN_PCT = 3.5; + +function stripIsolatedImpulseSpikes(pts) { + if (pts.length < 3) return pts; + const out = pts.slice(); + let i = 1; + while (i < out.length - 1) { + const prev = out[i - 1]; + const cur = out[i]; + const next = out[i + 1]; + const spanMs = next.ts - prev.ts; + if ( + spanMs >= 0 && + spanMs <= IMPULSE_GLITCH_MAX_SPAN_MS && + cur.val - prev.val >= IMPULSE_GLITCH_MIN_JUMP_PCT && + cur.val - next.val >= IMPULSE_GLITCH_MIN_JUMP_PCT && + Math.abs(next.val - prev.val) <= IMPULSE_GLITCH_MAX_NEIGHBOR_DIFF_PCT + ) { + out.splice(i, 1); + if (i > 1) i -= 1; + continue; + } + i += 1; + } + return out; +} + +function resolveQuotaWindowBounds({ + pools, + selectedRange, + selectedWin, + selectedPool, + customStartMs = 0, + customEndMs = 0, + nowMs = Date.now(), +}) { + if (selectedRange === 'custom') { + return { + startMs: Math.min(customStartMs, customEndMs), + endMs: Math.max(customStartMs, customEndMs), + }; + } + if (selectedRange === 'today') { + const d = new Date(nowMs); + d.setHours(0, 0, 0, 0); + return { startMs: d.getTime(), endMs: nowMs }; + } + if (selectedRange === '24h') return { startMs: nowMs - 24 * 3600_000, endMs: nowMs }; + if (selectedRange === '7d') return { startMs: nowMs - 7 * 86400_000, endMs: nowMs }; + if (selectedRange === '14d') return { startMs: nowMs - 14 * 86400_000, endMs: nowMs }; + if (selectedRange === '30d') return { startMs: nowMs - 30 * 86400_000, endMs: nowMs }; + if (selectedRange === 'all') { + let minTs = Infinity; + for (const p of pools) { + for (const pt of p.points) { + if (pt[0] > 0 && pt[0] < minTs) minTs = pt[0]; + } + } + const safeStart = + Number.isFinite(minTs) && minTs < nowMs - 60_000 ? minTs : nowMs - 5 * 3600_000; + return { startMs: safeStart, endMs: nowMs }; + } + + const targetPools = + selectedPool !== 'all' ? pools.filter((p) => p.poolKey === selectedPool) : pools; + const refPools = targetPools.length ? targetPools : pools; + const isWeekly = selectedWin === 'weekly'; + const winDurationMs = isWeekly ? 7 * 86400_000 : 5 * 3600_000; + + let activeMinResetMs = Infinity; + let anyMinResetMs = Infinity; + for (const p of refPools) { + const rMs = isWeekly ? p.resetWeeklyMs : p.reset5hMs; + const liveVal = isWeekly ? p.liveWk : p.live5h; + if (!(rMs > nowMs)) continue; + if (rMs < anyMinResetMs) anyMinResetMs = rMs; + if (liveVal !== null && liveVal !== undefined && liveVal < 99.95 && rMs < activeMinResetMs) { + activeMinResetMs = rMs; + } + } + const chosenResetMs = Number.isFinite(activeMinResetMs) + ? activeMinResetMs + : Number.isFinite(anyMinResetMs) + ? anyMinResetMs + : 0; + const startMs = chosenResetMs > nowMs ? chosenResetMs - winDurationMs : nowMs - winDurationMs; + return { startMs: Math.min(startMs, nowMs - 60_000), endMs: nowMs }; +} + +/** + * Extracts per-pool trajectories for the requested window, ensuring `'cycle'` mode + * strictly excludes pre-reset points from any previous expired cycle (eliminating + * artificial `84% -> 100%` vertical reset spikes at cycle start). + */ +function buildQuotaWindowSeries({ + visiblePools, + rawBounds, + selectedRange, + selectedWin, + nowMs = Date.now(), +}) { + const isCycleMode = selectedRange === 'cycle'; + const isWeekly = selectedWin === 'weekly'; + const valIdx = isWeekly ? 2 : 1; + const winDurationMs = isWeekly ? 7 * 86400_000 : 5 * 3600_000; + + const staged = []; + let hasStartAnchor = false; + let earliestInWinTs = Infinity; + + for (const p of visiblePools) { + const cleanedAll = []; + for (let i = 0; i < p.points.length; i++) { + const pt = p.points[i]; + const v = pt[valIdx]; + if (v === null || v === undefined || !Number.isFinite(Number(v))) continue; + cleanedAll.push({ + ts: pt[0], + val: Math.max(0, Math.min(100, Number(v))), + }); + } + const sanitized = stripIsolatedImpulseSpikes(cleanedAll); + if (sanitized.length === 0) continue; + + const pResetMs = isWeekly ? p.resetWeeklyMs : p.reset5hMs; + const pLiveVal = isWeekly ? p.liveWk : p.live5h; + const poolCutoffMs = + isCycleMode && pResetMs > nowMs && pLiveVal !== null && pLiveVal < 99.95 + ? Math.max(rawBounds.startMs, pResetMs - winDurationMs) + : rawBounds.startMs; + + let carryBefore = null; + let inWinPts = []; + for (let i = 0; i < sanitized.length; i++) { + const sp = sanitized[i]; + if (sp.ts <= poolCutoffMs) { + carryBefore = sp; + } else if (sp.ts <= rawBounds.endMs) { + inWinPts.push(sp); + } + } + + // In `'cycle'` mode, `< 99.95%` observations at or before `poolCutoffMs` belong + // to the previous expired cycle and must never carry forward into the new cycle. + if (isCycleMode && carryBefore && carryBefore.val < 99.95) { + carryBefore = null; + } + + let hadResetInWindow = false; + if (isCycleMode && inWinPts.length >= 2) { + let lastResetIdx = 0; + for (let k = 1; k < inWinPts.length; k++) { + const prev = inWinPts[k - 1].val; + const cur = inWinPts[k].val; + if (cur - prev >= CYCLE_RESET_JUMP_MIN_PCT || (cur >= 99.5 && prev <= 98.5)) { + lastResetIdx = k; + } + } + if (lastResetIdx > 0) { + inWinPts = inWinPts.slice(lastResetIdx); + carryBefore = null; + hadResetInWindow = true; + } + } + + const isMidWindowReset = hadResetInWindow && inWinPts[0].ts - poolCutoffMs > 5 * 60_000; + const anchorsCycleOrigin = + isCycleMode && + !isMidWindowReset && + !carryBefore && + inWinPts.length > 0 && + inWinPts[0].ts - poolCutoffMs <= 3 * 60_000; + + if (carryBefore || (anchorsCycleOrigin && poolCutoffMs <= rawBounds.startMs + 5000)) { + hasStartAnchor = true; + } + if (inWinPts.length > 0 && inWinPts[0].ts < earliestInWinTs) { + earliestInWinTs = inWinPts[0].ts; + } + + staged.push({ + pool: p, + poolCutoffMs, + carryBefore, + inWinPts, + isMidWindowReset, + anchorsCycleOrigin, + }); + } + + let plottedStartMs = rawBounds.startMs; + if ( + !hasStartAnchor && + selectedRange !== 'custom' && + Number.isFinite(earliestInWinTs) && + earliestInWinTs > rawBounds.startMs + ) { + plottedStartMs = + rawBounds.endMs - earliestInWinTs >= 60_000 + ? earliestInWinTs + : Math.max(rawBounds.startMs, rawBounds.endMs - 5 * 60_000); + } + const plottedEndMs = Math.max(plottedStartMs + 60_000, rawBounds.endMs); + + const rawBuilt = []; + let dataMin = Infinity; + let dataMax = -Infinity; + + for (const item of staged) { + const { pool, poolCutoffMs, carryBefore, inWinPts, isMidWindowReset, anchorsCycleOrigin } = item; + const seriesPts = []; + const anchorStartTs = Math.max(plottedStartMs, poolCutoffMs); + + if (carryBefore) { + seriesPts.push({ ts: anchorStartTs, val: carryBefore.val }); + } else if (anchorsCycleOrigin) { + const originVal = Math.max(100, inWinPts[0].val); + if (inWinPts[0].ts > anchorStartTs) { + seriesPts.push({ ts: anchorStartTs, val: Math.min(100, originVal) }); + } + } else if ( + !isMidWindowReset && + inWinPts.length > 0 && + inWinPts[0].ts > plottedStartMs && + (inWinPts.length === 1 || inWinPts[0].val >= 99.95) + ) { + seriesPts.push({ ts: plottedStartMs, val: inWinPts[0].val }); + } + + for (let i = 0; i < inWinPts.length; i++) { + const vp = inWinPts[i]; + if (vp.ts >= plottedStartMs) { + seriesPts.push(vp); + } + } + + if (seriesPts.length > 0 && plottedEndMs <= nowMs + 60_000) { + const tail = seriesPts[seriesPts.length - 1]; + const tailEnd = Math.min(plottedEndMs, nowMs); + if (tailEnd > tail.ts + 1000) { + seriesPts.push({ ts: tailEnd, val: tail.val }); + } + } + if (seriesPts.length === 0) continue; + + for (let i = 0; i < seriesPts.length; i++) { + const v = seriesPts[i].val; + if (v < dataMin) dataMin = v; + if (v > dataMax) dataMax = v; + } + rawBuilt.push({ + poolKey: pool.poolKey, + label: pool.label, + color: POOL_COLORS[pool.poolKey] || 'var(--accent-primary)', + rawPoints: seriesPts, + latestVal: seriesPts[seriesPts.length - 1].val, + }); + } + + return { + plottedStartMs, + plottedEndMs, + rawBuilt, + yScale: rawBuilt.length > 0 ? computeAdaptiveQuotaScale(dataMin, dataMax) : null, + }; +} + +function getTimeRangeOptions() { + return [ + { value: 'cycle', label: t('cons.rangeCycle') }, + { value: 'today', label: t('cons.rangeToday') }, + { value: '24h', label: t('cons.range24h') }, + { value: '7d', label: t('cons.range7d') }, + { value: '14d', label: t('cons.range14d') }, + { value: '30d', label: t('cons.range30d') }, + { value: 'all', label: t('cons.rangeAll') }, + { value: 'custom', label: t('cons.rangeCustom') }, + ]; +} + +function getQuotaWindowOptions() { + return [ + { value: '5h', label: t('settings.account.win5h') }, + { value: 'weekly', label: t('settings.account.winWeekly') }, + ]; +} + +const AccountQuotaTrendCard = defineComponent({ + name: 'AccountQuotaTrendCard', + setup({ onCleanup }) { + const VIEW_H = 184; + const TOP_Y = 18; + const BOTTOM_Y = 166; + const SPAN_Y = BOTTOM_Y - TOP_Y; + const LEFT_X = 1; + const RIGHT_PAD = 1; + + let viewW = 460; + let selectedRange = 'cycle'; + let selectedWin = '5h'; + let selectedPool = 'all'; + let selectedAccountEmail = ''; + + const initNow = Date.now(); + let customStartMs = floorMinute(initNow - 24 * 3600_000); + let customEndMs = ceilMinute(initNow); + let draftStartMs = customStartMs; + let draftEndMs = customEndMs; + + let latestState = { + activeEmail: '', + activePools: [], + riskAccepted: false, + accounts: [], + savedQuotas: new Map(), + quotaHistory: {}, + }; + + let plottedSeries = []; + let plottedStartMs = 0; + let plottedEndMs = 0; + let plottedYScale = { yMin: 0, yMid: 50, yMax: 100, span: 100 }; + + const cardTitleText = h('span', { text: t('settings.account.trendTitle') }); + const spanBadgeEl = h('span', { className: 'cons-badge text-mono', text: '' }); + + const accountSelect = CustomSelect({ + className: 'cons-cycle-pool-select account-trend-email-select', + modelValue: '', + options: [], + onUpdateModelValue: (val) => { + selectedAccountEmail = String(val || '').trim().toLowerCase(); + renderChart(); + }, + }); + patchAttr(accountSelect.el, 'hidden', true); + + const poolSelect = CustomSelect({ + className: 'cons-cycle-pool-select', + modelValue: 'all', + options: [{ value: 'all', label: t('cons.cyclePoolAll') }], + onUpdateModelValue: (val) => { + selectedPool = val || 'all'; + renderChart(); + }, + }); + + const winSwitch = SegmentedControl({ + ariaLabel: t('settings.account.trendWinAria'), + modelValue: selectedWin, + options: getQuotaWindowOptions(), + onUpdateModelValue: (val) => { + selectedWin = val === 'weekly' ? 'weekly' : '5h'; + winSwitch.setProps({ modelValue: selectedWin, options: getQuotaWindowOptions() }); + renderChart(); + }, + }); + + const rangeSwitch = SegmentedControl({ + ariaLabel: t('cons.rangeAria'), + modelValue: selectedRange, + options: getTimeRangeOptions(), + onUpdateModelValue: (nextRange) => { + selectedRange = nextRange || 'cycle'; + if (selectedRange === 'custom') { + startPicker.setValue(draftStartMs); + endPicker.setValue(draftEndMs); + } + rangeSwitch.setProps({ modelValue: selectedRange, options: getTimeRangeOptions() }); + renderChart(); + }, + }); + + const startPicker = CustomDateTimePicker({ + modelValue: draftStartMs, + onUpdateModelValue: (ms) => { + draftStartMs = ms; + startPicker.setValue(ms); + }, + }); + const endPicker = CustomDateTimePicker({ + modelValue: draftEndMs, + onUpdateModelValue: (ms) => { + draftEndMs = ms; + endPicker.setValue(ms); + }, + }); + const applyCustomLabel = h('span', { text: t('cons.applyFilter') }); + const applyCustomBtn = h( + 'button', + { + type: 'button', + className: 'btn-primary', + onClick: () => { + const s = Number(draftStartMs || 0); + const e = Number(draftEndMs || 0); + const lo = floorMinute(s > 0 && e > 0 ? Math.min(s, e) : s); + const hi = ceilMinute(s > 0 && e > 0 ? Math.max(s, e) : e); + if (lo > 0 && hi > lo) { + customStartMs = lo; + customEndMs = hi; + draftStartMs = lo; + draftEndMs = hi; + startPicker.setValue(lo); + endPicker.setValue(hi); + renderChart(); + } + }, + }, + [createSvgIcon('check', { size: 12 }), applyCustomLabel] + ); + const rangeToEl = h('span', { className: 'text-muted', text: t('cons.rangeTo') }); + const customRangeBar = h('div', { className: 'cons-custom-range-bar', hidden: true }, [ + startPicker.el, + rangeToEl, + endPicker.el, + applyCustomBtn, + ]); + + // --- Full-Bleed Edge-to-Edge 184px SVG Stage (reusing TrendAreaChart `.trend-svg`) --- + const uid = ++quotaChartUid; + const gradId = `acct-quota-grad-${uid}`; + const svg = document.createElementNS(SVG_NS, 'svg'); + svg.setAttribute('viewBox', `0 0 ${viewW} ${VIEW_H}`); + svg.setAttribute('class', 'trend-svg'); + svg.setAttribute('aria-hidden', 'true'); + + const defs = document.createElementNS(SVG_NS, 'defs'); + const grad = document.createElementNS(SVG_NS, 'linearGradient'); + grad.setAttribute('id', gradId); + grad.setAttribute('x1', '0'); + grad.setAttribute('y1', '0'); + grad.setAttribute('x2', '0'); + grad.setAttribute('y2', '1'); + const stopTop = document.createElementNS(SVG_NS, 'stop'); + stopTop.setAttribute('offset', '0%'); + stopTop.setAttribute('stop-color', 'var(--accent-primary)'); + stopTop.setAttribute('stop-opacity', '0.24'); + const stopBot = document.createElementNS(SVG_NS, 'stop'); + stopBot.setAttribute('offset', '100%'); + stopBot.setAttribute('stop-color', 'var(--accent-primary)'); + stopBot.setAttribute('stop-opacity', '0.0'); + grad.appendChild(stopTop); + grad.appendChild(stopBot); + defs.appendChild(grad); + svg.appendChild(defs); + + const gridGroup = document.createElementNS(SVG_NS, 'g'); + gridGroup.setAttribute('class', 'trend-grid'); + const gridRowSpecs = [ + { y: TOP_Y, dy: 3, labeled: true }, + { y: Math.round(TOP_Y + SPAN_Y * 0.25), dy: 0, labeled: false }, + { y: Math.round(TOP_Y + SPAN_Y * 0.5), dy: 3, labeled: true }, + { y: Math.round(TOP_Y + SPAN_Y * 0.75), dy: 0, labeled: false }, + { y: BOTTOM_Y, dy: -3, labeled: true }, + ]; + const gridLines = []; + const yLabels = []; + for (const row of gridRowSpecs) { + const line = document.createElementNS(SVG_NS, 'line'); + line.setAttribute('x1', '0'); + line.setAttribute('y1', String(row.y)); + line.setAttribute('x2', String(viewW)); + line.setAttribute('y2', String(row.y)); + gridGroup.appendChild(line); + gridLines.push(line); + + if (row.labeled) { + const lbl = document.createElementNS(SVG_NS, 'text'); + lbl.setAttribute('class', 'sstep-axis-label is-y-inline'); + lbl.setAttribute('x', '3'); + lbl.setAttribute('y', String(row.y + row.dy)); + lbl.setAttribute('text-anchor', 'start'); + yLabels.push(lbl); + } + } + svg.appendChild(gridGroup); + + const seriesGroup = document.createElementNS(SVG_NS, 'g'); + svg.appendChild(seriesGroup); + + // Append inline Y labels above series fills so halo stroke keeps text crisp + const yLabelOverlay = document.createElementNS(SVG_NS, 'g'); + for (const lbl of yLabels) { + yLabelOverlay.appendChild(lbl); + } + svg.appendChild(yLabelOverlay); + + const hoverLine = document.createElementNS(SVG_NS, 'line'); + hoverLine.setAttribute('class', 'trend-hover-line'); + hoverLine.setAttribute('y1', String(TOP_Y)); + hoverLine.setAttribute('y2', String(BOTTOM_Y)); + hoverLine.setAttribute('hidden', ''); + svg.appendChild(hoverLine); + + const hoverDotsGroup = document.createElementNS(SVG_NS, 'g'); + hoverDotsGroup.setAttribute('hidden', ''); + svg.appendChild(hoverDotsGroup); + + const tipTimeEl = h('span', { className: 'trend-tip-time text-mono' }); + const tipWinEl = h('span', { className: 'trend-tip-calls' }); + const tipRowsHost = h('div', { className: 'account-trend-tip-rows' }); + const tooltipEl = h('div', { className: 'trend-tooltip', hidden: true }, [ + h('div', { className: 'trend-tip-head' }, [tipTimeEl, tipWinEl]), + tipRowsHost, + ]); + + const xAxisEl = h('div', { className: 'trend-x-axis' }); + const legendRowEl = h('div', { className: 'account-trend-legend' }); + const emptyStateEl = h('div', { className: 'smodel-empty settings-action-desc', hidden: true }); + + const plotStage = h( + 'div', + { + className: 'trend-plot-stage', + onPointermove: handlePointerMove, + onPointerleave: hideHover, + }, + [svg, tooltipEl] + ); + const chartWrap = h('div', { className: 'trend-chart-wrap' }, [ + plotStage, + xAxisEl, + legendRowEl, + ]); + + const rootEl = h('section', { className: 'panel account-quota-trend-panel' }, [ + h('div', { className: 'panel-header account-trend-toprow' }, [ + h('span', { className: 'panel-title cons-overview-title' }, [ + createSvgIcon('pulse', { size: 13 }), + cardTitleText, + accountSelect.el, + poolSelect.el, + ]), + winSwitch.el, + ]), + h('div', { className: 'panel-header cons-overview-head account-trend-subbar' }, [ + spanBadgeEl, + h('div', { className: 'cons-overview-controls' }, [rangeSwitch.el]), + ]), + customRangeBar, + chartWrap, + emptyStateEl, + ]); + + function valueToSvgY(val) { + const clamped = Math.max(plottedYScale.yMin, Math.min(plottedYScale.yMax, val)); + return BOTTOM_Y - ((clamped - plottedYScale.yMin) / plottedYScale.span) * SPAN_Y; + } + + function hideHover() { + hoverLine.setAttribute('hidden', ''); + hoverDotsGroup.setAttribute('hidden', ''); + patchAttr(tooltipEl, 'hidden', true); + } + + function sampleSeriesValueAt(points, targetMs) { + if (!points.length) return null; + if (targetMs < points[0].ts - 1000) return null; + if (targetMs <= points[0].ts) return points[0].val; + const last = points[points.length - 1]; + if (targetMs > last.ts + 60_000) return null; + if (targetMs >= last.ts) return last.val; + let lo = 0; + let hi = points.length - 1; + while (lo + 1 < hi) { + const mid = (lo + hi) >> 1; + if (points[mid].ts <= targetMs) lo = mid; + else hi = mid; + } + const a = points[lo]; + const b = points[hi]; + const span = b.ts - a.ts; + if (span <= 0) return b.val; + return a.val + (b.val - a.val) * ((targetMs - a.ts) / span); + } + + function handlePointerMove(ev) { + if (!plottedSeries.length || !(plottedEndMs > plottedStartMs)) { + hideHover(); + return; + } + const rect = svg.getBoundingClientRect?.(); + const widthPx = Number(rect?.width || viewW); + if (!(widthPx > 0)) return; + const offsetX = Number(ev.clientX || 0) - Number(rect?.left || 0); + const svgX = (offsetX / widthPx) * viewW; + const plotW = Math.max(20, viewW - LEFT_X - RIGHT_PAD); + const u = Math.max(0, Math.min(1, (svgX - LEFT_X) / plotW)); + const clampedX = LEFT_X + u * plotW; + const targetMs = Math.round(plottedStartMs + u * (plottedEndMs - plottedStartMs)); + + hoverLine.setAttribute('x1', clampedX.toFixed(1)); + hoverLine.setAttribute('x2', clampedX.toFixed(1)); + + while (hoverDotsGroup.firstChild) hoverDotsGroup.removeChild(hoverDotsGroup.firstChild); + const tipItems = []; + for (let i = 0; i < plottedSeries.length; i++) { + const s = plottedSeries[i]; + const val = sampleSeriesValueAt(s.rawPoints, targetMs); + if (val === null) continue; + const y = valueToSvgY(val); + const dot = document.createElementNS(SVG_NS, 'circle'); + dot.setAttribute('cx', clampedX.toFixed(1)); + dot.setAttribute('cy', y.toFixed(1)); + dot.setAttribute('r', '3.2'); + dot.setAttribute('fill', 'var(--surface-panel)'); + dot.setAttribute('stroke', s.color); + dot.setAttribute('stroke-width', '1.8'); + hoverDotsGroup.appendChild(dot); + tipItems.push({ key: s.poolKey, label: s.label, color: s.color, val }); + } + if (tipItems.length === 0) { + hideHover(); + return; + } + hoverLine.removeAttribute('hidden'); + hoverDotsGroup.removeAttribute('hidden'); + + patchText(tipTimeEl, formatHoverTimestamp(targetMs)); + patchText(tipWinEl, t(selectedWin === 'weekly' ? 'settings.account.winWeekly' : 'settings.account.win5h')); + patchKeyedList(tipRowsHost, tipItems, { + keyFn: (it) => it.key, + sigFn: (it) => `${it.label}:${it.val.toFixed(1)}`, + createRow: () => { + const sw = h('span', { className: 'dot-swatch' }); + const nm = h('span', { className: 'account-trend-tip-name' }); + const vl = h('strong', { className: 'text-mono' }); + const row = h('div', { className: 'account-trend-tip-row' }, [ + h('span', { className: 'legend-left' }, [sw, nm]), + vl, + ]); + return { el: row, refs: { sw, nm, vl } }; + }, + updateRow: (r, it) => { + r.sw.style.background = it.color; + patchText(r.nm, it.label); + patchText(r.vl, `${+it.val.toFixed(1)}%`); + }, + }); + + const pctX = (clampedX / viewW) * 100; + const shift = pctX < 24 ? '0%' : pctX > 76 ? '-100%' : '-50%'; + patchStyleVar(tooltipEl, '--tip-x', `${pctX.toFixed(1)}%`); + patchStyleVar(tooltipEl, '--tip-shift', shift); + patchAttr(tooltipEl, 'hidden', false); + } + + function resolveTargetAccountEmail() { + const { activeEmail, riskAccepted, accounts } = latestState; + if (!riskAccepted) { + return activeEmail || ''; + } + const candidates = []; + if (activeEmail) candidates.push(activeEmail); + for (const addr of accounts) { + if (addr && !candidates.includes(addr)) candidates.push(addr); + } + if (selectedAccountEmail && candidates.includes(selectedAccountEmail)) { + return selectedAccountEmail; + } + selectedAccountEmail = candidates[0] || ''; + return selectedAccountEmail; + } + + function buildMergedPoolsForEmail(email, nowMs) { + const histPools = latestState.quotaHistory?.[email]?.pools || {}; + const livePools = + email && email === latestState.activeEmail + ? latestState.activePools || [] + : latestState.savedQuotas.get(email)?.pools || []; + + const merged = new Map(); + for (const [pk, hEntry] of Object.entries(histPools)) { + merged.set(pk, { + poolKey: pk, + label: hEntry.label || pk, + reset5hMs: Number(hEntry.reset5hMs) || 0, + resetWeeklyMs: Number(hEntry.resetWeeklyMs) || 0, + points: Array.isArray(hEntry.points) ? hEntry.points.slice() : [], + live5h: null, + liveWk: null, + }); + } + + for (const lp of livePools) { + const pk = String(lp?.poolKey || lp?.family || lp?.poolLabel || '').trim(); + if (!pk || pk === 'Unknown') continue; + let item = merged.get(pk); + if (!item) { + item = { + poolKey: pk, + label: String(lp?.poolLabel || pk), + reset5hMs: 0, + resetWeeklyMs: 0, + points: [], + live5h: null, + liveWk: null, + }; + merged.set(pk, item); + } + if (lp?.poolLabel) item.label = String(lp.poolLabel); + const r5Ms = Date.parse(lp?.resetTime || ''); + const rWMs = Date.parse(lp?.weeklyResetTime || ''); + if (Number.isFinite(r5Ms) && r5Ms > 0) item.reset5hMs = r5Ms; + if (Number.isFinite(rWMs) && rWMs > 0) item.resetWeeklyMs = rWMs; + if (Number.isFinite(lp?.remainingPct)) item.live5h = +Number(lp.remainingPct).toFixed(1); + if (Number.isFinite(lp?.weeklyRemainingPct)) { + item.liveWk = +Number(lp.weeklyRemainingPct).toFixed(1); + } + if (item.live5h !== null || item.liveWk !== null) { + const last = item.points[item.points.length - 1]; + if (!last || nowMs >= last[0]) { + item.points.push([ + nowMs, + item.live5h ?? (last ? last[1] : null), + item.liveWk ?? (last ? last[2] : null), + ]); + } + } + } + + return Array.from(merged.values()).sort( + (a, b) => + (POOL_ORDER.indexOf(a.poolKey) === -1 ? 99 : POOL_ORDER.indexOf(a.poolKey)) - + (POOL_ORDER.indexOf(b.poolKey) === -1 ? 99 : POOL_ORDER.indexOf(b.poolKey)) + ); + } + + function renderChart() { + hideHover(); + const nowMs = Date.now(); + void getEffectiveLocale(); + + patchText(cardTitleText, t('settings.account.trendTitle')); + patchText(rangeToEl, t('cons.rangeTo')); + patchText(applyCustomLabel, t('cons.applyFilter')); + winSwitch.setProps({ modelValue: selectedWin, options: getQuotaWindowOptions() }); + rangeSwitch.setProps({ modelValue: selectedRange, options: getTimeRangeOptions() }); + patchAttr(customRangeBar, 'hidden', selectedRange !== 'custom'); + + const targetEmail = resolveTargetAccountEmail(); + const candidateEmails = []; + if (latestState.activeEmail) candidateEmails.push(latestState.activeEmail); + if (latestState.riskAccepted) { + for (const addr of latestState.accounts) { + if (addr && !candidateEmails.includes(addr)) candidateEmails.push(addr); + } + } + const showAccountPicker = latestState.riskAccepted && candidateEmails.length > 1; + patchAttr(accountSelect.el, 'hidden', !showAccountPicker); + if (showAccountPicker) { + accountSelect.setProps({ + modelValue: targetEmail, + options: candidateEmails.map((addr) => ({ + value: addr, + label: addr.split('@')[0] || addr, + title: addr, + })), + }); + } + + const mergedPools = targetEmail ? buildMergedPoolsForEmail(targetEmail, nowMs) : []; + if (selectedPool !== 'all' && !mergedPools.some((p) => p.poolKey === selectedPool)) { + selectedPool = 'all'; + } + const poolOpts = [ + { + value: 'all', + label: t('cons.cyclePoolAll'), + }, + ...mergedPools.map((p) => ({ + value: p.poolKey, + label: p.poolKey === 'Claude & GPT' ? 'Claude/GPT' : p.label, + title: p.label, + })), + ]; + poolSelect.setProps({ modelValue: selectedPool, options: poolOpts }); + patchAttr(poolSelect.el, 'hidden', mergedPools.length <= 1); + + const rawBounds = resolveQuotaWindowBounds({ + pools: mergedPools, + selectedRange, + selectedWin, + selectedPool, + customStartMs, + customEndMs, + nowMs, + }); + const visiblePools = + selectedPool === 'all' + ? mergedPools + : mergedPools.filter((p) => p.poolKey === selectedPool); + + const built = buildQuotaWindowSeries({ + visiblePools, + rawBounds, + selectedRange, + selectedWin, + nowMs, + }); + plottedStartMs = built.plottedStartMs; + plottedEndMs = built.plottedEndMs; + const spanMs = plottedEndMs - plottedStartMs; + patchText(spanBadgeEl, formatDateSpanBadge(plottedStartMs, plottedEndMs)); + + const rawBuilt = built.rawBuilt; + const hasPlot = rawBuilt.length > 0; + patchAttr(chartWrap, 'hidden', !hasPlot); + patchAttr(emptyStateEl, 'hidden', hasPlot); + if (!hasPlot) { + plottedSeries = []; + patchText(emptyStateEl, t('settings.account.trendEmpty')); + return; + } + + plottedYScale = built.yScale; + yLabels[0].textContent = `${plottedYScale.yMax}%`; + yLabels[1].textContent = `${plottedYScale.yMid}%`; + yLabels[2].textContent = `${plottedYScale.yMin}%`; + + const plotW = Math.max(20, viewW - LEFT_X - RIGHT_PAD); + svg.setAttribute('viewBox', `0 0 ${viewW} ${VIEW_H}`); + for (const gl of gridLines) { + gl.setAttribute('x2', String(viewW)); + } + + plottedSeries = rawBuilt.map((rb) => ({ + ...rb, + svgPts: rb.rawPoints.map((sp) => { + const u = Math.max(0, Math.min(1, (sp.ts - plottedStartMs) / spanMs)); + return { + x: LEFT_X + u * plotW, + y: valueToSvgY(sp.val), + val: sp.val, + ts: sp.ts, + }; + }), + })); + + while (seriesGroup.firstChild) seriesGroup.removeChild(seriesGroup.firstChild); + for (let i = 0; i < plottedSeries.length; i++) { + const s = plottedSeries[i]; + const lineD = buildHorizontalTangentSpline(s.svgPts); + if (s.svgPts.length >= 2) { + const first = s.svgPts[0]; + const last = s.svgPts[s.svgPts.length - 1]; + const areaEl = document.createElementNS(SVG_NS, 'path'); + areaEl.setAttribute( + 'd', + `${lineD} L ${last.x.toFixed(1)} ${BOTTOM_Y} L ${first.x.toFixed(1)} ${BOTTOM_Y} Z` + ); + if (plottedSeries.length === 1) { + stopTop.setAttribute('stop-color', s.color); + stopBot.setAttribute('stop-color', s.color); + areaEl.setAttribute('fill', `url(#${gradId})`); + } else { + areaEl.setAttribute('class', 'sstep-multi-area'); + areaEl.style.setProperty('--sstep-series-color', s.color); + } + seriesGroup.appendChild(areaEl); + } + const pathEl = document.createElementNS(SVG_NS, 'path'); + pathEl.setAttribute('d', lineD); + pathEl.setAttribute('fill', 'none'); + pathEl.setAttribute('stroke', s.color); + pathEl.setAttribute('stroke-width', '1.9'); + pathEl.setAttribute('stroke-linecap', 'round'); + seriesGroup.appendChild(pathEl); + + const endPt = s.svgPts[s.svgPts.length - 1]; + const endDot = document.createElementNS(SVG_NS, 'circle'); + endDot.setAttribute('cx', endPt.x.toFixed(1)); + endDot.setAttribute('cy', endPt.y.toFixed(1)); + endDot.setAttribute('r', '2.7'); + endDot.setAttribute('fill', s.color); + seriesGroup.appendChild(endDot); + } + + // Edge-to-edge X-axis ticks (`0% .. 100%`) + const tickCount = 5; + const ticks = []; + for (let k = 0; k < tickCount; k++) { + const ratio = k / (tickCount - 1); + const tickMs = Math.round(plottedStartMs + ratio * spanMs); + const leftPct = ratio * 100; + ticks.push({ + idx: k, + leftPct, + label: formatAxisTickTime(tickMs, spanMs), + edge: k === 0 ? 'start' : k === tickCount - 1 ? 'end' : '', + }); + } + patchKeyedList(xAxisEl, ticks, { + keyFn: (tk) => tk.idx, + sigFn: (tk) => `${tk.leftPct.toFixed(1)}:${tk.label}:${tk.edge}`, + createRow: () => { + const el = h('span', { className: 'trend-x-tick text-mono' }); + return { el, refs: { el } }; + }, + updateRow: (r, tk) => { + patchText(r.el, tk.label); + patchStyleVar(r.el, '--tick-x', `${tk.leftPct.toFixed(2)}%`); + patchClasses(r.el, { + 'is-start': tk.edge === 'start', + 'is-end': tk.edge === 'end', + }); + }, + }); + + patchKeyedList(legendRowEl, plottedSeries, { + keyFn: (s) => s.poolKey, + sigFn: (s) => `${s.label}:${s.latestVal.toFixed(1)}:${s.color}`, + createRow: () => { + const sw = h('span', { className: 'dot-swatch' }); + const nm = h('span', { className: 'account-trend-leg-name' }); + const vl = h('strong', { className: 'text-mono' }); + const el = h('span', { className: 'account-trend-leg-item' }, [sw, nm, vl]); + return { el, refs: { sw, nm, vl } }; + }, + updateRow: (r, s) => { + r.sw.style.background = s.color; + patchText(r.nm, s.label); + patchText(r.vl, `${+s.latestVal.toFixed(1)}%`); + }, + }); + } + + let resizeObs = null; + if (typeof ResizeObserver === 'function') { + resizeObs = new ResizeObserver((entries) => { + const w = Math.floor(Number(entries?.[0]?.contentRect?.width || 0)); + if (w >= 180 && Math.abs(w - viewW) >= 2) { + viewW = w; + renderChart(); + } + }); + resizeObs.observe(plotStage); + } + + onCleanup(() => { + resizeObs?.disconnect(); + accountSelect.destroy(); + poolSelect.destroy(); + winSwitch.destroy(); + rangeSwitch.destroy(); + startPicker.destroy(); + endPicker.destroy(); + }); + + function update(nextState) { + latestState = { ...latestState, ...nextState }; + renderChart(); + } + + renderChart(); + return { + el: rootEl, + refs: { update, refreshLabels: renderChart }, + }; + }, +}); +return { resolveQuotaWindowBounds, buildQuotaWindowSeries, AccountQuotaTrendCard }; +})(); + // --- src/components/widgets/LiveActivityWidget.js --- const { buildFallbackRunningConversations, LiveActivityWidget } = (() => { /** @@ -29339,7 +30433,11 @@ return { SettingsStructureTab }; // --- src/views/settings/SettingsAccountTab.js --- const { SettingsAccountTab } = (() => { -/** @fileoverview Unified multi-account switcher, passphrase vault UI, and per-pool quota cards. */ +/** + * @fileoverview Multi-account switcher, quota trajectory chart, risk gate, and passphrase vault UI. + * Reuses `SegmentedControl`, `CustomSelect`, `AccountQuotaTrendCard`, `.ctx-risk-mask`, + * `.settings-bridge-notice`, `.status-tag`, `.btn-ghost-sm`, `.btn-danger-confirm`, and `.quota-pool-row`. + */ const key = (name) => `settings.account.${name}`; @@ -29355,7 +30453,7 @@ const errors = new Set([ 'switchStartFailed', 'clientCloseFailed', 'clientRestartFailed', 'loginStartFailed', 'loginExchangeFailed', 'loginExpired', 'loginDenied', 'vaultLocked', 'wrongPassword', 'invalidPassword', 'passwordMismatch', - 'passwordRateLimited', 'vaultDecryptFailed', 'invalidActionNonce', + 'passwordRateLimited', 'vaultDecryptFailed', 'invalidActionNonce', 'riskNotAccepted', ]); const okToastKeys = new Set([ @@ -29376,6 +30474,13 @@ function getAccountRefreshOptions() { ]; } +function getPwdModeOptions() { + return [ + { value: 'change', label: t(key('pwdTabChange')) }, + { value: 'disable', label: t(key('pwdTabDisable')) }, + ]; +} + function formatValidCountdown(isoTime) { if (!isoTime) return ''; const ms = Date.parse(isoTime); @@ -29405,8 +30510,9 @@ function renderQuotaRows(container, quotaPools) { ); patchKeyedList(container, pools, { keyFn: (pool) => pool.poolKey || pool.poolLabel, - sigFn: (pool) => + sigFn: (pool, idx) => JSON.stringify([ + idx > 0, locale, pool.poolLabel, pool.remainingPct, @@ -29415,32 +30521,39 @@ function renderQuotaRows(container, quotaPools) { pool.weeklyResetTime, ]), createRow: () => { - const poolTitle = h('div', { className: 'account-pool-name' }); + const poolTitle = h('div', { className: 'account-pool-label' }); - const label5h = h('span', { className: 'settings-account-quota text-mono' }); - const reset5h = h('span', { className: 'account-meter-reset text-mono', hidden: true }); + const label5h = h('span', { className: 'quota-win-title settings-account-quota text-mono' }); + const reset5h = h('span', { className: 'panel-meta quota-win-meta text-mono', hidden: true }); const fill5h = h('div', { className: 'progress-fill' }); const meter5h = h('div', { className: 'account-meter-col' }, [ - h('div', { className: 'account-meter-head' }, [label5h, reset5h]), - h('div', { className: 'progress-track account-progress-track' }, [fill5h]), + h('div', { className: 'panel-header quota-win-head' }, [ + h('span', { className: 'quota-win-left' }, [label5h]), + reset5h, + ]), + h('div', { className: 'progress-track' }, [fill5h]), ]); - const labelWk = h('span', { className: 'settings-account-quota text-mono' }); - const resetWk = h('span', { className: 'account-meter-reset text-mono', hidden: true }); + const labelWk = h('span', { className: 'quota-win-title settings-account-quota text-mono' }); + const resetWk = h('span', { className: 'panel-meta quota-win-meta text-mono', hidden: true }); const fillWk = h('div', { className: 'progress-fill' }); const meterWk = h('div', { className: 'account-meter-col' }, [ - h('div', { className: 'account-meter-head' }, [labelWk, resetWk]), - h('div', { className: 'progress-track account-progress-track' }, [fillWk]), + h('div', { className: 'panel-header quota-win-head' }, [ + h('span', { className: 'quota-win-left' }, [labelWk]), + resetWk, + ]), + h('div', { className: 'progress-track' }, [fillWk]), ]); const gridEl = h('div', { className: 'account-pool-meters' }, [meter5h, meterWk]); - const el = h('div', { className: 'account-pool-group' }, [poolTitle, gridEl]); + const el = h('div', { className: 'quota-pool-row' }, [poolTitle, gridEl]); return { el, - refs: { poolTitle, gridEl, meter5h, label5h, reset5h, fill5h, meterWk, labelWk, resetWk, fillWk }, + refs: { el, poolTitle, gridEl, meter5h, label5h, reset5h, fill5h, meterWk, labelWk, resetWk, fillWk }, }; }, - updateRow: (refs, pool) => { + updateRow: (refs, pool, idx) => { + patchClasses(refs.el, { 'has-divider': idx > 0 }); patchText(refs.poolTitle, pool.poolLabel || pool.poolKey); const has5h = Number.isFinite(pool.remainingPct); const hasWk = Number.isFinite(pool.weeklyRemainingPct); @@ -29485,18 +30598,22 @@ function isValidVaultPassphrase(raw) { const SettingsAccountTab = defineComponent({ name: 'SettingsAccountTab', setup({ props, onCleanup }) { + const initialSettings = telemetryStore.getState().telemetry?.settings; let account = null; let quotas = null; let refreshMinutes = normalizeAccountQuotaRefreshMinutes( - telemetryStore.getState().telemetry?.settings?.accountQuotaRefreshMinutes ?? - DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES + initialSettings?.accountQuotaRefreshMinutes ?? DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES ); + let draftRiskAccepted = Boolean(initialSettings?.accountSwitchRiskAccepted); + let acceptingRisk = false; + let riskLockedFromServer = warmAccountCache ? Boolean(warmAccountCache.riskLocked) : true; let accounts = warmAccountCache ? [...warmAccountCache.accounts] : []; let selectedEmail = warmAccountCache ? warmAccountCache.selectedEmail : ''; let supported = warmAccountCache ? warmAccountCache.supported : null; let vaultSecurity = warmAccountCache?.vaultSecurity ? { ...warmAccountCache.vaultSecurity } : { enabled: false, unlocked: true }; + let quotaHistory = warmAccountCache?.quotaHistory ? { ...warmAccountCache.quotaHistory } : {}; let actionNonce = warmAccountCache?.actionNonce || ''; let operation = ''; let status = ''; @@ -29527,10 +30644,12 @@ const SettingsAccountTab = defineComponent({ if (supported !== true) return; warmAccountCache = { supported, + riskLocked: riskLockedFromServer, accounts: [...accounts], selectedEmail, savedQuotasEntries: [...savedQuotas.entries()], vaultSecurity: { ...vaultSecurity }, + quotaHistory: { ...quotaHistory }, actionNonce, }; } @@ -29581,12 +30700,19 @@ const SettingsAccountTab = defineComponent({ pwdConfirmInput.value = ''; } + // --- Section 1: Active Account Card Host --- + const activeCardHost = h('div', { className: 'account-cards-stack' }); + + // --- Section 2: Quota Trend Line Chart Card --- + const quotaTrendCard = AccountQuotaTrendCard(); + + // --- Section 3: Multi-Account Toolbar, Drawers, Backup Cards & Risk Gate --- const loginBtnLabel = h('span'); const loginButton = h( 'button', { type: 'button', - className: 'btn-primary settings-account-login account-toolbar-btn', + className: 'btn-primary settings-account-login', onClick: () => { clearConfirmTarget(false); void request('loginStart', { language: getEffectiveLocale() }); @@ -29596,7 +30722,7 @@ const SettingsAccountTab = defineComponent({ ); const cancelLogin = h('button', { type: 'button', - className: 'btn-secondary settings-account-login-cancel account-toolbar-btn', + className: 'btn-secondary settings-account-login-cancel', hidden: true, onClick: () => request('loginCancel', { id: loginSession?.id }), }); @@ -29605,7 +30731,7 @@ const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn', + className: 'btn-secondary', onClick: () => { clearConfirmTarget(false); importOpen = !importOpen; @@ -29626,13 +30752,14 @@ const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn settings-account-pwd-toggle', + className: 'btn-secondary settings-account-pwd-toggle', onClick: () => { clearConfirmTarget(false); pwdDrawerOpen = !pwdDrawerOpen; if (pwdDrawerOpen) { importOpen = false; pwdManageMode = 'change'; + pwdModeSegment.setProps({ modelValue: 'change', options: getPwdModeOptions() }); } clearPasswordInputs(); render(); @@ -29649,7 +30776,7 @@ const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn settings-account-lock-now', + className: 'btn-secondary settings-account-lock-now', hidden: true, onClick: () => { clearConfirmTarget(false); @@ -29691,17 +30818,17 @@ const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'icon-btn account-reload-btn', + className: 'icon-btn', onClick: () => request('list'), }, [createSvgIcon('refresh', { size: 13 })] ); - // --- Locked Vault Unlock Strip --- - const unlockHintLabel = h('span'); + // Locked Vault Notice (reusing `.settings-bridge-notice`) + const unlockHintLabel = h('span', { className: 'settings-bridge-title' }); const forgotToggleBtn = h('button', { type: 'button', - className: 'account-link-btn settings-account-forgot-btn', + className: 'btn-ghost-sm settings-account-forgot-btn', onClick: () => { forgotResetOpen = !forgotResetOpen; clearConfirmTarget(false); @@ -29717,7 +30844,7 @@ const SettingsAccountTab = defineComponent({ }); const unlockSubmitBtn = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn settings-account-unlock-submit', + className: 'btn-primary settings-account-unlock-submit', }); const unlockForm = h( 'form', @@ -29728,55 +30855,47 @@ const SettingsAccountTab = defineComponent({ }, [unlockInput, unlockSubmitBtn] ); - const resetHintEl = h('span', { className: 'settings-action-desc' }); + const resetHintEl = h('p', { className: 'settings-bridge-desc', hidden: true }); const resetVaultBtn = h('button', { type: 'button', - className: 'btn-secondary account-card-btn account-confirm-danger-btn settings-account-reset-vault', + className: 'btn-secondary btn-danger-confirm settings-account-reset-vault', + hidden: true, onClick: () => { armOrRunConfirm('resetVault', 'vault', () => { void request('resetVault', { confirmReset: 'RESET' }); }); }, }); - const resetRow = h('div', { className: 'account-reset-row', hidden: true }, [ - resetHintEl, - resetVaultBtn, - ]); - const unlockBox = h('div', { className: 'account-unlock-box settings-account-unlock-box', hidden: true }, [ - h('div', { className: 'account-unlock-head' }, [ - h('span', { className: 'account-unlock-title' }, [ - createSvgIcon('lock', { size: 13 }), - unlockHintLabel, + const unlockBox = h( + 'div', + { className: 'settings-bridge-notice settings-account-unlock-box', hidden: true }, + [ + h('div', { className: 'settings-bridge-head' }, [ + h('span', { className: 'settings-bridge-head-main' }, [ + createSvgIcon('lock', { size: 13, className: 'settings-bridge-icon' }), + unlockHintLabel, + ]), + forgotToggleBtn, ]), - forgotToggleBtn, - ]), - unlockForm, - resetRow, - ]); + unlockForm, + resetHintEl, + h('div', { className: 'settings-bridge-actions', hidden: true }, [resetVaultBtn]), + ] + ); + const resetActionsWrap = resetVaultBtn.parentElement; - // --- Passphrase Setup / Manage Drawer --- - const pwdTabChangeBtn = h('button', { - type: 'button', - className: 'account-pwd-tab', - onClick: () => { - pwdManageMode = 'change'; + // Passphrase Setup / Manage Drawer (reusing `SegmentedControl`!) + const pwdModeSegment = SegmentedControl({ + ariaLabel: t(key('pwdBtnManage')), + modelValue: pwdManageMode, + options: getPwdModeOptions(), + onUpdateModelValue: (val) => { + pwdManageMode = val === 'disable' ? 'disable' : 'change'; + pwdModeSegment.setProps({ modelValue: pwdManageMode, options: getPwdModeOptions() }); clearPasswordInputs(); render(); }, }); - const pwdTabDisableBtn = h('button', { - type: 'button', - className: 'account-pwd-tab', - onClick: () => { - pwdManageMode = 'disable'; - clearPasswordInputs(); - render(); - }, - }); - const pwdTabsWrap = h('div', { className: 'account-pwd-tabs', hidden: true }, [ - pwdTabChangeBtn, - pwdTabDisableBtn, - ]); const pwdCurrentInput = h('input', { type: 'password', className: 'settings-text-input account-import-input settings-account-pwd-current', @@ -29801,9 +30920,9 @@ const SettingsAccountTab = defineComponent({ }); const pwdSubmitBtn = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn settings-account-pwd-submit', + className: 'btn-primary settings-account-pwd-submit', }); - const pwdHint = h('div', { className: 'settings-action-desc account-import-hint' }); + const pwdHint = h('div', { className: 'settings-action-desc' }); const pwdForm = h( 'form', { @@ -29813,7 +30932,7 @@ const SettingsAccountTab = defineComponent({ onSubmit: submitPasswordDrawer, }, [ - pwdTabsWrap, + pwdModeSegment.el, h('div', { className: 'account-import-row' }, [ pwdCurrentInput, pwdNewInput, @@ -29824,7 +30943,7 @@ const SettingsAccountTab = defineComponent({ ] ); - // --- Credential Import Drawer --- + // Credential Import Drawer const credential = h('input', { type: 'password', className: 'settings-text-input account-import-input', @@ -29835,9 +30954,9 @@ const SettingsAccountTab = defineComponent({ }); const save = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn', + className: 'btn-primary', }); - const importHint = h('div', { className: 'settings-action-desc account-import-hint' }); + const importHint = h('div', { className: 'settings-action-desc' }); const form = h( 'form', { @@ -29852,26 +30971,27 @@ const SettingsAccountTab = defineComponent({ ] ); - const loginMessage = h('div', { - className: 'settings-action-desc account-flow-msg', + // OAuth Browser Login & Switch Notices (reusing `.settings-bridge-notice`) + const loginMessage = h('p', { + className: 'settings-bridge-desc settings-action-desc', role: 'status', hidden: true, }); const loginLink = h('a', { - className: 'account-flow-link', + className: 'settings-bridge-relaunch-btn', target: '_blank', rel: 'noopener noreferrer', hidden: true, }); - const loginStrip = h('div', { className: 'account-flow-strip', hidden: true }, [ - loginMessage, - loginLink, - ]); - const pending = h('div', { - className: 'settings-action-desc account-flow-strip is-warn', - role: 'status', - hidden: true, - }); + const loginStrip = h( + 'div', + { className: 'settings-bridge-notice is-restart', hidden: true }, + [loginMessage, h('div', { className: 'settings-bridge-actions' }, [loginLink])] + ); + const loginLinkWrap = loginLink.parentElement; + + const pendingDesc = h('p', { className: 'settings-bridge-desc', role: 'status' }); + const pending = h('div', { className: 'settings-bridge-notice', hidden: true }, [pendingDesc]); const toolbarPanel = h('section', { className: 'panel account-toolbar-panel' }, [ h('div', { className: 'account-toolbar-row' }, [ @@ -29895,36 +31015,261 @@ const SettingsAccountTab = defineComponent({ pending, ]); - const cardsHost = h('div', { className: 'account-cards-stack' }); + const backupCardsHost = h('div', { className: 'account-cards-stack' }); const emptyPanel = h('section', { className: 'panel account-empty-panel', hidden: true }, [ h('div', { className: 'settings-action-desc account-empty-text' }), ]); const emptyTextEl = emptyPanel.firstChild; + // Risk Gate Stage (100% shared `.ctx-risk-stage` / `.ctx-risk-mask` with `SettingsOtherTab`) + const riskTitleEl = h('strong', { + className: 'ctx-risk-title', + text: t('settings.oth.ctxRiskTitle'), + }); + const riskBodyEl = h('p', { + className: 'ctx-risk-desc', + text: t(key('riskBody')), + }); + const riskAcceptBtnText = h('span', { text: t('settings.oth.ctxRiskAcceptBtn') }); + const riskAcceptBtn = h( + 'button', + { + type: 'button', + className: 'btn-secondary btn-danger-confirm ctx-risk-accept-btn settings-account-risk-accept', + onClick: async () => { + if (acceptingRisk || draftRiskAccepted) return; + acceptingRisk = true; + draftRiskAccepted = true; + render(); + try { + await saveSettingsPatch({ accountSwitchRiskAccepted: true }); + await request('list'); + } catch { + draftRiskAccepted = false; + setTransientStatus('storeFailed', 2800); + } finally { + acceptingRisk = false; + if (!disposed) render(); + } + }, + }, + [createSvgIcon('warning', { size: 12 }), riskAcceptBtnText] + ); + const riskMask = h( + 'div', + { + className: 'ctx-risk-mask settings-account-risk-mask', + role: 'region', + 'aria-label': t('settings.oth.ctxRiskTitle'), + }, + [ + h('div', { className: 'ctx-risk-head' }, [ + createSvgIcon('warning', { size: 14, className: 'ctx-risk-icon' }), + riskTitleEl, + ]), + riskBodyEl, + riskAcceptBtn, + ] + ); + const managedControlsBody = h('div', { className: 'ctx-controls-body is-locked' }, [ + toolbarPanel, + backupCardsHost, + ]); + const riskStage = h('div', { className: 'ctx-risk-stage' }, [managedControlsBody, riskMask]); + function resolveActiveEmail() { return account?.status === 'signedIn' ? (account.email || '').trim().toLowerCase() : ''; } - function buildUnifiedAccountEntries(activeEmail) { - const entries = []; - if (activeEmail) { - entries.push({ - email: activeEmail, - isActive: true, - isSaved: accounts.includes(activeEmail), - }); - } - for (let i = 0; i < accounts.length; i++) { - const addr = accounts[i]; - if (addr && addr !== activeEmail) { - entries.push({ - email: addr, - isActive: false, - isSaved: true, + function createAccountCardDom(entry) { + const emailEl = h('span', { + className: 'settings-account-identity account-card-email', + text: entry.email, + title: entry.email, + }); + const tierPill = h('span', { className: 'status-tag state-synced', hidden: true }); + const statePill = h('span', { className: 'status-tag', hidden: true }); + + const captureBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm', + hidden: true, + onClick: () => { + clearConfirmTarget(false); + void request('capture'); + }, + }); + const selectBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm', + hidden: true, + onClick: () => { + armOrRunConfirm('switch', entry.email, () => { + const curActive = resolveActiveEmail(); + void request('switch', { + email: entry.email, + ...(curActive ? { activeEmail: curActive } : {}), + }); }); - } + }, + }); + const loadQuotaBtn = h( + 'button', + { + type: 'button', + className: 'icon-btn settings-account-load-quota', + hidden: true, + onClick: () => { + clearConfirmTarget(false); + void request('quota', { email: entry.email, manual: true }); + }, + }, + [createSvgIcon('refresh', { size: 12 })] + ); + const removeBtn = h( + 'button', + { + type: 'button', + className: 'icon-btn slot-remove-btn settings-account-remove', + hidden: true, + onClick: () => { + armOrRunConfirm('remove', entry.email, () => { + void request('remove', { email: entry.email }); + }); + }, + }, + [createSvgIcon('close', { size: 12 })] + ); + const removeConfirmBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm btn-danger-confirm settings-account-remove-confirm', + hidden: true, + onClick: () => { + armOrRunConfirm('remove', entry.email, () => { + void request('remove', { email: entry.email }); + }); + }, + }); + + const actionsWrap = h('div', { className: 'account-card-actions' }, [ + captureBtn, + selectBtn, + loadQuotaBtn, + removeBtn, + removeConfirmBtn, + ]); + const poolsWrap = h('div', { className: 'account-pools-stack' }); + const noteEl = h('div', { className: 'settings-action-desc account-card-note', hidden: true }); + + const cardEl = h('section', { className: 'panel account-card' }, [ + h('div', { className: 'panel-header account-card-head' }, [ + h('div', { className: 'account-card-meta' }, [emailEl, tierPill, statePill]), + actionsWrap, + ]), + poolsWrap, + noteEl, + ]); + return { + el: cardEl, + refs: { + cardEl, + emailEl, + tierPill, + statePill, + actionsWrap, + captureBtn, + selectBtn, + loadQuotaBtn, + removeBtn, + removeConfirmBtn, + poolsWrap, + noteEl, + }, + }; + } + + function updateAccountCardDom(row, entry, ctx) { + const addr = entry.email; + const active = entry.isActive; + const isSaved = entry.isSaved; + const written = addr === selectedEmail && !active && switchState?.state === 'written'; + const cached = active ? null : savedQuotas.get(addr); + const tierText = active + ? ctx.activeTierBadge + : cached?.tierName || cached?.tierId + ? formatAccountTierBadge({ + tierId: cached.tierId, + tierName: cached.tierName, + tierMultiplier: cached.tierMultiplier, + }) + : ''; + const isSpinning = !active && quotaVisibleLoading.has(addr); + const canSwitch = !active && isSaved && ctx.riskAccepted; + const armedSwitch = confirmTarget?.type === 'switch' && confirmTarget.key === addr; + const armedRemove = confirmTarget?.type === 'remove' && confirmTarget.key === addr; + + patchClasses(row.cardEl, { 'is-active-account': active }); + patchText(row.emailEl, addr); + patchAttr(row.emailEl, 'title', addr); + + patchText(row.tierPill, tierText); + patchAttr(row.tierPill, 'hidden', !tierText); + + patchText(row.statePill, active ? t(key('active')) : written ? t(key('written')) : ''); + patchClasses(row.statePill, { 'state-warn': written && !active }); + patchAttr(row.statePill, 'hidden', !active && !written); + + const showCapture = active && !isSaved && ctx.riskAccepted && supported !== false; + const showRemove = isSaved && ctx.riskAccepted; + const showQuotaRefresh = !active && isSaved && ctx.riskAccepted; + const hasAnyAction = showCapture || canSwitch || showQuotaRefresh || showRemove; + patchAttr(row.actionsWrap, 'hidden', !hasAnyAction); + + patchText(row.captureBtn, t(key('capture'))); + patchAttr(row.captureBtn, 'hidden', !showCapture); + patchAttr(row.captureBtn, 'disabled', !ctx.canWrite); + + patchText(row.selectBtn, t(key(armedSwitch ? 'switchConfirm' : 'switch'))); + patchAttr(row.selectBtn, 'title', t(key('switchHint'))); + patchClasses(row.selectBtn, { + 'settings-account-switch': canSwitch, + 'btn-danger-confirm': armedSwitch, + }); + patchAttr(row.selectBtn, 'hidden', !canSwitch); + patchAttr(row.selectBtn, 'disabled', !ctx.canWrite); + + patchAttr(row.loadQuotaBtn, 'title', t(key(isSpinning ? 'loadingQuota' : 'loadQuota'))); + patchAttr(row.loadQuotaBtn, 'aria-label', t(key('loadQuota'))); + patchClasses(row.loadQuotaBtn, { 'is-spinning': isSpinning }); + patchAttr(row.loadQuotaBtn, 'hidden', !showQuotaRefresh); + patchAttr(row.loadQuotaBtn, 'disabled', !ctx.canWrite || isSpinning); + + patchAttr(row.removeBtn, 'title', t(key('remove'))); + patchAttr(row.removeBtn, 'aria-label', t(key('remove'))); + patchAttr(row.removeBtn, 'hidden', !showRemove || armedRemove); + patchAttr(row.removeBtn, 'disabled', !ctx.canWrite); + + patchText(row.removeConfirmBtn, t(key('removeConfirm'))); + patchAttr(row.removeConfirmBtn, 'hidden', !showRemove || !armedRemove); + patchAttr(row.removeConfirmBtn, 'disabled', !ctx.canWrite); + + const displayedPools = active ? quotas?.pools || [] : cached?.pools || []; + const poolCount = renderQuotaRows(row.poolsWrap, displayedPools); + const quotaError = active ? '' : quotaErrors.get(addr); + + let noteText = ''; + if (quotaError) { + noteText = t(key(quotaError)); + } else if (active && poolCount > 0 && quotas?.meta?.isStale) { + noteText = t(key('quotaStale')); + } else if (active && poolCount === 0) { + noteText = t(key('noQuota')); + } else if (!active && cached && poolCount === 0) { + noteText = t(key('emptyCredentialQuota')); } - return entries; + patchText(row.noteEl, noteText); + patchClasses(row.noteEl, { 'is-error': Boolean(quotaError) }); + patchAttr(row.noteEl, 'hidden', !noteText); } function render() { @@ -29933,8 +31278,13 @@ const SettingsAccountTab = defineComponent({ if (activeEmail && selectedEmail === activeEmail && switchState?.state !== 'starting') { selectedEmail = ''; } + const riskAccepted = Boolean(draftRiskAccepted); + if (!riskAccepted) { + refreshIntervalSelect.close?.(); + } const vaultLocked = isVaultLocked(); const canOperateVault = + riskAccepted && supported === true && !operation && status !== 'restarting' && @@ -29945,6 +31295,55 @@ const SettingsAccountTab = defineComponent({ signedIn && (account?.tierName || account?.planName) ? formatAccountTierBadge(account) : ''; + const cardCtx = { riskAccepted, canWrite, activeTierBadge }; + + // 1. Active Logged-in Account Card + const activeEntries = activeEmail + ? [ + { + email: activeEmail, + isActive: true, + isSaved: riskAccepted && accounts.includes(activeEmail), + }, + ] + : []; + patchAttr(activeCardHost, 'hidden', activeEntries.length === 0); + patchKeyedList(activeCardHost, activeEntries, { + keyFn: (entry) => entry.email, + sigFn: (entry) => + JSON.stringify([ + locale, + entry.isSaved, + riskAccepted, + canWrite, + confirmTarget?.key === entry.email ? confirmTarget.type : '', + quotas, + activeTierBadge, + ]), + createRow: createAccountCardDom, + updateRow: (row, entry) => updateAccountCardDom(row, entry, cardCtx), + }); + + // 2. Quota Trend Line Chart Card (always visible; locked to activeEmail when !riskAccepted) + quotaTrendCard.refs.update({ + activeEmail, + activePools: quotas?.pools || [], + riskAccepted, + accounts, + savedQuotas, + quotaHistory, + }); + + // 3. Risk Gate Stage & Multi-Account Controls + patchClasses(managedControlsBody, { 'is-locked': !riskAccepted }); + patchAttr(managedControlsBody, 'inert', !riskAccepted); + patchAttr(managedControlsBody, 'aria-hidden', riskAccepted ? null : 'true'); + patchAttr(riskMask, 'hidden', riskAccepted); + patchAttr(riskMask, 'aria-label', t('settings.oth.ctxRiskTitle')); + patchText(riskTitleEl, t('settings.oth.ctxRiskTitle')); + patchText(riskBodyEl, t(key('riskBody'))); + patchText(riskAcceptBtnText, t('settings.oth.ctxRiskAcceptBtn')); + patchAttr(riskAcceptBtn, 'disabled', acceptingRisk); const loginActive = ['waiting', 'exchanging', 'saving'].includes(loginSession?.status); patchText(loginBtnLabel, t(key('googleLogin'))); @@ -29969,32 +31368,34 @@ const SettingsAccountTab = defineComponent({ patchAttr(lockNowBtn, 'hidden', !vaultSecurity.enabled || vaultLocked || supported === false); patchAttr(lockNowBtn, 'disabled', !canOperateVault); - // Unlock strip (only when passphrase enabled & locked) - patchAttr(unlockBox, 'hidden', supported !== true || !vaultLocked); + // Unlock Notice + patchAttr(unlockBox, 'hidden', !riskAccepted || supported !== true || !vaultLocked); patchText(unlockHintLabel, t(key('vaultLockedHint'))); patchText(forgotToggleBtn, t(key('forgotPwdToggle'))); patchAttr(unlockInput, 'placeholder', t(key('unlockPlaceholder'))); patchAttr(unlockInput, 'disabled', !canOperateVault); patchText(unlockSubmitBtn, t(key('unlockBtn'))); patchAttr(unlockSubmitBtn, 'disabled', !canOperateVault); - patchAttr(resetRow, 'hidden', !forgotResetOpen || !vaultLocked); + const showResetSection = forgotResetOpen && vaultLocked; + patchAttr(resetHintEl, 'hidden', !showResetSection); + patchAttr(resetActionsWrap, 'hidden', !showResetSection); + patchAttr(resetVaultBtn, 'hidden', !showResetSection); patchText(resetHintEl, t(key('resetVaultHint'))); const armedVaultReset = confirmTarget?.type === 'resetVault' && confirmTarget.key === 'vault'; patchText(resetVaultBtn, t(key(armedVaultReset ? 'resetVaultConfirm' : 'resetVaultBtn'))); patchAttr(resetVaultBtn, 'disabled', !canOperateVault); - // Passphrase setup / manage drawer - const showPwdDrawer = pwdDrawerOpen && !vaultLocked && supported === true; + // Passphrase Drawer (`SegmentedControl` for Change / Disable) + const showPwdDrawer = riskAccepted && pwdDrawerOpen && !vaultLocked && supported === true; patchAttr(pwdForm, 'hidden', !showPwdDrawer); if (showPwdDrawer) { const isSetNew = !vaultSecurity.enabled; const isDisableMode = !isSetNew && pwdManageMode === 'disable'; - patchAttr(pwdTabsWrap, 'hidden', isSetNew); - patchText(pwdTabChangeBtn, t(key('pwdTabChange'))); - patchClasses(pwdTabChangeBtn, { 'is-active': pwdManageMode === 'change' }); - patchText(pwdTabDisableBtn, t(key('pwdTabDisable'))); - patchClasses(pwdTabDisableBtn, { 'is-active': isDisableMode }); + patchAttr(pwdModeSegment.el, 'hidden', isSetNew); + if (!isSetNew) { + pwdModeSegment.setProps({ modelValue: pwdManageMode, options: getPwdModeOptions() }); + } patchAttr(pwdCurrentInput, 'hidden', isSetNew); patchAttr(pwdCurrentInput, 'placeholder', t(key('pwdCurrentPlaceholder'))); @@ -30015,7 +31416,7 @@ const SettingsAccountTab = defineComponent({ patchClasses(pwdSubmitBtn, { 'btn-primary': !isDisableMode, 'btn-secondary': isDisableMode, - 'account-confirm-danger-btn': isDisableMode, + 'btn-danger-confirm': isDisableMode, }); patchAttr(pwdSubmitBtn, 'disabled', !canOperateVault); patchText(pwdHint, t(key(isSetNew ? 'pwdSetHint' : 'pwdManageHint'))); @@ -30034,7 +31435,7 @@ const SettingsAccountTab = defineComponent({ patchAttr(reload, 'disabled', Boolean(operation)); patchClasses(reload, { 'is-spinning': operation === 'list' }); - patchAttr(form, 'hidden', !importOpen || vaultLocked || supported === false); + patchAttr(form, 'hidden', !riskAccepted || !importOpen || vaultLocked || supported === false); patchAttr(credential, 'placeholder', t(key('credentialPlaceholder'))); patchAttr(credential, 'disabled', !canWrite); patchText(save, t(key('save'))); @@ -30073,11 +31474,8 @@ const SettingsAccountTab = defineComponent({ patchText(loginLink, t(key('openLoginPage'))); patchAttr(loginLink, 'href', authUrl || null); patchAttr(loginLink, 'hidden', !authUrl); - patchClasses(loginStrip, { - 'is-error': loginIsError, - 'is-ok': loginSession?.status === 'success', - 'is-info': loginActive, - }); + patchAttr(loginLinkWrap, 'hidden', !authUrl); + patchClasses(loginStrip, { 'is-restart': !loginIsError }); patchAttr(loginStrip, 'hidden', !hasLoginSession || !loginMessageKey); const failedSwitch = @@ -30097,7 +31495,6 @@ const SettingsAccountTab = defineComponent({ patchClasses(feedback, { 'is-error': isFeedbackError, 'is-ok': isFeedbackOk, - 'is-info': !isFeedbackError && !isFeedbackOk && Boolean(message), }); patchAttr(feedback, 'hidden', !message); @@ -30106,16 +31503,38 @@ const SettingsAccountTab = defineComponent({ activeEmail !== selectedEmail && switchState?.state === 'written' && status !== 'restarting'; - patchText(pending, waiting ? t(key('pending'), { email: selectedEmail }) : ''); + patchText(pendingDesc, waiting ? t(key('pending'), { email: selectedEmail }) : ''); patchAttr(pending, 'hidden', !waiting); - const entries = buildUnifiedAccountEntries(activeEmail); - patchAttr(cardsHost, 'hidden', entries.length === 0); + const backupEntries = riskAccepted + ? accounts + .filter((addr) => addr && addr !== activeEmail) + .map((addr) => ({ email: addr, isActive: false, isSaved: true })) + : []; + patchAttr(backupCardsHost, 'hidden', backupEntries.length === 0); + patchKeyedList(backupCardsHost, backupEntries, { + keyFn: (entry) => entry.email, + sigFn: (entry) => + JSON.stringify([ + locale, + selectedEmail, + switchState?.state || '', + canWrite, + confirmTarget?.key === entry.email ? confirmTarget.type : '', + savedQuotas.get(entry.email), + quotaVisibleLoading.has(entry.email), + quotaErrors.get(entry.email), + ]), + createRow: createAccountCardDom, + updateRow: (row, entry) => updateAccountCardDom(row, entry, cardCtx), + }); + + const showEmpty = !activeEmail && (!riskAccepted || backupEntries.length === 0); patchText( emptyTextEl, t( key( - supported === null + supported === null && !activeEmail ? 'loading' : account?.status === 'signedOut' ? 'signedOut' @@ -30123,217 +31542,7 @@ const SettingsAccountTab = defineComponent({ ) ) ); - patchAttr(emptyPanel, 'hidden', entries.length > 0); - - patchKeyedList(cardsHost, entries, { - keyFn: (entry) => entry.email, - sigFn: (entry) => - JSON.stringify([ - locale, - entry.isActive, - entry.isSaved, - selectedEmail, - switchState?.state || '', - canWrite, - confirmTarget?.key === entry.email ? confirmTarget.type : '', - entry.isActive ? null : savedQuotas.get(entry.email), - quotaVisibleLoading.has(entry.email), - entry.isActive ? null : quotaErrors.get(entry.email), - entry.isActive ? quotas : null, - entry.isActive ? activeTierBadge : '', - ]), - createRow: (entry) => { - const activeDot = h('span', { className: 'dot-swatch account-active-dot', hidden: true }); - const emailEl = h('span', { - className: 'settings-account-identity account-card-email', - text: entry.email, - title: entry.email, - }); - const tierPill = h('span', { className: 'account-tag is-tier', hidden: true }); - const statePill = h('span', { className: 'account-tag', hidden: true }); - - const captureBtn = h('button', { - type: 'button', - className: 'btn-secondary account-card-btn', - hidden: true, - onClick: () => { - clearConfirmTarget(false); - void request('capture'); - }, - }); - const selectBtn = h('button', { - type: 'button', - className: 'btn-secondary account-card-btn', - hidden: true, - onClick: () => { - armOrRunConfirm('switch', entry.email, () => { - const curActive = resolveActiveEmail(); - void request('switch', { - email: entry.email, - ...(curActive ? { activeEmail: curActive } : {}), - }); - }); - }, - }); - const loadQuotaBtn = h( - 'button', - { - type: 'button', - className: 'icon-btn settings-account-load-quota account-icon-btn', - hidden: true, - onClick: () => { - clearConfirmTarget(false); - void request('quota', { email: entry.email, manual: true }); - }, - }, - [createSvgIcon('refresh', { size: 12 })] - ); - const removeBtn = h( - 'button', - { - type: 'button', - className: 'icon-btn slot-remove-btn settings-account-remove account-icon-btn', - hidden: true, - onClick: () => { - armOrRunConfirm('remove', entry.email, () => { - void request('remove', { email: entry.email }); - }); - }, - }, - [createSvgIcon('close', { size: 12 })] - ); - const removeConfirmBtn = h('button', { - type: 'button', - className: - 'btn-secondary account-card-btn account-confirm-danger-btn settings-account-remove-confirm', - hidden: true, - onClick: () => { - armOrRunConfirm('remove', entry.email, () => { - void request('remove', { email: entry.email }); - }); - }, - }); - - const poolsWrap = h('div', { className: 'account-pools-stack' }); - const noteEl = h('div', { className: 'settings-action-desc account-card-note', hidden: true }); - - const cardEl = h('section', { className: 'panel account-card' }, [ - h('div', { className: 'panel-header account-card-head' }, [ - h('div', { className: 'account-card-meta' }, [ - activeDot, - emailEl, - tierPill, - statePill, - ]), - h('div', { className: 'account-card-actions' }, [ - captureBtn, - selectBtn, - loadQuotaBtn, - removeBtn, - removeConfirmBtn, - ]), - ]), - poolsWrap, - noteEl, - ]); - return { - el: cardEl, - refs: { - cardEl, - activeDot, - emailEl, - tierPill, - statePill, - captureBtn, - selectBtn, - loadQuotaBtn, - removeBtn, - removeConfirmBtn, - poolsWrap, - noteEl, - }, - }; - }, - updateRow: (row, entry) => { - const addr = entry.email; - const active = entry.isActive; - const isSaved = entry.isSaved; - const written = addr === selectedEmail && !active && switchState?.state === 'written'; - const cached = active ? null : savedQuotas.get(addr); - const tierText = active - ? activeTierBadge - : cached?.tierName || cached?.tierId - ? formatAccountTierBadge({ - tierId: cached.tierId, - tierName: cached.tierName, - tierMultiplier: cached.tierMultiplier, - }) - : ''; - const isSpinning = !active && quotaVisibleLoading.has(addr); - const canSwitch = !active && isSaved; - const armedSwitch = confirmTarget?.type === 'switch' && confirmTarget.key === addr; - const armedRemove = confirmTarget?.type === 'remove' && confirmTarget.key === addr; - - patchClasses(row.cardEl, { 'is-active-account': active }); - patchAttr(row.activeDot, 'hidden', !active); - patchText(row.emailEl, addr); - patchAttr(row.emailEl, 'title', addr); - - patchText(row.tierPill, tierText); - patchAttr(row.tierPill, 'hidden', !tierText); - - patchText(row.statePill, active ? t(key('active')) : written ? t(key('written')) : ''); - patchClasses(row.statePill, { 'is-active': active, 'is-written': written }); - patchAttr(row.statePill, 'hidden', !active && !written); - - patchText(row.captureBtn, t(key('capture'))); - patchAttr(row.captureBtn, 'hidden', !active || isSaved || supported === false); - patchAttr(row.captureBtn, 'disabled', !canWrite); - - patchText(row.selectBtn, t(key(armedSwitch ? 'switchConfirm' : 'switch'))); - patchAttr(row.selectBtn, 'title', t(key('switchHint'))); - patchClasses(row.selectBtn, { - 'settings-account-switch': canSwitch, - 'is-confirm-warn': armedSwitch, - }); - patchAttr(row.selectBtn, 'hidden', !canSwitch); - patchAttr(row.selectBtn, 'disabled', !canWrite); - - patchAttr(row.loadQuotaBtn, 'title', t(key(isSpinning ? 'loadingQuota' : 'loadQuota'))); - patchAttr(row.loadQuotaBtn, 'aria-label', t(key('loadQuota'))); - patchClasses(row.loadQuotaBtn, { 'is-spinning': isSpinning }); - // Active account exclusively uses host telemetry — hide OAuth quota refresh on active card - patchAttr(row.loadQuotaBtn, 'hidden', active || !isSaved); - patchAttr(row.loadQuotaBtn, 'disabled', !canWrite || isSpinning); - - patchAttr(row.removeBtn, 'title', t(key('remove'))); - patchAttr(row.removeBtn, 'aria-label', t(key('remove'))); - patchAttr(row.removeBtn, 'hidden', !isSaved || armedRemove); - patchAttr(row.removeBtn, 'disabled', !canWrite); - - patchText(row.removeConfirmBtn, t(key('removeConfirm'))); - patchAttr(row.removeConfirmBtn, 'hidden', !isSaved || !armedRemove); - patchAttr(row.removeConfirmBtn, 'disabled', !canWrite); - - const displayedPools = active ? quotas?.pools || [] : cached?.pools || []; - const poolCount = renderQuotaRows(row.poolsWrap, displayedPools); - const quotaError = active ? '' : quotaErrors.get(addr); - - let noteText = ''; - if (quotaError) { - noteText = t(key(quotaError)); - } else if (active && poolCount > 0 && quotas?.meta?.isStale) { - noteText = t(key('quotaStale')); - } else if (active && poolCount === 0) { - noteText = t(key('noQuota')); - } else if (!active && cached && poolCount === 0) { - noteText = t(key('emptyCredentialQuota')); - } - patchText(row.noteEl, noteText); - patchClasses(row.noteEl, { 'is-error': Boolean(quotaError) }); - patchAttr(row.noteEl, 'hidden', !noteText); - }, - }); + patchAttr(emptyPanel, 'hidden', !showEmpty); props.onLayoutChange?.(); } @@ -30369,6 +31578,7 @@ const SettingsAccountTab = defineComponent({ } async function request(action, fields = {}) { + if (action !== 'list' && !draftRiskAccepted) return; if (action === 'quota') return refreshQuota(fields.email, Boolean(fields.manual)); if (operation) return; operation = action; @@ -30387,6 +31597,9 @@ const SettingsAccountTab = defineComponent({ if (typeof result?.actionNonce === 'string' && result.actionNonce) { actionNonce = result.actionNonce; } + if (result?.quotaHistory && typeof result.quotaHistory === 'object') { + quotaHistory = { ...quotaHistory, ...result.quotaHistory }; + } if (result?.error) { if (result.error === 'unsupported') supported = false; if (result.error === 'vaultLocked') { @@ -30402,6 +31615,7 @@ const SettingsAccountTab = defineComponent({ return; } supported = result?.supported === true; + riskLockedFromServer = Boolean(result?.riskLocked); accounts = result?.accounts || []; selectedEmail = result?.selectedEmail || ''; switchState = result?.switchState || null; @@ -30422,7 +31636,7 @@ const SettingsAccountTab = defineComponent({ savedQuotas.set(addr, snapshot); } syncWarmCache(); - if (action !== 'switch' && !isVaultLocked()) { + if (action !== 'switch' && draftRiskAccepted && !riskLockedFromServer && !isVaultLocked()) { void refreshSavedQuotas(); } if (action === 'save' || action === 'capture') { @@ -30478,7 +31692,7 @@ const SettingsAccountTab = defineComponent({ return; } await request('list'); - if (!disposed && !isVaultLocked() && email !== resolveActiveEmail()) { + if (!disposed && draftRiskAccepted && !isVaultLocked() && email !== resolveActiveEmail()) { void refreshQuota(email, false); } } @@ -30513,13 +31727,12 @@ const SettingsAccountTab = defineComponent({ } async function refreshSavedQuotas() { - if (refreshingSavedQuotas || isVaultLocked()) return; + if (refreshingSavedQuotas || !draftRiskAccepted || isVaultLocked()) return; refreshingSavedQuotas = true; try { const activeEmail = resolveActiveEmail(); const ttlMs = refreshMinutes * 60_000; const staleTargets = accounts.filter((addr) => { - // Active account exclusively uses host telemetry — never poll via OAuth if (!addr || addr === activeEmail) return false; const cached = savedQuotas.get(addr); if (needsLegacyUltraTierRefresh(cached)) return true; @@ -30527,7 +31740,7 @@ const SettingsAccountTab = defineComponent({ }); const BATCH_SIZE = 3; for (let i = 0; i < staleTargets.length; i += BATCH_SIZE) { - if (disposed || operation === 'switch' || isVaultLocked()) break; + if (disposed || operation === 'switch' || !draftRiskAccepted || isVaultLocked()) break; const batch = staleTargets.slice(i, i + BATCH_SIZE); await Promise.all(batch.map((addr) => refreshQuota(addr, false))); } @@ -30538,7 +31751,14 @@ const SettingsAccountTab = defineComponent({ async function refreshQuota(addr, manual = false) { const activeEmail = resolveActiveEmail(); - if (!addr || addr === activeEmail || isVaultLocked() || quotaInFlight.has(addr) || disposed) { + if ( + !addr || + addr === activeEmail || + !draftRiskAccepted || + isVaultLocked() || + quotaInFlight.has(addr) || + disposed + ) { return; } const hasCachedPools = Boolean(savedQuotas.get(addr)?.pools?.length); @@ -30570,9 +31790,10 @@ const SettingsAccountTab = defineComponent({ const nextTierId = result.tierId || prev.tierId || ''; const nextTierName = result.tierName || prev.tierName || ''; const nextTierMult = result.tierMultiplier || prev.tierMultiplier || ''; + const fetchedAt = result.fetchedAt || Date.now(); savedQuotas.set(addr, { pools: result.pools, - fetchedAt: result.fetchedAt || Date.now(), + fetchedAt, ...(nextTierId ? { tierId: nextTierId } : {}), ...(nextTierName ? { tierName: nextTierName } : {}), ...(nextTierMult ? { tierMultiplier: nextTierMult } : {}), @@ -30661,24 +31882,41 @@ const SettingsAccountTab = defineComponent({ settingsData?.accountQuotaRefreshMinutes !== undefined ? normalizeAccountQuotaRefreshMinutes(settingsData.accountQuotaRefreshMinutes) : refreshMinutes; - if (account === value && quotas === quotaData && refreshMinutes === nextRefreshMins) return; + const nextRiskAccepted = + acceptingRisk || settingsData?.accountSwitchRiskAccepted === undefined + ? draftRiskAccepted + : Boolean(settingsData.accountSwitchRiskAccepted); + if ( + account === value && + quotas === quotaData && + refreshMinutes === nextRefreshMins && + draftRiskAccepted === nextRiskAccepted + ) { + return; + } + const becameRiskUnlocked = !draftRiskAccepted && nextRiskAccepted; account = value; quotas = quotaData; refreshMinutes = nextRefreshMins; + draftRiskAccepted = nextRiskAccepted; render(); + if (becameRiskUnlocked && riskLockedFromServer && !operation) { + void request('list'); + } } const quotaScheduleTimer = setInterval(() => { - if (!disposed && accounts.length > 0 && !operation && !isVaultLocked()) { + if (!disposed && draftRiskAccepted && accounts.length > 0 && !operation && !isVaultLocked()) { void refreshSavedQuotas(); } }, 60_000); quotaScheduleTimer.unref?.(); const root = h('div', { className: 'settings-tab-stack settings-account-root' }, [ - toolbarPanel, - cardsHost, + quotaTrendCard.el, + activeCardHost, emptyPanel, + riskStage, ]); render(); onCleanup(() => { @@ -30688,6 +31926,8 @@ const SettingsAccountTab = defineComponent({ if (statusClearTimer) clearTimeout(statusClearTimer); if (confirmClearTimer) clearTimeout(confirmClearTimer); refreshIntervalSelect.destroy(); + pwdModeSegment.destroy(); + quotaTrendCard.destroy(); loginGeneration += 1; credential.value = ''; clearPasswordInputs(); @@ -30696,9 +31936,17 @@ const SettingsAccountTab = defineComponent({ el: root, refs: { renderAccount, - refreshLabels: render, + refreshLabels: () => { + quotaTrendCard.refs.refreshLabels(); + render(); + }, preload: () => (supported === null ? request('list') : Promise.resolve()), - load: () => (supported === null ? request('list') : refreshSavedQuotas()), + load: () => + supported === null || (draftRiskAccepted && riskLockedFromServer) + ? request('list') + : draftRiskAccepted + ? refreshSavedQuotas() + : Promise.resolve(), }, }; }, diff --git a/sidecars/panel/backend/account-quota-history.mjs b/sidecars/panel/backend/account-quota-history.mjs new file mode 100644 index 0000000..07e28e0 --- /dev/null +++ b/sidecars/panel/backend/account-quota-history.mjs @@ -0,0 +1,477 @@ +/** + * @fileoverview Compact, collinear-compressed per-account quota history store + * (`account-quota-history.mjs`). + * + * Records 5-hour and weekly remaining-percentage trajectories across quota pools + * for the active host account (from live telemetry) and saved backup accounts + * (from scheduled OAuth refreshes). Bootstraps initial history from existing + * `quota_reservation_state.json` observations when available. + */ + +import { parseIsoMs } from '../trajectory-metrics.mjs'; +import { + ACCOUNT_QUOTA_HISTORY_FILE, + QUOTA_RESERVATION_STATE_FILE, + loadJson, + lsRuntimeEndpoint, + saveJson, +} from './config.mjs'; + +const AQH_SCHEMA_VERSION = 1; +const AQH_MAX_ACCOUNTS = 32; +const AQH_MAX_POOLS_PER_ACCOUNT = 8; +const AQH_MAX_POINTS_PER_POOL = 1600; +const AQH_PERSIST_MIN_INTERVAL_MS = 5000; +const AQH_CHANGE_EPSILON_PCT = 0.05; +const AQH_MAX_FLAT_MERGE_GAP_MS = 6 * 3600 * 1000; + +function aqhClampPct(val) { + if (val === null || val === undefined || val === '') return null; + const num = Number(val); + if (!Number.isFinite(num)) return null; + return Math.round(Math.max(0, Math.min(100, num)) * 10) / 10; +} + +function aqhNormEmail(raw) { + return typeof raw === 'string' ? raw.trim().toLowerCase() : ''; +} + +function aqhSameVal(a, b) { + if (a === null && b === null) return true; + if (a === null || b === null) return false; + return Math.abs(a - b) < AQH_CHANGE_EPSILON_PCT; +} + +const AQH_IMPULSE_MAX_SPAN_MS = 20 * 60_000; +const AQH_IMPULSE_MIN_JUMP_PCT = 3; +const AQH_IMPULSE_MAX_NEIGHBOR_DIFF_PCT = 1.2; + +function aqhIsIsolatedUpwardImpulse(prevVal, curVal, nextVal) { + if (prevVal === null || curVal === null || nextVal === null) return false; + return ( + curVal - prevVal >= AQH_IMPULSE_MIN_JUMP_PCT && + curVal - nextVal >= AQH_IMPULSE_MIN_JUMP_PCT && + Math.abs(nextVal - prevVal) <= AQH_IMPULSE_MAX_NEIGHBOR_DIFF_PCT + ); +} + +/** + * Removes isolated single-sample upward spikes (e.g. cross-account `init` seed events + * where remaining % momentarily jumps `94.8 -> 100 -> 94.7` within minutes and snaps + * right back to the pre-spike trajectory). + */ +export function sanitizeQuotaHistoryPoints(points) { + if (!Array.isArray(points) || points.length < 3) return false; + let removed = false; + let i = 1; + while (i < points.length - 1) { + const prev = points[i - 1]; + const cur = points[i]; + const next = points[i + 1]; + const spanMs = next[0] - prev[0]; + if ( + spanMs >= 0 && + spanMs <= AQH_IMPULSE_MAX_SPAN_MS && + (aqhIsIsolatedUpwardImpulse(prev[1], cur[1], next[1]) || + aqhIsIsolatedUpwardImpulse(prev[2], cur[2], next[2])) + ) { + points.splice(i, 1); + removed = true; + if (i > 1) i -= 1; + continue; + } + i += 1; + } + return removed; +} + +function aqhDownsamplePoints(points, targetLen) { + if (points.length <= targetLen) return points; + const keepRecent = Math.floor(targetLen * 0.65); + const oldEnd = points.length - keepRecent; + const oldQuota = Math.max(2, targetLen - keepRecent); + const stride = Math.max(2, Math.ceil(oldEnd / oldQuota)); + const compacted = []; + for (let i = 0; i < oldEnd; i += stride) { + const sliceEnd = Math.min(oldEnd, i + stride); + let minPt = points[i]; + let maxPt = points[i]; + for (let j = i + 1; j < sliceEnd; j++) { + const p = points[j]; + const v = p[1] ?? p[2] ?? 100; + if (v < (minPt[1] ?? minPt[2] ?? 100)) minPt = p; + if (v > (maxPt[1] ?? maxPt[2] ?? 0)) maxPt = p; + } + if (minPt === maxPt) { + compacted.push(minPt); + } else if (minPt[0] <= maxPt[0]) { + compacted.push(minPt, maxPt); + } else { + compacted.push(maxPt, minPt); + } + } + for (let i = oldEnd; i < points.length; i++) { + compacted.push(points[i]); + } + return compacted; +} + +function aqhAppendPoint(points, tsMs, r5h, rWk) { + const len = points.length; + if (len === 0) { + points.push([tsMs, r5h, rWk]); + return true; + } + const last = points[len - 1]; + if (tsMs < last[0]) { + return false; + } + const resolved5h = r5h !== null ? r5h : last[1]; + const resolvedWk = rWk !== null ? rWk : last[2]; + if (tsMs === last[0]) { + if (!aqhSameVal(last[1], resolved5h) || !aqhSameVal(last[2], resolvedWk)) { + last[1] = resolved5h; + last[2] = resolvedWk; + return true; + } + return false; + } + const unchanged = aqhSameVal(last[1], resolved5h) && aqhSameVal(last[2], resolvedWk); + if (unchanged && len >= 2) { + const prev = points[len - 2]; + const prevSame = aqhSameVal(prev[1], resolved5h) && aqhSameVal(prev[2], resolvedWk); + if (prevSame && tsMs - prev[0] <= AQH_MAX_FLAT_MERGE_GAP_MS) { + // Collinear flat segment: advance trailing endpoint timestamp in place + if (tsMs - last[0] < 60_000) return false; + last[0] = tsMs; + return true; + } + } + if (unchanged && tsMs - last[0] < 10 * 60_000) { + return false; + } + points.push([tsMs, resolved5h, resolvedWk]); + if (points.length > AQH_MAX_POINTS_PER_POOL) { + const trimmed = aqhDownsamplePoints(points, Math.floor(AQH_MAX_POINTS_PER_POOL * 0.8)); + points.length = 0; + for (let i = 0; i < trimmed.length; i++) points.push(trimmed[i]); + } + return true; +} + +export function createAccountQuotaHistoryTracker({ + loadState = () => null, + saveState = () => {}, + loadSeedEvents = () => null, +} = {}) { + const accountsMap = new Map(); + let latestHostEmail = ''; + let hydrated = false; + let seededFromQr = false; + let lastPersistMs = 0; + let dirty = false; + + function ensureAccountEntry(email, nowMs) { + let acc = accountsMap.get(email); + if (acc) { + acc.updatedAtMs = nowMs; + return acc; + } + if (accountsMap.size >= AQH_MAX_ACCOUNTS) { + let oldestEmail = null; + let oldestTs = Infinity; + for (const [k, v] of accountsMap.entries()) { + if ((v.updatedAtMs || 0) < oldestTs) { + oldestTs = v.updatedAtMs || 0; + oldestEmail = k; + } + } + if (oldestEmail) accountsMap.delete(oldestEmail); + } + acc = { updatedAtMs: nowMs, pools: new Map() }; + accountsMap.set(email, acc); + return acc; + } + + function hydrate() { + if (hydrated) return; + hydrated = true; + try { + const raw = loadState(); + if (raw && typeof raw === 'object' && raw.accounts && typeof raw.accounts === 'object') { + seededFromQr = Boolean(raw.seededFromQr); + for (const [rawEmail, accVal] of Object.entries(raw.accounts)) { + const email = aqhNormEmail(rawEmail); + if (!email || !accVal || typeof accVal !== 'object') continue; + const poolsMap = new Map(); + for (const [pk, pVal] of Object.entries(accVal.pools || {})) { + if (!pk || !pVal || !Array.isArray(pVal.points)) continue; + const cleanPts = []; + for (let i = 0; i < pVal.points.length; i++) { + const pt = pVal.points[i]; + if (!Array.isArray(pt) || !(Number(pt[0]) > 0)) continue; + const r5 = aqhClampPct(pt[1]); + const rw = aqhClampPct(pt[2]); + if (r5 === null && rw === null) continue; + cleanPts.push([Math.floor(Number(pt[0])), r5, rw]); + } + if (cleanPts.length > 0) { + if (sanitizeQuotaHistoryPoints(cleanPts)) { + dirty = true; + } + poolsMap.set(pk, { + label: String(pVal.label || pk), + reset5hMs: Number(pVal.reset5hMs) > 0 ? Math.floor(Number(pVal.reset5hMs)) : 0, + resetWeeklyMs: + Number(pVal.resetWeeklyMs) > 0 ? Math.floor(Number(pVal.resetWeeklyMs)) : 0, + points: cleanPts.slice(-AQH_MAX_POINTS_PER_POOL), + }); + } + } + if (poolsMap.size > 0) { + accountsMap.set(email, { + updatedAtMs: Number(accVal.updatedAtMs) || Date.now(), + pools: poolsMap, + }); + } + } + } + } catch { + // Ignore corrupt history file and start fresh + } + } + + function seedFromReservationEventsIfEmpty(activeEmail) { + if (seededFromQr || !activeEmail) return; + const existing = accountsMap.get(activeEmail); + if (existing && existing.pools.size > 0) { + seededFromQr = true; + return; + } + seededFromQr = true; + try { + const qr = loadSeedEvents(); + const events = Array.isArray(qr?.events) ? qr.events : []; + if (events.length === 0) return; + const acc = ensureAccountEntry(activeEmail, Date.now()); + for (let i = 0; i < events.length; i++) { + const ev = events[i]; + const pk = String(ev?.pool || '').trim(); + const win = String(ev?.win || '').trim(); + const tsMs = parseIsoMs(ev?.ts); + const used = Number(ev?.usedPct); + if (!pk || !(tsMs > 0) || !Number.isFinite(used)) continue; + const rem = aqhClampPct(100 - used); + if (rem === null) continue; + let poolEntry = acc.pools.get(pk); + if (!poolEntry) { + if (acc.pools.size >= AQH_MAX_POOLS_PER_ACCOUNT) continue; + poolEntry = { label: pk, reset5hMs: 0, resetWeeklyMs: 0, points: [] }; + acc.pools.set(pk, poolEntry); + } + if (ev?.event === 'init' && poolEntry.points.length > 0) { + const lastPt = poolEntry.points[poolEntry.points.length - 1]; + const prevVal = win === 'weekly' ? lastPt[2] : lastPt[1]; + if (prevVal !== null && rem - prevVal >= AQH_IMPULSE_MIN_JUMP_PCT) { + continue; + } + } + aqhAppendPoint( + poolEntry.points, + tsMs, + win === '5h' ? rem : null, + win === 'weekly' ? rem : null + ); + dirty = true; + } + for (const poolEntry of acc.pools.values()) { + sanitizeQuotaHistoryPoints(poolEntry.points); + } + } catch { + // Optional seed is best-effort + } + } + + function persistIfNeeded(nowMs, force = false) { + if (!dirty) return; + if (!force && lastPersistMs > 0 && nowMs - lastPersistMs < AQH_PERSIST_MIN_INTERVAL_MS) { + return; + } + lastPersistMs = nowMs; + dirty = false; + const serializedAccounts = {}; + for (const [email, acc] of accountsMap.entries()) { + const poolsObj = {}; + for (const [pk, pVal] of acc.pools.entries()) { + if (!pVal.points.length) continue; + poolsObj[pk] = { + label: pVal.label, + ...(pVal.reset5hMs > 0 ? { reset5hMs: pVal.reset5hMs } : {}), + ...(pVal.resetWeeklyMs > 0 ? { resetWeeklyMs: pVal.resetWeeklyMs } : {}), + points: pVal.points, + }; + } + if (Object.keys(poolsObj).length > 0) { + serializedAccounts[email] = { + updatedAtMs: acc.updatedAtMs, + pools: poolsObj, + }; + } + } + try { + saveState({ + schemaVersion: AQH_SCHEMA_VERSION, + seededFromQr, + updatedAt: new Date(nowMs).toISOString(), + accounts: serializedAccounts, + }); + } catch { + // Best-effort persistence + } + } + + function recordSnapshot(rawEmail, pools, nowMs = Date.now(), { isActiveHost = false } = {}) { + const email = aqhNormEmail(rawEmail); + if (!email || !Array.isArray(pools) || pools.length === 0) return; + hydrate(); + if (isActiveHost) { + latestHostEmail = email; + seedFromReservationEventsIfEmpty(email); + } + const tsMs = Number.isFinite(Number(nowMs)) && Number(nowMs) > 0 ? Math.floor(Number(nowMs)) : Date.now(); + const acc = ensureAccountEntry(email, tsMs); + let anyChanged = false; + + for (let i = 0; i < pools.length; i++) { + const p = pools[i]; + const pk = String(p?.poolKey || p?.family || p?.poolLabel || '').trim(); + if (!pk || pk === 'Unknown') continue; + const r5h = aqhClampPct(p?.remainingPct ?? p?.remaining5hPct); + const rWk = aqhClampPct(p?.weeklyRemainingPct ?? p?.remainingWeeklyPct); + if (r5h === null && rWk === null) continue; + + let poolEntry = acc.pools.get(pk); + if (!poolEntry) { + if (acc.pools.size >= AQH_MAX_POOLS_PER_ACCOUNT) continue; + poolEntry = { + label: String(p?.poolLabel || pk), + reset5hMs: 0, + resetWeeklyMs: 0, + points: [], + }; + acc.pools.set(pk, poolEntry); + } + if (p?.poolLabel) poolEntry.label = String(p.poolLabel); + const nextReset5h = parseIsoMs(p?.resetTime || p?.reset5h); + const nextResetWk = parseIsoMs(p?.weeklyResetTime || p?.resetWeekly); + if (nextReset5h > 0 && nextReset5h !== poolEntry.reset5hMs) { + poolEntry.reset5hMs = nextReset5h; + anyChanged = true; + } + if (nextResetWk > 0 && nextResetWk !== poolEntry.resetWeeklyMs) { + poolEntry.resetWeeklyMs = nextResetWk; + anyChanged = true; + } + if (aqhAppendPoint(poolEntry.points, tsMs, r5h, rWk)) { + anyChanged = true; + } + } + + if (anyChanged) { + dirty = true; + persistIfNeeded(tsMs, false); + } + } + + function removeAccount(rawEmail, nowMs = Date.now()) { + const email = aqhNormEmail(rawEmail); + if (!email) return; + hydrate(); + if (accountsMap.delete(email)) { + dirty = true; + persistIfNeeded(nowMs, true); + } + } + + function getHistoryPayload({ allowedEmails = null } = {}) { + hydrate(); + const allowSet = Array.isArray(allowedEmails) + ? new Set(allowedEmails.map(aqhNormEmail).filter(Boolean)) + : null; + const out = {}; + for (const [email, acc] of accountsMap.entries()) { + if (allowSet && !allowSet.has(email)) continue; + const poolsObj = {}; + for (const [pk, pVal] of acc.pools.entries()) { + if (!pVal.points.length) continue; + poolsObj[pk] = { + label: pVal.label, + ...(pVal.reset5hMs > 0 ? { reset5hMs: pVal.reset5hMs } : {}), + ...(pVal.resetWeeklyMs > 0 ? { resetWeeklyMs: pVal.resetWeeklyMs } : {}), + points: pVal.points, + }; + } + if (Object.keys(poolsObj).length > 0) { + out[email] = { pools: poolsObj }; + } + } + return out; + } + + function reset() { + accountsMap.clear(); + latestHostEmail = ''; + hydrated = true; + seededFromQr = false; + lastPersistMs = 0; + dirty = false; + } + + return { + recordSnapshot, + removeAccount, + getHistoryPayload, + getLatestHostEmail: () => latestHostEmail, + flush: (nowMs = Date.now()) => persistIfNeeded(nowMs, true), + reset, + }; +} + +function aqhIsTestRuntime() { + return ( + lsRuntimeEndpoint?.addr === 'test.invalid' || + String(process?.env?.ANTIGRAVITY_LS_ADDRESS || '').trim() === 'test.invalid' + ); +} + +const defaultAccountQuotaHistoryTracker = createAccountQuotaHistoryTracker({ + loadState: () => (aqhIsTestRuntime() ? null : loadJson(ACCOUNT_QUOTA_HISTORY_FILE, null)), + saveState: (payload) => { + if (!aqhIsTestRuntime()) saveJson(ACCOUNT_QUOTA_HISTORY_FILE, payload); + }, + loadSeedEvents: () => (aqhIsTestRuntime() ? null : loadJson(QUOTA_RESERVATION_STATE_FILE, null)), +}); + +export function recordAccountQuotaSnapshot(email, pools, nowMs = Date.now(), opts = {}) { + defaultAccountQuotaHistoryTracker.recordSnapshot(email, pools, nowMs, opts); +} + +export function removeAccountQuotaHistory(email, nowMs = Date.now()) { + defaultAccountQuotaHistoryTracker.removeAccount(email, nowMs); +} + +export function getAccountQuotaHistoryPayload(opts = {}) { + return defaultAccountQuotaHistoryTracker.getHistoryPayload(opts); +} + +export function getLatestRecordedHostEmail() { + return defaultAccountQuotaHistoryTracker.getLatestHostEmail(); +} + +export function flushAccountQuotaHistoryNow(nowMs = Date.now()) { + defaultAccountQuotaHistoryTracker.flush(nowMs); +} + +export function resetAccountQuotaHistoryState() { + defaultAccountQuotaHistoryTracker.reset(); +} diff --git a/sidecars/panel/backend/account-switch.mjs b/sidecars/panel/backend/account-switch.mjs index 4b86355..b682dc5 100644 --- a/sidecars/panel/backend/account-switch.mjs +++ b/sidecars/panel/backend/account-switch.mjs @@ -11,6 +11,10 @@ import { resolveGoogleCredential, retrieveCredentialQuota, } from './account-oauth.mjs'; +import { + recordAccountQuotaSnapshot, + removeAccountQuotaHistory, +} from './account-quota-history.mjs'; import { checkVaultUnlockCooldown, createVaultSecurityMetadata, @@ -36,7 +40,7 @@ const ACCOUNT_ERRORS = new Set([ 'oauthUnavailable', 'quotaUnavailable', 'switchStartFailed', 'clientCloseFailed', 'clientRestartFailed', 'vaultLocked', 'wrongPassword', 'invalidPassword', 'passwordRateLimited', - 'vaultDecryptFailed', 'invalidActionNonce', + 'vaultDecryptFailed', 'invalidActionNonce', 'riskNotAccepted', ]); const ALLOWED_ACTIONS = new Set([ @@ -717,6 +721,7 @@ export async function handleAccountCredentialAction( if (action === 'remove') { const activeSec = await ensureUnlockedVaultContext(); clearTransientAccessTokens(email); + removeAccountQuotaHistory(email); return publicList( await runStore({ action, @@ -806,6 +811,9 @@ export async function handleAccountCredentialAction( expectedCredential: expectedCredForQuota, quota: quotaPayload, }); + if (useDiskSigCache) { + recordAccountQuotaSnapshot(email, pools, fetchedAt, { isActiveHost: false }); + } return { email, pools, fetchedAt, ...tierMeta }; } diff --git a/sidecars/panel/backend/api-actions.mjs b/sidecars/panel/backend/api-actions.mjs index b278ab9..f71d756 100644 --- a/sidecars/panel/backend/api-actions.mjs +++ b/sidecars/panel/backend/api-actions.mjs @@ -9,6 +9,10 @@ import { randomUUID } from 'node:crypto'; import { isDeepStrictEqual } from 'node:util'; import { BRIDGE_API_ROUTE_DEFS, LOCAL_ENDPOINT_ID } from '../shared/window-bridge-protocol.mjs'; +import { + getAccountQuotaHistoryPayload, + getLatestRecordedHostEmail, +} from './account-quota-history.mjs'; import { handleAccountCredentialAction, warmAccountPublicListCache } from './account-switch.mjs'; import { issueAccountActionNonce, verifyAccountActionNonce } from './account-vault-crypto.mjs'; import { @@ -644,6 +648,38 @@ export async function executeAccountCredentialAction(data, opts = {}) { } const cleanData = stripEndpointField(data); const action = cleanData?.action || 'list'; + const riskAccepted = + opts?.bypassRiskCheck === true || loadSettings().accountSwitchRiskAccepted === true; + if (!riskAccepted) { + if (action !== 'list') { + return { + status: 200, + contentType: 'application/json', + headers: { 'Cache-Control': 'no-store' }, + body: JSON.stringify({ + error: 'riskNotAccepted', + actionNonce: issueAccountActionNonce(), + }), + }; + } + const hostEmail = getLatestRecordedHostEmail(); + return { + status: 200, + contentType: 'application/json', + headers: { 'Cache-Control': 'no-store' }, + body: JSON.stringify({ + supported: process.platform === 'win32', + riskLocked: true, + accounts: [], + selectedEmail: '', + quotas: {}, + quotaHistory: getAccountQuotaHistoryPayload({ + allowedEmails: hostEmail ? [hostEmail] : [], + }), + actionNonce: issueAccountActionNonce(), + }), + }; + } if (NONCE_PROTECTED_ACCOUNT_ACTIONS.has(action) && !verifyAccountActionNonce(cleanData?.actionNonce)) { return { status: 200, @@ -663,9 +699,25 @@ export async function executeAccountCredentialAction(data, opts = {}) { if (action === 'switch' && !result?.error) { clearTelemetryBuilderMemo(LOCAL_ENDPOINT_ID); } + const hostEmail = getLatestRecordedHostEmail(); + const allowedHistoryEmails = Array.isArray(result?.accounts) + ? [...new Set([...result.accounts, ...(hostEmail ? [hostEmail] : [])])] + : hostEmail + ? [hostEmail] + : null; const enriched = result && typeof result === 'object' - ? { ...result, actionNonce: issueAccountActionNonce() } + ? { + ...result, + ...(action === 'list' || Array.isArray(result.accounts) + ? { + quotaHistory: getAccountQuotaHistoryPayload({ + allowedEmails: allowedHistoryEmails, + }), + } + : {}), + actionNonce: issueAccountActionNonce(), + } : result; return { status: 200, @@ -904,8 +956,9 @@ export function bootWindowBridgeRuntime({ allowDynamicLs = false, onRestart = nu if (lsRuntimeEndpoint.addr === 'test.invalid') return null; if (!allowDynamicLs && !lsRuntimeEndpoint.addr) return null; - ensureCliStatusLineConfigured({ enabled: loadSettings().enableCliStatusLine !== false }); - if (allowDynamicLs) { + const bootSettings = loadSettings(); + ensureCliStatusLineConfigured({ enabled: bootSettings.enableCliStatusLine !== false }); + if (allowDynamicLs && bootSettings.accountSwitchRiskAccepted === true) { setTimeout(() => { void warmAccountPublicListCache(); }, 80).unref?.(); diff --git a/sidecars/panel/backend/config.mjs b/sidecars/panel/backend/config.mjs index 2964347..fc649df 100644 --- a/sidecars/panel/backend/config.mjs +++ b/sidecars/panel/backend/config.mjs @@ -169,6 +169,7 @@ export const TRAJECTORY_DB_FILE = join(DATA_DIR, 'trajectory_store.sqlite'); export const REMOTE_CATALOG_CACHE_FILE = join(DATA_DIR, 'remote_pricing_cache.json'); export const CLI_STATUS_SNAPSHOT_FILE = join(DATA_DIR, 'cli-status-snapshot.json'); export const QUOTA_RESERVATION_STATE_FILE = join(DATA_DIR, 'quota_reservation_state.json'); +export const ACCOUNT_QUOTA_HISTORY_FILE = join(DATA_DIR, 'account_quota_history.json'); /** * 本地对话记录目录、脑图日志目录、守护发现目录与跨端深层统计缓存路径候选列表(联合覆盖 Desktop / CLI / IDE)。 diff --git a/sidecars/panel/backend/settings-schema.mjs b/sidecars/panel/backend/settings-schema.mjs index b89a792..2ed5ad6 100644 --- a/sidecars/panel/backend/settings-schema.mjs +++ b/sidecars/panel/backend/settings-schema.mjs @@ -169,6 +169,7 @@ export const DEFAULT_SETTINGS_SCHEMA = Object.freeze({ stepTableColumns: DEFAULT_STEP_TABLE_COLUMNS, experimentalQuotaReservation: true, accountQuotaRefreshMinutes: DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES, + accountSwitchRiskAccepted: false, contextWindowRiskAccepted: false, contextWindowMainEnabled: true, contextWindowSwarmEnabled: false, @@ -256,6 +257,7 @@ export function normalizeFullSettings(rawInput) { accountQuotaRefreshMinutes: normalizeAccountQuotaRefreshMinutes( safeRaw.accountQuotaRefreshMinutes ), + accountSwitchRiskAccepted: safeRaw.accountSwitchRiskAccepted === true, contextWindowRiskAccepted: safeRaw.contextWindowRiskAccepted === true, contextWindowMainEnabled: safeRaw.contextWindowMainEnabled !== false, contextWindowSwarmEnabled: safeRaw.contextWindowSwarmEnabled === true, diff --git a/sidecars/panel/backend/sidecar-worker.mjs b/sidecars/panel/backend/sidecar-worker.mjs index 8923212..a49230a 100644 --- a/sidecars/panel/backend/sidecar-worker.mjs +++ b/sidecars/panel/backend/sidecar-worker.mjs @@ -14,6 +14,7 @@ import { dispatchBridgeApiRequest, } from './api-actions.mjs'; import { computeRuntimeCodeVersion } from './config.mjs'; +import { flushAccountQuotaHistoryNow } from './account-quota-history.mjs'; import { flushAllEndpointCachesNow } from './endpoint-registry.mjs'; let shuttingDown = false; @@ -52,6 +53,7 @@ async function gracefulStop() { shuttingDown = true; clearInterval(driftWatchTimer); try { + flushAccountQuotaHistoryNow(); flushAllEndpointCachesNow(); } catch { // Ignore cache flush errors diff --git a/sidecars/panel/backend/telemetry-builder.mjs b/sidecars/panel/backend/telemetry-builder.mjs index 4be4357..f4e0974 100644 --- a/sidecars/panel/backend/telemetry-builder.mjs +++ b/sidecars/panel/backend/telemetry-builder.mjs @@ -45,6 +45,7 @@ import { } from './model-pricing.mjs'; import { getCloudStatus, getPricingPhaseEpoch, pricingCatalogRevision } from './pricing-catalog.mjs'; import { buildOfficialQuotaPools } from './quota-pools.mjs'; +import { recordAccountQuotaSnapshot } from './account-quota-history.mjs'; import { updatePoolQuotaReservationEstimates } from './quota-reservation.mjs'; import { getDefaultCloudCodeProxyStatus } from './cloudcode-proxy.mjs'; import { getDefaultWindowBridgeStatus } from './window-bridge.mjs'; @@ -2293,6 +2294,20 @@ export async function buildTelemetryPayloadInternal( const order = { Gemini: 0, Argon: 1, Carbon: 2, 'Claude & GPT': 3 }; return (order[a.family] ?? 99) - (order[b.family] ?? 99); }); + if ( + !isRemote && + accountStatus === 'signedIn' && + userStatus?.email && + officialQuotaInfo?.data && + !officialQuotaInfo.isStale + ) { + recordAccountQuotaSnapshot( + userStatus.email, + sortedQuotaPools, + officialQuotaInfo.fetchedAtMs || Date.now(), + { isActiveHost: true } + ); + } const weeklyCapacityEstimate = buildWeeklyCapacityEstimate({ sortedPools: sortedQuotaPools, isCustomWeeklyMode, diff --git a/sidecars/panel/index.html b/sidecars/panel/index.html index fe7191d..c37c1f1 100644 --- a/sidecars/panel/index.html +++ b/sidecars/panel/index.html @@ -4,11 +4,11 @@ 用量监控 - +
- + diff --git a/sidecars/panel/scripts/build-bundle.mjs b/sidecars/panel/scripts/build-bundle.mjs index cb2a1ea..4c7cb2c 100644 --- a/sidecars/panel/scripts/build-bundle.mjs +++ b/sidecars/panel/scripts/build-bundle.mjs @@ -628,6 +628,7 @@ export function buildBundle(panelDir = PANEL_DIR) { 'src/components/widgets/MetricMatrixWidget.js', 'src/components/widgets/ContextWindowWidget.js', 'src/components/widgets/QuotaPoolsWidget.js', + 'src/components/widgets/AccountQuotaTrendCard.js', 'src/components/widgets/LiveActivityWidget.js', 'src/registries/widgetRegistry.js', 'src/registries/stepColumnRegistry.js', diff --git a/sidecars/panel/src/components/widgets/AccountQuotaTrendCard.js b/sidecars/panel/src/components/widgets/AccountQuotaTrendCard.js new file mode 100644 index 0000000..bd07d2b --- /dev/null +++ b/sidecars/panel/src/components/widgets/AccountQuotaTrendCard.js @@ -0,0 +1,1084 @@ +/** + * @fileoverview Per-account quota remaining-% trajectory chart (`AccountQuotaTrendCard.js`). + * Full-bleed edge-to-edge plot (`x = 1 .. viewW - 1`), X-axis clamped to earliest recorded + * sample when history is shorter than the selected preset, and adaptive integer Y-scale so + * real quota movements never compress into a tiny corner with wasted whitespace. + */ + +import { defineComponent } from '../../core/component.js'; +import { + h, + patchAttr, + patchClasses, + patchKeyedList, + patchStyleVar, + patchText, +} from '../../core/domPatch.js'; +import { formatDateSpanBadge } from '../../core/formatters.js'; +import { SVG_NS, createSvgIcon } from '../../icons/SvgIcon.js'; +import { getEffectiveLocale, t } from '../../i18n/index.js'; +import { CustomDateTimePicker } from '../ui/CustomDateTimePicker.js'; +import { CustomSelect } from '../ui/CustomSelect.js'; +import { SegmentedControl } from '../ui/SegmentedControl.js'; + +const POOL_COLORS = Object.freeze({ + Gemini: 'var(--accent-primary)', + 'Claude & GPT': 'var(--metric-ok)', + Argon: 'var(--ctx-thinking)', + Carbon: 'var(--metric-warn)', +}); + +const POOL_ORDER = ['Gemini', 'Argon', 'Carbon', 'Claude & GPT']; + +let quotaChartUid = 0; + +function floorMinute(ms) { + const n = Number(ms); + if (!(n > 0)) return 0; + const d = new Date(n); + d.setSeconds(0, 0); + return d.getTime(); +} + +function ceilMinute(ms) { + const n = Number(ms); + if (!(n > 0)) return 0; + const d = new Date(n); + d.setSeconds(59, 999); + return d.getTime(); +} + +function pad2(n) { + return String(n).padStart(2, '0'); +} + +function formatAxisTickTime(ms, spanMs) { + const d = new Date(ms); + const hm = `${pad2(d.getHours())}:${pad2(d.getMinutes())}`; + if (spanMs <= 36 * 3600_000) { + return hm; + } + const md = `${d.getMonth() + 1}/${pad2(d.getDate())}`; + if (spanMs <= 7 * 86400_000) { + return `${md} ${pad2(d.getHours())}h`; + } + return md; +} + +function formatHoverTimestamp(ms) { + const d = new Date(ms); + return `${d.getMonth() + 1}/${pad2(d.getDate())} ${pad2(d.getHours())}:${pad2(d.getMinutes())}`; +} + +function buildHorizontalTangentSpline(pts) { + if (pts.length === 0) return ''; + if (pts.length === 1) return `M ${pts[0].x.toFixed(1)} ${pts[0].y.toFixed(1)}`; + let d = `M ${pts[0].x.toFixed(1)} ${pts[0].y.toFixed(1)}`; + for (let i = 0; i < pts.length - 1; i++) { + const p0 = pts[i]; + const p1 = pts[i + 1]; + const dx = (p1.x - p0.x) * 0.38; + d += ` C ${(p0.x + dx).toFixed(1)} ${p0.y.toFixed(1)}, ${(p1.x - dx).toFixed(1)} ${p1.y.toFixed(1)}, ${p1.x.toFixed(1)} ${p1.y.toFixed(1)}`; + } + return d; +} + +/** + * Computes tight, integer-aligned `[yMin, yMid, yMax]` bounds (`0..100%`) around + * observed `[dataMin, dataMax]` so small-to-moderate quota changes use the full + * vertical canvas instead of leaving 80%+ empty bottom space. + */ +function computeAdaptiveQuotaScale(dataMin, dataMax) { + const lo = Math.max(0, Math.min(100, Number.isFinite(dataMin) ? dataMin : 0)); + const hi = Math.max(lo, Math.min(100, Number.isFinite(dataMax) ? dataMax : 100)); + const rawSpan = hi - lo; + const pad = Math.max(1.5, rawSpan * 0.14); + + let yMax = hi >= 98.5 ? 100 : Math.min(100, Math.ceil((hi + pad) / 10) * 10); + let yMin = lo <= 2 ? 0 : Math.max(0, Math.floor((lo - pad) / 10) * 10); + + if (yMax - yMin < 10) { + if (yMax >= 100) { + yMax = 100; + yMin = 90; + } else if (yMin <= 0) { + yMin = 0; + yMax = 10; + } else { + yMin = Math.max(0, yMax - 10); + } + } + // Ensure `(yMax - yMin)` is even so midpoint tick is always an exact integer + if ((yMax - yMin) % 2 !== 0) { + if (yMin > 0) yMin -= 1; + else if (yMax < 100) yMax += 1; + } + const yMid = Math.round((yMin + yMax) / 2); + return { yMin, yMid, yMax, span: Math.max(1, yMax - yMin) }; +} + +const IMPULSE_GLITCH_MAX_SPAN_MS = 20 * 60_000; +const IMPULSE_GLITCH_MIN_JUMP_PCT = 3; +const IMPULSE_GLITCH_MAX_NEIGHBOR_DIFF_PCT = 1.2; +const CYCLE_RESET_JUMP_MIN_PCT = 3.5; + +function stripIsolatedImpulseSpikes(pts) { + if (pts.length < 3) return pts; + const out = pts.slice(); + let i = 1; + while (i < out.length - 1) { + const prev = out[i - 1]; + const cur = out[i]; + const next = out[i + 1]; + const spanMs = next.ts - prev.ts; + if ( + spanMs >= 0 && + spanMs <= IMPULSE_GLITCH_MAX_SPAN_MS && + cur.val - prev.val >= IMPULSE_GLITCH_MIN_JUMP_PCT && + cur.val - next.val >= IMPULSE_GLITCH_MIN_JUMP_PCT && + Math.abs(next.val - prev.val) <= IMPULSE_GLITCH_MAX_NEIGHBOR_DIFF_PCT + ) { + out.splice(i, 1); + if (i > 1) i -= 1; + continue; + } + i += 1; + } + return out; +} + +export function resolveQuotaWindowBounds({ + pools, + selectedRange, + selectedWin, + selectedPool, + customStartMs = 0, + customEndMs = 0, + nowMs = Date.now(), +}) { + if (selectedRange === 'custom') { + return { + startMs: Math.min(customStartMs, customEndMs), + endMs: Math.max(customStartMs, customEndMs), + }; + } + if (selectedRange === 'today') { + const d = new Date(nowMs); + d.setHours(0, 0, 0, 0); + return { startMs: d.getTime(), endMs: nowMs }; + } + if (selectedRange === '24h') return { startMs: nowMs - 24 * 3600_000, endMs: nowMs }; + if (selectedRange === '7d') return { startMs: nowMs - 7 * 86400_000, endMs: nowMs }; + if (selectedRange === '14d') return { startMs: nowMs - 14 * 86400_000, endMs: nowMs }; + if (selectedRange === '30d') return { startMs: nowMs - 30 * 86400_000, endMs: nowMs }; + if (selectedRange === 'all') { + let minTs = Infinity; + for (const p of pools) { + for (const pt of p.points) { + if (pt[0] > 0 && pt[0] < minTs) minTs = pt[0]; + } + } + const safeStart = + Number.isFinite(minTs) && minTs < nowMs - 60_000 ? minTs : nowMs - 5 * 3600_000; + return { startMs: safeStart, endMs: nowMs }; + } + + const targetPools = + selectedPool !== 'all' ? pools.filter((p) => p.poolKey === selectedPool) : pools; + const refPools = targetPools.length ? targetPools : pools; + const isWeekly = selectedWin === 'weekly'; + const winDurationMs = isWeekly ? 7 * 86400_000 : 5 * 3600_000; + + let activeMinResetMs = Infinity; + let anyMinResetMs = Infinity; + for (const p of refPools) { + const rMs = isWeekly ? p.resetWeeklyMs : p.reset5hMs; + const liveVal = isWeekly ? p.liveWk : p.live5h; + if (!(rMs > nowMs)) continue; + if (rMs < anyMinResetMs) anyMinResetMs = rMs; + if (liveVal !== null && liveVal !== undefined && liveVal < 99.95 && rMs < activeMinResetMs) { + activeMinResetMs = rMs; + } + } + const chosenResetMs = Number.isFinite(activeMinResetMs) + ? activeMinResetMs + : Number.isFinite(anyMinResetMs) + ? anyMinResetMs + : 0; + const startMs = chosenResetMs > nowMs ? chosenResetMs - winDurationMs : nowMs - winDurationMs; + return { startMs: Math.min(startMs, nowMs - 60_000), endMs: nowMs }; +} + +/** + * Extracts per-pool trajectories for the requested window, ensuring `'cycle'` mode + * strictly excludes pre-reset points from any previous expired cycle (eliminating + * artificial `84% -> 100%` vertical reset spikes at cycle start). + */ +export function buildQuotaWindowSeries({ + visiblePools, + rawBounds, + selectedRange, + selectedWin, + nowMs = Date.now(), +}) { + const isCycleMode = selectedRange === 'cycle'; + const isWeekly = selectedWin === 'weekly'; + const valIdx = isWeekly ? 2 : 1; + const winDurationMs = isWeekly ? 7 * 86400_000 : 5 * 3600_000; + + const staged = []; + let hasStartAnchor = false; + let earliestInWinTs = Infinity; + + for (const p of visiblePools) { + const cleanedAll = []; + for (let i = 0; i < p.points.length; i++) { + const pt = p.points[i]; + const v = pt[valIdx]; + if (v === null || v === undefined || !Number.isFinite(Number(v))) continue; + cleanedAll.push({ + ts: pt[0], + val: Math.max(0, Math.min(100, Number(v))), + }); + } + const sanitized = stripIsolatedImpulseSpikes(cleanedAll); + if (sanitized.length === 0) continue; + + const pResetMs = isWeekly ? p.resetWeeklyMs : p.reset5hMs; + const pLiveVal = isWeekly ? p.liveWk : p.live5h; + const poolCutoffMs = + isCycleMode && pResetMs > nowMs && pLiveVal !== null && pLiveVal < 99.95 + ? Math.max(rawBounds.startMs, pResetMs - winDurationMs) + : rawBounds.startMs; + + let carryBefore = null; + let inWinPts = []; + for (let i = 0; i < sanitized.length; i++) { + const sp = sanitized[i]; + if (sp.ts <= poolCutoffMs) { + carryBefore = sp; + } else if (sp.ts <= rawBounds.endMs) { + inWinPts.push(sp); + } + } + + // In `'cycle'` mode, `< 99.95%` observations at or before `poolCutoffMs` belong + // to the previous expired cycle and must never carry forward into the new cycle. + if (isCycleMode && carryBefore && carryBefore.val < 99.95) { + carryBefore = null; + } + + let hadResetInWindow = false; + if (isCycleMode && inWinPts.length >= 2) { + let lastResetIdx = 0; + for (let k = 1; k < inWinPts.length; k++) { + const prev = inWinPts[k - 1].val; + const cur = inWinPts[k].val; + if (cur - prev >= CYCLE_RESET_JUMP_MIN_PCT || (cur >= 99.5 && prev <= 98.5)) { + lastResetIdx = k; + } + } + if (lastResetIdx > 0) { + inWinPts = inWinPts.slice(lastResetIdx); + carryBefore = null; + hadResetInWindow = true; + } + } + + const isMidWindowReset = hadResetInWindow && inWinPts[0].ts - poolCutoffMs > 5 * 60_000; + const anchorsCycleOrigin = + isCycleMode && + !isMidWindowReset && + !carryBefore && + inWinPts.length > 0 && + inWinPts[0].ts - poolCutoffMs <= 3 * 60_000; + + if (carryBefore || (anchorsCycleOrigin && poolCutoffMs <= rawBounds.startMs + 5000)) { + hasStartAnchor = true; + } + if (inWinPts.length > 0 && inWinPts[0].ts < earliestInWinTs) { + earliestInWinTs = inWinPts[0].ts; + } + + staged.push({ + pool: p, + poolCutoffMs, + carryBefore, + inWinPts, + isMidWindowReset, + anchorsCycleOrigin, + }); + } + + let plottedStartMs = rawBounds.startMs; + if ( + !hasStartAnchor && + selectedRange !== 'custom' && + Number.isFinite(earliestInWinTs) && + earliestInWinTs > rawBounds.startMs + ) { + plottedStartMs = + rawBounds.endMs - earliestInWinTs >= 60_000 + ? earliestInWinTs + : Math.max(rawBounds.startMs, rawBounds.endMs - 5 * 60_000); + } + const plottedEndMs = Math.max(plottedStartMs + 60_000, rawBounds.endMs); + + const rawBuilt = []; + let dataMin = Infinity; + let dataMax = -Infinity; + + for (const item of staged) { + const { pool, poolCutoffMs, carryBefore, inWinPts, isMidWindowReset, anchorsCycleOrigin } = item; + const seriesPts = []; + const anchorStartTs = Math.max(plottedStartMs, poolCutoffMs); + + if (carryBefore) { + seriesPts.push({ ts: anchorStartTs, val: carryBefore.val }); + } else if (anchorsCycleOrigin) { + const originVal = Math.max(100, inWinPts[0].val); + if (inWinPts[0].ts > anchorStartTs) { + seriesPts.push({ ts: anchorStartTs, val: Math.min(100, originVal) }); + } + } else if ( + !isMidWindowReset && + inWinPts.length > 0 && + inWinPts[0].ts > plottedStartMs && + (inWinPts.length === 1 || inWinPts[0].val >= 99.95) + ) { + seriesPts.push({ ts: plottedStartMs, val: inWinPts[0].val }); + } + + for (let i = 0; i < inWinPts.length; i++) { + const vp = inWinPts[i]; + if (vp.ts >= plottedStartMs) { + seriesPts.push(vp); + } + } + + if (seriesPts.length > 0 && plottedEndMs <= nowMs + 60_000) { + const tail = seriesPts[seriesPts.length - 1]; + const tailEnd = Math.min(plottedEndMs, nowMs); + if (tailEnd > tail.ts + 1000) { + seriesPts.push({ ts: tailEnd, val: tail.val }); + } + } + if (seriesPts.length === 0) continue; + + for (let i = 0; i < seriesPts.length; i++) { + const v = seriesPts[i].val; + if (v < dataMin) dataMin = v; + if (v > dataMax) dataMax = v; + } + rawBuilt.push({ + poolKey: pool.poolKey, + label: pool.label, + color: POOL_COLORS[pool.poolKey] || 'var(--accent-primary)', + rawPoints: seriesPts, + latestVal: seriesPts[seriesPts.length - 1].val, + }); + } + + return { + plottedStartMs, + plottedEndMs, + rawBuilt, + yScale: rawBuilt.length > 0 ? computeAdaptiveQuotaScale(dataMin, dataMax) : null, + }; +} + +function getTimeRangeOptions() { + return [ + { value: 'cycle', label: t('cons.rangeCycle') }, + { value: 'today', label: t('cons.rangeToday') }, + { value: '24h', label: t('cons.range24h') }, + { value: '7d', label: t('cons.range7d') }, + { value: '14d', label: t('cons.range14d') }, + { value: '30d', label: t('cons.range30d') }, + { value: 'all', label: t('cons.rangeAll') }, + { value: 'custom', label: t('cons.rangeCustom') }, + ]; +} + +function getQuotaWindowOptions() { + return [ + { value: '5h', label: t('settings.account.win5h') }, + { value: 'weekly', label: t('settings.account.winWeekly') }, + ]; +} + +export const AccountQuotaTrendCard = defineComponent({ + name: 'AccountQuotaTrendCard', + setup({ onCleanup }) { + const VIEW_H = 184; + const TOP_Y = 18; + const BOTTOM_Y = 166; + const SPAN_Y = BOTTOM_Y - TOP_Y; + const LEFT_X = 1; + const RIGHT_PAD = 1; + + let viewW = 460; + let selectedRange = 'cycle'; + let selectedWin = '5h'; + let selectedPool = 'all'; + let selectedAccountEmail = ''; + + const initNow = Date.now(); + let customStartMs = floorMinute(initNow - 24 * 3600_000); + let customEndMs = ceilMinute(initNow); + let draftStartMs = customStartMs; + let draftEndMs = customEndMs; + + let latestState = { + activeEmail: '', + activePools: [], + riskAccepted: false, + accounts: [], + savedQuotas: new Map(), + quotaHistory: {}, + }; + + let plottedSeries = []; + let plottedStartMs = 0; + let plottedEndMs = 0; + let plottedYScale = { yMin: 0, yMid: 50, yMax: 100, span: 100 }; + + const cardTitleText = h('span', { text: t('settings.account.trendTitle') }); + const spanBadgeEl = h('span', { className: 'cons-badge text-mono', text: '' }); + + const accountSelect = CustomSelect({ + className: 'cons-cycle-pool-select account-trend-email-select', + modelValue: '', + options: [], + onUpdateModelValue: (val) => { + selectedAccountEmail = String(val || '').trim().toLowerCase(); + renderChart(); + }, + }); + patchAttr(accountSelect.el, 'hidden', true); + + const poolSelect = CustomSelect({ + className: 'cons-cycle-pool-select', + modelValue: 'all', + options: [{ value: 'all', label: t('cons.cyclePoolAll') }], + onUpdateModelValue: (val) => { + selectedPool = val || 'all'; + renderChart(); + }, + }); + + const winSwitch = SegmentedControl({ + ariaLabel: t('settings.account.trendWinAria'), + modelValue: selectedWin, + options: getQuotaWindowOptions(), + onUpdateModelValue: (val) => { + selectedWin = val === 'weekly' ? 'weekly' : '5h'; + winSwitch.setProps({ modelValue: selectedWin, options: getQuotaWindowOptions() }); + renderChart(); + }, + }); + + const rangeSwitch = SegmentedControl({ + ariaLabel: t('cons.rangeAria'), + modelValue: selectedRange, + options: getTimeRangeOptions(), + onUpdateModelValue: (nextRange) => { + selectedRange = nextRange || 'cycle'; + if (selectedRange === 'custom') { + startPicker.setValue(draftStartMs); + endPicker.setValue(draftEndMs); + } + rangeSwitch.setProps({ modelValue: selectedRange, options: getTimeRangeOptions() }); + renderChart(); + }, + }); + + const startPicker = CustomDateTimePicker({ + modelValue: draftStartMs, + onUpdateModelValue: (ms) => { + draftStartMs = ms; + startPicker.setValue(ms); + }, + }); + const endPicker = CustomDateTimePicker({ + modelValue: draftEndMs, + onUpdateModelValue: (ms) => { + draftEndMs = ms; + endPicker.setValue(ms); + }, + }); + const applyCustomLabel = h('span', { text: t('cons.applyFilter') }); + const applyCustomBtn = h( + 'button', + { + type: 'button', + className: 'btn-primary', + onClick: () => { + const s = Number(draftStartMs || 0); + const e = Number(draftEndMs || 0); + const lo = floorMinute(s > 0 && e > 0 ? Math.min(s, e) : s); + const hi = ceilMinute(s > 0 && e > 0 ? Math.max(s, e) : e); + if (lo > 0 && hi > lo) { + customStartMs = lo; + customEndMs = hi; + draftStartMs = lo; + draftEndMs = hi; + startPicker.setValue(lo); + endPicker.setValue(hi); + renderChart(); + } + }, + }, + [createSvgIcon('check', { size: 12 }), applyCustomLabel] + ); + const rangeToEl = h('span', { className: 'text-muted', text: t('cons.rangeTo') }); + const customRangeBar = h('div', { className: 'cons-custom-range-bar', hidden: true }, [ + startPicker.el, + rangeToEl, + endPicker.el, + applyCustomBtn, + ]); + + // --- Full-Bleed Edge-to-Edge 184px SVG Stage (reusing TrendAreaChart `.trend-svg`) --- + const uid = ++quotaChartUid; + const gradId = `acct-quota-grad-${uid}`; + const svg = document.createElementNS(SVG_NS, 'svg'); + svg.setAttribute('viewBox', `0 0 ${viewW} ${VIEW_H}`); + svg.setAttribute('class', 'trend-svg'); + svg.setAttribute('aria-hidden', 'true'); + + const defs = document.createElementNS(SVG_NS, 'defs'); + const grad = document.createElementNS(SVG_NS, 'linearGradient'); + grad.setAttribute('id', gradId); + grad.setAttribute('x1', '0'); + grad.setAttribute('y1', '0'); + grad.setAttribute('x2', '0'); + grad.setAttribute('y2', '1'); + const stopTop = document.createElementNS(SVG_NS, 'stop'); + stopTop.setAttribute('offset', '0%'); + stopTop.setAttribute('stop-color', 'var(--accent-primary)'); + stopTop.setAttribute('stop-opacity', '0.24'); + const stopBot = document.createElementNS(SVG_NS, 'stop'); + stopBot.setAttribute('offset', '100%'); + stopBot.setAttribute('stop-color', 'var(--accent-primary)'); + stopBot.setAttribute('stop-opacity', '0.0'); + grad.appendChild(stopTop); + grad.appendChild(stopBot); + defs.appendChild(grad); + svg.appendChild(defs); + + const gridGroup = document.createElementNS(SVG_NS, 'g'); + gridGroup.setAttribute('class', 'trend-grid'); + const gridRowSpecs = [ + { y: TOP_Y, dy: 3, labeled: true }, + { y: Math.round(TOP_Y + SPAN_Y * 0.25), dy: 0, labeled: false }, + { y: Math.round(TOP_Y + SPAN_Y * 0.5), dy: 3, labeled: true }, + { y: Math.round(TOP_Y + SPAN_Y * 0.75), dy: 0, labeled: false }, + { y: BOTTOM_Y, dy: -3, labeled: true }, + ]; + const gridLines = []; + const yLabels = []; + for (const row of gridRowSpecs) { + const line = document.createElementNS(SVG_NS, 'line'); + line.setAttribute('x1', '0'); + line.setAttribute('y1', String(row.y)); + line.setAttribute('x2', String(viewW)); + line.setAttribute('y2', String(row.y)); + gridGroup.appendChild(line); + gridLines.push(line); + + if (row.labeled) { + const lbl = document.createElementNS(SVG_NS, 'text'); + lbl.setAttribute('class', 'sstep-axis-label is-y-inline'); + lbl.setAttribute('x', '3'); + lbl.setAttribute('y', String(row.y + row.dy)); + lbl.setAttribute('text-anchor', 'start'); + yLabels.push(lbl); + } + } + svg.appendChild(gridGroup); + + const seriesGroup = document.createElementNS(SVG_NS, 'g'); + svg.appendChild(seriesGroup); + + // Append inline Y labels above series fills so halo stroke keeps text crisp + const yLabelOverlay = document.createElementNS(SVG_NS, 'g'); + for (const lbl of yLabels) { + yLabelOverlay.appendChild(lbl); + } + svg.appendChild(yLabelOverlay); + + const hoverLine = document.createElementNS(SVG_NS, 'line'); + hoverLine.setAttribute('class', 'trend-hover-line'); + hoverLine.setAttribute('y1', String(TOP_Y)); + hoverLine.setAttribute('y2', String(BOTTOM_Y)); + hoverLine.setAttribute('hidden', ''); + svg.appendChild(hoverLine); + + const hoverDotsGroup = document.createElementNS(SVG_NS, 'g'); + hoverDotsGroup.setAttribute('hidden', ''); + svg.appendChild(hoverDotsGroup); + + const tipTimeEl = h('span', { className: 'trend-tip-time text-mono' }); + const tipWinEl = h('span', { className: 'trend-tip-calls' }); + const tipRowsHost = h('div', { className: 'account-trend-tip-rows' }); + const tooltipEl = h('div', { className: 'trend-tooltip', hidden: true }, [ + h('div', { className: 'trend-tip-head' }, [tipTimeEl, tipWinEl]), + tipRowsHost, + ]); + + const xAxisEl = h('div', { className: 'trend-x-axis' }); + const legendRowEl = h('div', { className: 'account-trend-legend' }); + const emptyStateEl = h('div', { className: 'smodel-empty settings-action-desc', hidden: true }); + + const plotStage = h( + 'div', + { + className: 'trend-plot-stage', + onPointermove: handlePointerMove, + onPointerleave: hideHover, + }, + [svg, tooltipEl] + ); + const chartWrap = h('div', { className: 'trend-chart-wrap' }, [ + plotStage, + xAxisEl, + legendRowEl, + ]); + + const rootEl = h('section', { className: 'panel account-quota-trend-panel' }, [ + h('div', { className: 'panel-header account-trend-toprow' }, [ + h('span', { className: 'panel-title cons-overview-title' }, [ + createSvgIcon('pulse', { size: 13 }), + cardTitleText, + accountSelect.el, + poolSelect.el, + ]), + winSwitch.el, + ]), + h('div', { className: 'panel-header cons-overview-head account-trend-subbar' }, [ + spanBadgeEl, + h('div', { className: 'cons-overview-controls' }, [rangeSwitch.el]), + ]), + customRangeBar, + chartWrap, + emptyStateEl, + ]); + + function valueToSvgY(val) { + const clamped = Math.max(plottedYScale.yMin, Math.min(plottedYScale.yMax, val)); + return BOTTOM_Y - ((clamped - plottedYScale.yMin) / plottedYScale.span) * SPAN_Y; + } + + function hideHover() { + hoverLine.setAttribute('hidden', ''); + hoverDotsGroup.setAttribute('hidden', ''); + patchAttr(tooltipEl, 'hidden', true); + } + + function sampleSeriesValueAt(points, targetMs) { + if (!points.length) return null; + if (targetMs < points[0].ts - 1000) return null; + if (targetMs <= points[0].ts) return points[0].val; + const last = points[points.length - 1]; + if (targetMs > last.ts + 60_000) return null; + if (targetMs >= last.ts) return last.val; + let lo = 0; + let hi = points.length - 1; + while (lo + 1 < hi) { + const mid = (lo + hi) >> 1; + if (points[mid].ts <= targetMs) lo = mid; + else hi = mid; + } + const a = points[lo]; + const b = points[hi]; + const span = b.ts - a.ts; + if (span <= 0) return b.val; + return a.val + (b.val - a.val) * ((targetMs - a.ts) / span); + } + + function handlePointerMove(ev) { + if (!plottedSeries.length || !(plottedEndMs > plottedStartMs)) { + hideHover(); + return; + } + const rect = svg.getBoundingClientRect?.(); + const widthPx = Number(rect?.width || viewW); + if (!(widthPx > 0)) return; + const offsetX = Number(ev.clientX || 0) - Number(rect?.left || 0); + const svgX = (offsetX / widthPx) * viewW; + const plotW = Math.max(20, viewW - LEFT_X - RIGHT_PAD); + const u = Math.max(0, Math.min(1, (svgX - LEFT_X) / plotW)); + const clampedX = LEFT_X + u * plotW; + const targetMs = Math.round(plottedStartMs + u * (plottedEndMs - plottedStartMs)); + + hoverLine.setAttribute('x1', clampedX.toFixed(1)); + hoverLine.setAttribute('x2', clampedX.toFixed(1)); + + while (hoverDotsGroup.firstChild) hoverDotsGroup.removeChild(hoverDotsGroup.firstChild); + const tipItems = []; + for (let i = 0; i < plottedSeries.length; i++) { + const s = plottedSeries[i]; + const val = sampleSeriesValueAt(s.rawPoints, targetMs); + if (val === null) continue; + const y = valueToSvgY(val); + const dot = document.createElementNS(SVG_NS, 'circle'); + dot.setAttribute('cx', clampedX.toFixed(1)); + dot.setAttribute('cy', y.toFixed(1)); + dot.setAttribute('r', '3.2'); + dot.setAttribute('fill', 'var(--surface-panel)'); + dot.setAttribute('stroke', s.color); + dot.setAttribute('stroke-width', '1.8'); + hoverDotsGroup.appendChild(dot); + tipItems.push({ key: s.poolKey, label: s.label, color: s.color, val }); + } + if (tipItems.length === 0) { + hideHover(); + return; + } + hoverLine.removeAttribute('hidden'); + hoverDotsGroup.removeAttribute('hidden'); + + patchText(tipTimeEl, formatHoverTimestamp(targetMs)); + patchText(tipWinEl, t(selectedWin === 'weekly' ? 'settings.account.winWeekly' : 'settings.account.win5h')); + patchKeyedList(tipRowsHost, tipItems, { + keyFn: (it) => it.key, + sigFn: (it) => `${it.label}:${it.val.toFixed(1)}`, + createRow: () => { + const sw = h('span', { className: 'dot-swatch' }); + const nm = h('span', { className: 'account-trend-tip-name' }); + const vl = h('strong', { className: 'text-mono' }); + const row = h('div', { className: 'account-trend-tip-row' }, [ + h('span', { className: 'legend-left' }, [sw, nm]), + vl, + ]); + return { el: row, refs: { sw, nm, vl } }; + }, + updateRow: (r, it) => { + r.sw.style.background = it.color; + patchText(r.nm, it.label); + patchText(r.vl, `${+it.val.toFixed(1)}%`); + }, + }); + + const pctX = (clampedX / viewW) * 100; + const shift = pctX < 24 ? '0%' : pctX > 76 ? '-100%' : '-50%'; + patchStyleVar(tooltipEl, '--tip-x', `${pctX.toFixed(1)}%`); + patchStyleVar(tooltipEl, '--tip-shift', shift); + patchAttr(tooltipEl, 'hidden', false); + } + + function resolveTargetAccountEmail() { + const { activeEmail, riskAccepted, accounts } = latestState; + if (!riskAccepted) { + return activeEmail || ''; + } + const candidates = []; + if (activeEmail) candidates.push(activeEmail); + for (const addr of accounts) { + if (addr && !candidates.includes(addr)) candidates.push(addr); + } + if (selectedAccountEmail && candidates.includes(selectedAccountEmail)) { + return selectedAccountEmail; + } + selectedAccountEmail = candidates[0] || ''; + return selectedAccountEmail; + } + + function buildMergedPoolsForEmail(email, nowMs) { + const histPools = latestState.quotaHistory?.[email]?.pools || {}; + const livePools = + email && email === latestState.activeEmail + ? latestState.activePools || [] + : latestState.savedQuotas.get(email)?.pools || []; + + const merged = new Map(); + for (const [pk, hEntry] of Object.entries(histPools)) { + merged.set(pk, { + poolKey: pk, + label: hEntry.label || pk, + reset5hMs: Number(hEntry.reset5hMs) || 0, + resetWeeklyMs: Number(hEntry.resetWeeklyMs) || 0, + points: Array.isArray(hEntry.points) ? hEntry.points.slice() : [], + live5h: null, + liveWk: null, + }); + } + + for (const lp of livePools) { + const pk = String(lp?.poolKey || lp?.family || lp?.poolLabel || '').trim(); + if (!pk || pk === 'Unknown') continue; + let item = merged.get(pk); + if (!item) { + item = { + poolKey: pk, + label: String(lp?.poolLabel || pk), + reset5hMs: 0, + resetWeeklyMs: 0, + points: [], + live5h: null, + liveWk: null, + }; + merged.set(pk, item); + } + if (lp?.poolLabel) item.label = String(lp.poolLabel); + const r5Ms = Date.parse(lp?.resetTime || ''); + const rWMs = Date.parse(lp?.weeklyResetTime || ''); + if (Number.isFinite(r5Ms) && r5Ms > 0) item.reset5hMs = r5Ms; + if (Number.isFinite(rWMs) && rWMs > 0) item.resetWeeklyMs = rWMs; + if (Number.isFinite(lp?.remainingPct)) item.live5h = +Number(lp.remainingPct).toFixed(1); + if (Number.isFinite(lp?.weeklyRemainingPct)) { + item.liveWk = +Number(lp.weeklyRemainingPct).toFixed(1); + } + if (item.live5h !== null || item.liveWk !== null) { + const last = item.points[item.points.length - 1]; + if (!last || nowMs >= last[0]) { + item.points.push([ + nowMs, + item.live5h ?? (last ? last[1] : null), + item.liveWk ?? (last ? last[2] : null), + ]); + } + } + } + + return Array.from(merged.values()).sort( + (a, b) => + (POOL_ORDER.indexOf(a.poolKey) === -1 ? 99 : POOL_ORDER.indexOf(a.poolKey)) - + (POOL_ORDER.indexOf(b.poolKey) === -1 ? 99 : POOL_ORDER.indexOf(b.poolKey)) + ); + } + + function renderChart() { + hideHover(); + const nowMs = Date.now(); + void getEffectiveLocale(); + + patchText(cardTitleText, t('settings.account.trendTitle')); + patchText(rangeToEl, t('cons.rangeTo')); + patchText(applyCustomLabel, t('cons.applyFilter')); + winSwitch.setProps({ modelValue: selectedWin, options: getQuotaWindowOptions() }); + rangeSwitch.setProps({ modelValue: selectedRange, options: getTimeRangeOptions() }); + patchAttr(customRangeBar, 'hidden', selectedRange !== 'custom'); + + const targetEmail = resolveTargetAccountEmail(); + const candidateEmails = []; + if (latestState.activeEmail) candidateEmails.push(latestState.activeEmail); + if (latestState.riskAccepted) { + for (const addr of latestState.accounts) { + if (addr && !candidateEmails.includes(addr)) candidateEmails.push(addr); + } + } + const showAccountPicker = latestState.riskAccepted && candidateEmails.length > 1; + patchAttr(accountSelect.el, 'hidden', !showAccountPicker); + if (showAccountPicker) { + accountSelect.setProps({ + modelValue: targetEmail, + options: candidateEmails.map((addr) => ({ + value: addr, + label: addr.split('@')[0] || addr, + title: addr, + })), + }); + } + + const mergedPools = targetEmail ? buildMergedPoolsForEmail(targetEmail, nowMs) : []; + if (selectedPool !== 'all' && !mergedPools.some((p) => p.poolKey === selectedPool)) { + selectedPool = 'all'; + } + const poolOpts = [ + { + value: 'all', + label: t('cons.cyclePoolAll'), + }, + ...mergedPools.map((p) => ({ + value: p.poolKey, + label: p.poolKey === 'Claude & GPT' ? 'Claude/GPT' : p.label, + title: p.label, + })), + ]; + poolSelect.setProps({ modelValue: selectedPool, options: poolOpts }); + patchAttr(poolSelect.el, 'hidden', mergedPools.length <= 1); + + const rawBounds = resolveQuotaWindowBounds({ + pools: mergedPools, + selectedRange, + selectedWin, + selectedPool, + customStartMs, + customEndMs, + nowMs, + }); + const visiblePools = + selectedPool === 'all' + ? mergedPools + : mergedPools.filter((p) => p.poolKey === selectedPool); + + const built = buildQuotaWindowSeries({ + visiblePools, + rawBounds, + selectedRange, + selectedWin, + nowMs, + }); + plottedStartMs = built.plottedStartMs; + plottedEndMs = built.plottedEndMs; + const spanMs = plottedEndMs - plottedStartMs; + patchText(spanBadgeEl, formatDateSpanBadge(plottedStartMs, plottedEndMs)); + + const rawBuilt = built.rawBuilt; + const hasPlot = rawBuilt.length > 0; + patchAttr(chartWrap, 'hidden', !hasPlot); + patchAttr(emptyStateEl, 'hidden', hasPlot); + if (!hasPlot) { + plottedSeries = []; + patchText(emptyStateEl, t('settings.account.trendEmpty')); + return; + } + + plottedYScale = built.yScale; + yLabels[0].textContent = `${plottedYScale.yMax}%`; + yLabels[1].textContent = `${plottedYScale.yMid}%`; + yLabels[2].textContent = `${plottedYScale.yMin}%`; + + const plotW = Math.max(20, viewW - LEFT_X - RIGHT_PAD); + svg.setAttribute('viewBox', `0 0 ${viewW} ${VIEW_H}`); + for (const gl of gridLines) { + gl.setAttribute('x2', String(viewW)); + } + + plottedSeries = rawBuilt.map((rb) => ({ + ...rb, + svgPts: rb.rawPoints.map((sp) => { + const u = Math.max(0, Math.min(1, (sp.ts - plottedStartMs) / spanMs)); + return { + x: LEFT_X + u * plotW, + y: valueToSvgY(sp.val), + val: sp.val, + ts: sp.ts, + }; + }), + })); + + while (seriesGroup.firstChild) seriesGroup.removeChild(seriesGroup.firstChild); + for (let i = 0; i < plottedSeries.length; i++) { + const s = plottedSeries[i]; + const lineD = buildHorizontalTangentSpline(s.svgPts); + if (s.svgPts.length >= 2) { + const first = s.svgPts[0]; + const last = s.svgPts[s.svgPts.length - 1]; + const areaEl = document.createElementNS(SVG_NS, 'path'); + areaEl.setAttribute( + 'd', + `${lineD} L ${last.x.toFixed(1)} ${BOTTOM_Y} L ${first.x.toFixed(1)} ${BOTTOM_Y} Z` + ); + if (plottedSeries.length === 1) { + stopTop.setAttribute('stop-color', s.color); + stopBot.setAttribute('stop-color', s.color); + areaEl.setAttribute('fill', `url(#${gradId})`); + } else { + areaEl.setAttribute('class', 'sstep-multi-area'); + areaEl.style.setProperty('--sstep-series-color', s.color); + } + seriesGroup.appendChild(areaEl); + } + const pathEl = document.createElementNS(SVG_NS, 'path'); + pathEl.setAttribute('d', lineD); + pathEl.setAttribute('fill', 'none'); + pathEl.setAttribute('stroke', s.color); + pathEl.setAttribute('stroke-width', '1.9'); + pathEl.setAttribute('stroke-linecap', 'round'); + seriesGroup.appendChild(pathEl); + + const endPt = s.svgPts[s.svgPts.length - 1]; + const endDot = document.createElementNS(SVG_NS, 'circle'); + endDot.setAttribute('cx', endPt.x.toFixed(1)); + endDot.setAttribute('cy', endPt.y.toFixed(1)); + endDot.setAttribute('r', '2.7'); + endDot.setAttribute('fill', s.color); + seriesGroup.appendChild(endDot); + } + + // Edge-to-edge X-axis ticks (`0% .. 100%`) + const tickCount = 5; + const ticks = []; + for (let k = 0; k < tickCount; k++) { + const ratio = k / (tickCount - 1); + const tickMs = Math.round(plottedStartMs + ratio * spanMs); + const leftPct = ratio * 100; + ticks.push({ + idx: k, + leftPct, + label: formatAxisTickTime(tickMs, spanMs), + edge: k === 0 ? 'start' : k === tickCount - 1 ? 'end' : '', + }); + } + patchKeyedList(xAxisEl, ticks, { + keyFn: (tk) => tk.idx, + sigFn: (tk) => `${tk.leftPct.toFixed(1)}:${tk.label}:${tk.edge}`, + createRow: () => { + const el = h('span', { className: 'trend-x-tick text-mono' }); + return { el, refs: { el } }; + }, + updateRow: (r, tk) => { + patchText(r.el, tk.label); + patchStyleVar(r.el, '--tick-x', `${tk.leftPct.toFixed(2)}%`); + patchClasses(r.el, { + 'is-start': tk.edge === 'start', + 'is-end': tk.edge === 'end', + }); + }, + }); + + patchKeyedList(legendRowEl, plottedSeries, { + keyFn: (s) => s.poolKey, + sigFn: (s) => `${s.label}:${s.latestVal.toFixed(1)}:${s.color}`, + createRow: () => { + const sw = h('span', { className: 'dot-swatch' }); + const nm = h('span', { className: 'account-trend-leg-name' }); + const vl = h('strong', { className: 'text-mono' }); + const el = h('span', { className: 'account-trend-leg-item' }, [sw, nm, vl]); + return { el, refs: { sw, nm, vl } }; + }, + updateRow: (r, s) => { + r.sw.style.background = s.color; + patchText(r.nm, s.label); + patchText(r.vl, `${+s.latestVal.toFixed(1)}%`); + }, + }); + } + + let resizeObs = null; + if (typeof ResizeObserver === 'function') { + resizeObs = new ResizeObserver((entries) => { + const w = Math.floor(Number(entries?.[0]?.contentRect?.width || 0)); + if (w >= 180 && Math.abs(w - viewW) >= 2) { + viewW = w; + renderChart(); + } + }); + resizeObs.observe(plotStage); + } + + onCleanup(() => { + resizeObs?.disconnect(); + accountSelect.destroy(); + poolSelect.destroy(); + winSwitch.destroy(); + rangeSwitch.destroy(); + startPicker.destroy(); + endPicker.destroy(); + }); + + function update(nextState) { + latestState = { ...latestState, ...nextState }; + renderChart(); + } + + renderChart(); + return { + el: rootEl, + refs: { update, refreshLabels: renderChart }, + }; + }, +}); diff --git a/sidecars/panel/src/i18n/en-US.js b/sidecars/panel/src/i18n/en-US.js index 2aafe9e..92f3d91 100644 --- a/sidecars/panel/src/i18n/en-US.js +++ b/sidecars/panel/src/i18n/en-US.js @@ -598,6 +598,13 @@ export const I18N_MESSAGES_EN_US = Object.freeze({ 'settings.account.passwordRateLimited': 'Too many failed attempts. Wait a moment and try again.', 'settings.account.vaultDecryptFailed': 'Vault decryption check failed.', 'settings.account.invalidActionNonce': 'Security token expired. Please try again.', + 'settings.account.riskNotAccepted': 'Acknowledge the unofficial feature risk warning first.', + 'settings.account.riskBody': 'Google has not authorized importing, storing, or switching Antigravity credentials or polling multi-account quotas in Antigravity App / IDE. Enabling multi-account management may get your Antigravity access, GCP services, or Google account disabled or banned; enabling this feature means you assume all associated risks. Make sure you know what you are doing!', + 'settings.account.trendTitle': 'Quota Trend', + 'settings.account.trendWinAria': 'Quota window selector', + 'settings.account.win5h': '5 Hours', + 'settings.account.winWeekly': 'Weekly', + 'settings.account.trendEmpty': 'No quota history recorded in the selected time range.', 'settings.subTab.aria': 'Preferences category tabs', 'settings.saving': 'Saving…', 'settings.saved': 'Saved', diff --git a/sidecars/panel/src/i18n/zh-CN.js b/sidecars/panel/src/i18n/zh-CN.js index 26911b4..c594b72 100644 --- a/sidecars/panel/src/i18n/zh-CN.js +++ b/sidecars/panel/src/i18n/zh-CN.js @@ -599,6 +599,13 @@ export const I18N_MESSAGES_ZH_CN = Object.freeze({ 'settings.account.passwordRateLimited': '密码连续输错次数过多,请稍候再试。', 'settings.account.vaultDecryptFailed': '凭证解密校验失败,请确认密码或重置凭证库。', 'settings.account.invalidActionNonce': '安全校验令牌已过期,请重试。', + 'settings.account.riskNotAccepted': '请先确认并接受非官方能力风险提示。', + 'settings.account.riskBody': 'Google 官方并未授权在 Antigravity App / IDE 中通过第三方插件导入、保存、切换账号凭证或后台查询多账号配额。擅自启用可能导致 Antigravity 或 GCP 服务被禁用,甚至触发账号封禁;启用该功能即默认由你自行承担全部风险。你最好清楚自己在做什么!', + 'settings.account.trendTitle': '额度变化', + 'settings.account.trendWinAria': '配额窗口切换', + 'settings.account.win5h': '5 小时', + 'settings.account.winWeekly': '每周', + 'settings.account.trendEmpty': '当前时间范围内暂无配额记录。', 'settings.saving': '保存中…', 'settings.saved': '已保存', 'settings.saveFailed': '保存失败', diff --git a/sidecars/panel/src/store/telemetryStore.js b/sidecars/panel/src/store/telemetryStore.js index 355d4aa..709360e 100644 --- a/sidecars/panel/src/store/telemetryStore.js +++ b/sidecars/panel/src/store/telemetryStore.js @@ -662,6 +662,9 @@ export function isEquivalentTelemetry(prev, next) { ) { return false; } + if (Boolean(pSet?.accountSwitchRiskAccepted) !== Boolean(nSet?.accountSwitchRiskAccepted)) { + return false; + } if (!sameArr(pSet?.topbarCapsuleSlots, nSet?.topbarCapsuleSlots)) return false; if (!sameArr(pSet?.hoverModules, nSet?.hoverModules)) return false; if (!sameArr(pSet?.overviewModules, nSet?.overviewModules)) return false; @@ -1926,6 +1929,9 @@ export async function saveSettingsPatch(patch) { safePatch.accountQuotaRefreshMinutes ); } + if (Object.prototype.hasOwnProperty.call(safePatch, 'accountSwitchRiskAccepted')) { + safePatch.accountSwitchRiskAccepted = Boolean(safePatch.accountSwitchRiskAccepted); + } if (Object.prototype.hasOwnProperty.call(safePatch, 'topbarCapsuleSlots')) { safePatch.topbarCapsuleSlots = normalizeClientTopbarSlots(safePatch.topbarCapsuleSlots); } diff --git a/sidecars/panel/src/styles/views-settings.css b/sidecars/panel/src/styles/views-settings.css index 68f0fe0..e7a6c61 100644 --- a/sidecars/panel/src/styles/views-settings.css +++ b/sidecars/panel/src/styles/views-settings.css @@ -646,15 +646,15 @@ } .account-toolbar-panel { - padding: 9px 12px; - gap: 8px; + padding: var(--space-3) var(--space-5); + gap: var(--space-2); } .account-toolbar-row { display: flex; align-items: center; justify-content: space-between; - gap: 8px; + gap: var(--space-2); flex-wrap: wrap; } @@ -662,6 +662,7 @@ .account-toolbar-right { display: inline-flex; align-items: center; + flex-wrap: wrap; gap: 6px; min-width: 0; } @@ -670,32 +671,19 @@ margin-left: auto; } -.account-toolbar-btn { - display: inline-flex; - align-items: center; - gap: 5px; - min-height: calc(25px * var(--ui-font-scale, 1)); - height: auto; +.account-toolbar-panel .btn-primary, +.account-toolbar-panel .btn-secondary { + min-height: calc(26px * var(--ui-font-scale, 1)); padding: 2px 10px; font-size: var(--fs-md); - font-weight: 500; - border-radius: var(--radius-xs); white-space: nowrap; } -.account-toolbar-btn.is-open { +.account-toolbar-panel .btn-secondary.is-open { color: var(--accent-primary); background: var(--accent-primary-soft); } -.account-reload-btn { - width: calc(25px * var(--ui-font-scale, 1)); - height: calc(25px * var(--ui-font-scale, 1)); - padding: 0; - border-radius: var(--radius-xs); - flex-shrink: 0; -} - .account-refresh-select { flex: 0 0 auto; width: auto; @@ -704,24 +692,11 @@ .account-refresh-select .select-trigger { width: auto; - min-height: calc(25px * var(--ui-font-scale, 1)); + min-height: calc(26px * var(--ui-font-scale, 1)); height: auto; padding: 2px 8px; gap: 5px; font-size: var(--fs-sm); - border: 1px solid var(--border-hairline); - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -[data-modern-ui="true"] .account-refresh-select .select-trigger { - border: none; - background: var(--modern-surface-subtle); -} - -[data-modern-ui="true"] .account-refresh-select .select-trigger:hover, -[data-modern-ui="true"] .account-refresh-select.is-open .select-trigger { - background: var(--modern-surface-hover); } .account-refresh-select .select-menu { @@ -748,17 +723,18 @@ color: var(--metric-danger); } -.account-status-inline.is-info { - color: var(--text-secondary); -} - .account-import-drawer { display: flex; flex-direction: column; - gap: 5px; + align-items: stretch; + gap: 6px; padding-top: 2px; } +.account-import-drawer > .segmented { + align-self: flex-start; +} + .account-import-row { display: flex; align-items: center; @@ -772,176 +748,20 @@ min-height: calc(26px * var(--ui-font-scale, 1)); } -.account-import-hint { - font-size: var(--fs-xs); - color: var(--text-muted); -} - -.account-unlock-box { - display: flex; - flex-direction: column; - gap: 6px; - padding: 8px 10px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -.account-unlock-head { - display: flex; - align-items: center; - justify-content: space-between; - gap: 8px; -} - -.account-unlock-title { - display: inline-flex; - align-items: center; - gap: 6px; - font-size: var(--fs-sm); - color: var(--text-secondary); -} - -.account-unlock-title .svg-icon { - color: var(--metric-warn); - flex-shrink: 0; -} - -.account-link-btn { - padding: 0; - border: none; - background: transparent; - font-size: var(--fs-xs); - color: var(--text-muted); - cursor: pointer; - white-space: nowrap; -} - -.account-link-btn:hover { - color: var(--text-primary); - text-decoration: underline; -} - -.account-reset-row { - display: flex; - align-items: center; - justify-content: space-between; - flex-wrap: wrap; - gap: 6px 10px; - padding-top: 4px; -} - -.account-reset-row .settings-action-desc { - flex: 1 1 180px; - font-size: var(--fs-xs); -} - -.account-pwd-tabs { - display: inline-flex; - align-items: center; - align-self: flex-start; - gap: 4px; - padding: 2px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -.account-pwd-tab { - padding: 2px 8px; - border: none; - border-radius: var(--radius-xs); - background: transparent; - font-size: var(--fs-xs); - font-weight: 500; - color: var(--text-secondary); - cursor: pointer; -} - -.account-pwd-tab.is-active { - background: var(--bg-elevated); - color: var(--text-primary); -} - -.account-card-btn.is-confirm-warn { - color: var(--accent-primary); - background: var(--accent-primary-soft); - font-weight: 600; -} - -.account-confirm-danger-btn { - color: var(--metric-danger); - background: color-mix(in srgb, var(--metric-danger) 12%, transparent); - font-weight: 600; -} - -.account-confirm-danger-btn:hover:not(:disabled) { - background: color-mix(in srgb, var(--metric-danger) 20%, transparent); -} - -.account-flow-strip { - display: flex; - align-items: center; - justify-content: space-between; - flex-wrap: wrap; - gap: 6px 10px; - padding: 6px 9px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); - font-size: var(--fs-sm); -} - -.account-flow-strip.is-info { - background: var(--accent-primary-soft); -} - -.account-flow-strip.is-info .account-flow-msg { - color: var(--accent-primary); -} - -.account-flow-strip.is-ok { - background: color-mix(in srgb, var(--metric-ok) 10%, transparent); -} - -.account-flow-strip.is-ok .account-flow-msg { - color: var(--metric-ok); -} - -.account-flow-strip.is-warn { - background: color-mix(in srgb, var(--metric-warn) 10%, transparent); - color: var(--metric-warn); -} - -.account-flow-strip.is-error { - background: color-mix(in srgb, var(--metric-danger) 10%, transparent); -} - -.account-flow-strip.is-error .account-flow-msg { - color: var(--metric-danger); -} - -.account-flow-link { - font-size: var(--fs-sm); - font-weight: 500; - color: var(--accent-primary); - text-decoration: none; - white-space: nowrap; -} - -.account-flow-link:hover { - text-decoration: underline; -} - .account-cards-stack { display: flex; flex-direction: column; gap: var(--space-3); } +/* Optically symmetrical vertical padding: 11px + 3px header leading == 14px bottom bar-to-edge */ .account-card { gap: 10px; - padding: 11px 14px; + padding: 11px var(--space-5) 14px; } .account-card-head { + min-height: calc(22px * var(--ui-font-scale, 1)); gap: 8px; flex-wrap: wrap; } @@ -955,14 +775,10 @@ flex: 1 1 150px; } -.account-active-dot { - background: var(--metric-ok); - box-shadow: 0 0 0 2px color-mix(in srgb, var(--metric-ok) 20%, transparent); -} - .account-card-email { font-size: var(--fs-base); font-weight: 600; + line-height: 1.3; color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; @@ -970,34 +786,6 @@ max-width: 100%; } -.account-tag { - display: inline-flex; - align-items: center; - min-height: calc(18px * var(--ui-font-scale, 1)); - padding: 1px 6px; - font-size: var(--fs-xs); - font-weight: 600; - line-height: 1.2; - border-radius: var(--radius-xs); - border: none; - white-space: nowrap; -} - -.account-tag.is-active { - color: var(--metric-ok); - background: color-mix(in srgb, var(--metric-ok) 12%, transparent); -} - -.account-tag.is-tier { - color: var(--accent-primary); - background: var(--accent-primary-soft); -} - -.account-tag.is-written { - color: var(--metric-warn); - background: color-mix(in srgb, var(--metric-warn) 12%, transparent); -} - .account-card-actions { display: inline-flex; align-items: center; @@ -1006,21 +794,16 @@ margin-left: auto; } -.account-card-btn { - min-height: calc(23px * var(--ui-font-scale, 1)); - height: auto; +.account-card-actions .btn-ghost-sm { + min-height: calc(22px * var(--ui-font-scale, 1)); padding: 1px 8px; - font-size: var(--fs-sm); - font-weight: 500; - border-radius: var(--radius-xs); - white-space: nowrap; } -.account-icon-btn { - width: calc(23px * var(--ui-font-scale, 1)); - height: calc(23px * var(--ui-font-scale, 1)); +.account-card-actions .icon-btn { + width: calc(22px * var(--ui-font-scale, 1)); + height: calc(22px * var(--ui-font-scale, 1)); + min-width: calc(22px * var(--ui-font-scale, 1)); padding: 0; - border-radius: var(--radius-xs); } .account-pools-stack { @@ -1029,18 +812,11 @@ gap: 9px; } -.account-pool-group { - display: flex; - flex-direction: column; - gap: 4px; -} - -.account-pool-name { +.account-pool-label { font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted); line-height: 1.25; - letter-spacing: 0.01em; } .account-pool-meters { @@ -1066,34 +842,61 @@ min-width: 0; } -.account-meter-head { +/* Quota Trend Card minor layout helpers */ +.account-trend-toprow { + gap: var(--space-2); + flex-wrap: wrap; +} + +.account-trend-toprow .cons-cycle-pool-select { + flex: 0 0 auto; +} + +.account-trend-subbar { + gap: 6px 8px; + flex-wrap: wrap; +} + +.account-trend-email-select { + max-width: 156px; +} + +.account-trend-legend { display: flex; - align-items: baseline; - justify-content: space-between; - gap: 6px; - min-width: 0; + align-items: center; + flex-wrap: wrap; + gap: 6px 12px; + padding-top: 2px; } -.settings-account-quota { - font-size: var(--fs-sm); - font-weight: 600; +.account-trend-leg-item { + display: inline-flex; + align-items: center; + gap: 5px; + font-size: var(--fs-xs); + color: var(--text-secondary); +} + +.account-trend-leg-item strong { color: var(--text-primary); - white-space: nowrap; } -.account-meter-reset { - font-size: var(--fs-2xs); - color: var(--text-muted); - white-space: nowrap; - flex-shrink: 0; +.account-trend-tip-rows { + display: flex; + flex-direction: column; + gap: 4px; } -.account-progress-track { - height: 5px; +.account-trend-tip-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + font-size: var(--fs-xs); } .account-empty-panel { - padding: 18px 14px; + padding: 16px var(--space-5); align-items: center; justify-content: center; } diff --git a/sidecars/panel/src/views/settings/SettingsAccountTab.js b/sidecars/panel/src/views/settings/SettingsAccountTab.js index 602ff7f..fe54634 100644 --- a/sidecars/panel/src/views/settings/SettingsAccountTab.js +++ b/sidecars/panel/src/views/settings/SettingsAccountTab.js @@ -1,4 +1,8 @@ -/** @fileoverview Unified multi-account switcher, passphrase vault UI, and per-pool quota cards. */ +/** + * @fileoverview Multi-account switcher, quota trajectory chart, risk gate, and passphrase vault UI. + * Reuses `SegmentedControl`, `CustomSelect`, `AccountQuotaTrendCard`, `.ctx-risk-mask`, + * `.settings-bridge-notice`, `.status-tag`, `.btn-ghost-sm`, `.btn-danger-confirm`, and `.quota-pool-row`. + */ import { defineComponent } from '../../core/component.js'; import { @@ -14,6 +18,8 @@ import { callSidecarApi } from '../../core/httpClient.js'; import { createSvgIcon, patchSvgIcon } from '../../icons/SvgIcon.js'; import { getEffectiveLocale, t } from '../../i18n/index.js'; import { CustomSelect } from '../../components/ui/CustomSelect.js'; +import { SegmentedControl } from '../../components/ui/SegmentedControl.js'; +import { AccountQuotaTrendCard } from '../../components/widgets/AccountQuotaTrendCard.js'; import { formatAccountTierBadge } from '../../components/widgets/QuotaPoolsWidget.js'; import { DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES, @@ -34,7 +40,7 @@ const errors = new Set([ 'switchStartFailed', 'clientCloseFailed', 'clientRestartFailed', 'loginStartFailed', 'loginExchangeFailed', 'loginExpired', 'loginDenied', 'vaultLocked', 'wrongPassword', 'invalidPassword', 'passwordMismatch', - 'passwordRateLimited', 'vaultDecryptFailed', 'invalidActionNonce', + 'passwordRateLimited', 'vaultDecryptFailed', 'invalidActionNonce', 'riskNotAccepted', ]); const okToastKeys = new Set([ @@ -55,6 +61,13 @@ function getAccountRefreshOptions() { ]; } +function getPwdModeOptions() { + return [ + { value: 'change', label: t(key('pwdTabChange')) }, + { value: 'disable', label: t(key('pwdTabDisable')) }, + ]; +} + function formatValidCountdown(isoTime) { if (!isoTime) return ''; const ms = Date.parse(isoTime); @@ -84,8 +97,9 @@ function renderQuotaRows(container, quotaPools) { ); patchKeyedList(container, pools, { keyFn: (pool) => pool.poolKey || pool.poolLabel, - sigFn: (pool) => + sigFn: (pool, idx) => JSON.stringify([ + idx > 0, locale, pool.poolLabel, pool.remainingPct, @@ -94,32 +108,39 @@ function renderQuotaRows(container, quotaPools) { pool.weeklyResetTime, ]), createRow: () => { - const poolTitle = h('div', { className: 'account-pool-name' }); + const poolTitle = h('div', { className: 'account-pool-label' }); - const label5h = h('span', { className: 'settings-account-quota text-mono' }); - const reset5h = h('span', { className: 'account-meter-reset text-mono', hidden: true }); + const label5h = h('span', { className: 'quota-win-title settings-account-quota text-mono' }); + const reset5h = h('span', { className: 'panel-meta quota-win-meta text-mono', hidden: true }); const fill5h = h('div', { className: 'progress-fill' }); const meter5h = h('div', { className: 'account-meter-col' }, [ - h('div', { className: 'account-meter-head' }, [label5h, reset5h]), - h('div', { className: 'progress-track account-progress-track' }, [fill5h]), + h('div', { className: 'panel-header quota-win-head' }, [ + h('span', { className: 'quota-win-left' }, [label5h]), + reset5h, + ]), + h('div', { className: 'progress-track' }, [fill5h]), ]); - const labelWk = h('span', { className: 'settings-account-quota text-mono' }); - const resetWk = h('span', { className: 'account-meter-reset text-mono', hidden: true }); + const labelWk = h('span', { className: 'quota-win-title settings-account-quota text-mono' }); + const resetWk = h('span', { className: 'panel-meta quota-win-meta text-mono', hidden: true }); const fillWk = h('div', { className: 'progress-fill' }); const meterWk = h('div', { className: 'account-meter-col' }, [ - h('div', { className: 'account-meter-head' }, [labelWk, resetWk]), - h('div', { className: 'progress-track account-progress-track' }, [fillWk]), + h('div', { className: 'panel-header quota-win-head' }, [ + h('span', { className: 'quota-win-left' }, [labelWk]), + resetWk, + ]), + h('div', { className: 'progress-track' }, [fillWk]), ]); const gridEl = h('div', { className: 'account-pool-meters' }, [meter5h, meterWk]); - const el = h('div', { className: 'account-pool-group' }, [poolTitle, gridEl]); + const el = h('div', { className: 'quota-pool-row' }, [poolTitle, gridEl]); return { el, - refs: { poolTitle, gridEl, meter5h, label5h, reset5h, fill5h, meterWk, labelWk, resetWk, fillWk }, + refs: { el, poolTitle, gridEl, meter5h, label5h, reset5h, fill5h, meterWk, labelWk, resetWk, fillWk }, }; }, - updateRow: (refs, pool) => { + updateRow: (refs, pool, idx) => { + patchClasses(refs.el, { 'has-divider': idx > 0 }); patchText(refs.poolTitle, pool.poolLabel || pool.poolKey); const has5h = Number.isFinite(pool.remainingPct); const hasWk = Number.isFinite(pool.weeklyRemainingPct); @@ -164,18 +185,22 @@ function isValidVaultPassphrase(raw) { export const SettingsAccountTab = defineComponent({ name: 'SettingsAccountTab', setup({ props, onCleanup }) { + const initialSettings = telemetryStore.getState().telemetry?.settings; let account = null; let quotas = null; let refreshMinutes = normalizeAccountQuotaRefreshMinutes( - telemetryStore.getState().telemetry?.settings?.accountQuotaRefreshMinutes ?? - DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES + initialSettings?.accountQuotaRefreshMinutes ?? DEFAULT_ACCOUNT_QUOTA_REFRESH_MINUTES ); + let draftRiskAccepted = Boolean(initialSettings?.accountSwitchRiskAccepted); + let acceptingRisk = false; + let riskLockedFromServer = warmAccountCache ? Boolean(warmAccountCache.riskLocked) : true; let accounts = warmAccountCache ? [...warmAccountCache.accounts] : []; let selectedEmail = warmAccountCache ? warmAccountCache.selectedEmail : ''; let supported = warmAccountCache ? warmAccountCache.supported : null; let vaultSecurity = warmAccountCache?.vaultSecurity ? { ...warmAccountCache.vaultSecurity } : { enabled: false, unlocked: true }; + let quotaHistory = warmAccountCache?.quotaHistory ? { ...warmAccountCache.quotaHistory } : {}; let actionNonce = warmAccountCache?.actionNonce || ''; let operation = ''; let status = ''; @@ -206,10 +231,12 @@ export const SettingsAccountTab = defineComponent({ if (supported !== true) return; warmAccountCache = { supported, + riskLocked: riskLockedFromServer, accounts: [...accounts], selectedEmail, savedQuotasEntries: [...savedQuotas.entries()], vaultSecurity: { ...vaultSecurity }, + quotaHistory: { ...quotaHistory }, actionNonce, }; } @@ -260,12 +287,19 @@ export const SettingsAccountTab = defineComponent({ pwdConfirmInput.value = ''; } + // --- Section 1: Active Account Card Host --- + const activeCardHost = h('div', { className: 'account-cards-stack' }); + + // --- Section 2: Quota Trend Line Chart Card --- + const quotaTrendCard = AccountQuotaTrendCard(); + + // --- Section 3: Multi-Account Toolbar, Drawers, Backup Cards & Risk Gate --- const loginBtnLabel = h('span'); const loginButton = h( 'button', { type: 'button', - className: 'btn-primary settings-account-login account-toolbar-btn', + className: 'btn-primary settings-account-login', onClick: () => { clearConfirmTarget(false); void request('loginStart', { language: getEffectiveLocale() }); @@ -275,7 +309,7 @@ export const SettingsAccountTab = defineComponent({ ); const cancelLogin = h('button', { type: 'button', - className: 'btn-secondary settings-account-login-cancel account-toolbar-btn', + className: 'btn-secondary settings-account-login-cancel', hidden: true, onClick: () => request('loginCancel', { id: loginSession?.id }), }); @@ -284,7 +318,7 @@ export const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn', + className: 'btn-secondary', onClick: () => { clearConfirmTarget(false); importOpen = !importOpen; @@ -305,13 +339,14 @@ export const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn settings-account-pwd-toggle', + className: 'btn-secondary settings-account-pwd-toggle', onClick: () => { clearConfirmTarget(false); pwdDrawerOpen = !pwdDrawerOpen; if (pwdDrawerOpen) { importOpen = false; pwdManageMode = 'change'; + pwdModeSegment.setProps({ modelValue: 'change', options: getPwdModeOptions() }); } clearPasswordInputs(); render(); @@ -328,7 +363,7 @@ export const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'btn-secondary account-toolbar-btn settings-account-lock-now', + className: 'btn-secondary settings-account-lock-now', hidden: true, onClick: () => { clearConfirmTarget(false); @@ -370,17 +405,17 @@ export const SettingsAccountTab = defineComponent({ 'button', { type: 'button', - className: 'icon-btn account-reload-btn', + className: 'icon-btn', onClick: () => request('list'), }, [createSvgIcon('refresh', { size: 13 })] ); - // --- Locked Vault Unlock Strip --- - const unlockHintLabel = h('span'); + // Locked Vault Notice (reusing `.settings-bridge-notice`) + const unlockHintLabel = h('span', { className: 'settings-bridge-title' }); const forgotToggleBtn = h('button', { type: 'button', - className: 'account-link-btn settings-account-forgot-btn', + className: 'btn-ghost-sm settings-account-forgot-btn', onClick: () => { forgotResetOpen = !forgotResetOpen; clearConfirmTarget(false); @@ -396,7 +431,7 @@ export const SettingsAccountTab = defineComponent({ }); const unlockSubmitBtn = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn settings-account-unlock-submit', + className: 'btn-primary settings-account-unlock-submit', }); const unlockForm = h( 'form', @@ -407,55 +442,47 @@ export const SettingsAccountTab = defineComponent({ }, [unlockInput, unlockSubmitBtn] ); - const resetHintEl = h('span', { className: 'settings-action-desc' }); + const resetHintEl = h('p', { className: 'settings-bridge-desc', hidden: true }); const resetVaultBtn = h('button', { type: 'button', - className: 'btn-secondary account-card-btn account-confirm-danger-btn settings-account-reset-vault', + className: 'btn-secondary btn-danger-confirm settings-account-reset-vault', + hidden: true, onClick: () => { armOrRunConfirm('resetVault', 'vault', () => { void request('resetVault', { confirmReset: 'RESET' }); }); }, }); - const resetRow = h('div', { className: 'account-reset-row', hidden: true }, [ - resetHintEl, - resetVaultBtn, - ]); - const unlockBox = h('div', { className: 'account-unlock-box settings-account-unlock-box', hidden: true }, [ - h('div', { className: 'account-unlock-head' }, [ - h('span', { className: 'account-unlock-title' }, [ - createSvgIcon('lock', { size: 13 }), - unlockHintLabel, + const unlockBox = h( + 'div', + { className: 'settings-bridge-notice settings-account-unlock-box', hidden: true }, + [ + h('div', { className: 'settings-bridge-head' }, [ + h('span', { className: 'settings-bridge-head-main' }, [ + createSvgIcon('lock', { size: 13, className: 'settings-bridge-icon' }), + unlockHintLabel, + ]), + forgotToggleBtn, ]), - forgotToggleBtn, - ]), - unlockForm, - resetRow, - ]); + unlockForm, + resetHintEl, + h('div', { className: 'settings-bridge-actions', hidden: true }, [resetVaultBtn]), + ] + ); + const resetActionsWrap = resetVaultBtn.parentElement; - // --- Passphrase Setup / Manage Drawer --- - const pwdTabChangeBtn = h('button', { - type: 'button', - className: 'account-pwd-tab', - onClick: () => { - pwdManageMode = 'change'; + // Passphrase Setup / Manage Drawer (reusing `SegmentedControl`!) + const pwdModeSegment = SegmentedControl({ + ariaLabel: t(key('pwdBtnManage')), + modelValue: pwdManageMode, + options: getPwdModeOptions(), + onUpdateModelValue: (val) => { + pwdManageMode = val === 'disable' ? 'disable' : 'change'; + pwdModeSegment.setProps({ modelValue: pwdManageMode, options: getPwdModeOptions() }); clearPasswordInputs(); render(); }, }); - const pwdTabDisableBtn = h('button', { - type: 'button', - className: 'account-pwd-tab', - onClick: () => { - pwdManageMode = 'disable'; - clearPasswordInputs(); - render(); - }, - }); - const pwdTabsWrap = h('div', { className: 'account-pwd-tabs', hidden: true }, [ - pwdTabChangeBtn, - pwdTabDisableBtn, - ]); const pwdCurrentInput = h('input', { type: 'password', className: 'settings-text-input account-import-input settings-account-pwd-current', @@ -480,9 +507,9 @@ export const SettingsAccountTab = defineComponent({ }); const pwdSubmitBtn = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn settings-account-pwd-submit', + className: 'btn-primary settings-account-pwd-submit', }); - const pwdHint = h('div', { className: 'settings-action-desc account-import-hint' }); + const pwdHint = h('div', { className: 'settings-action-desc' }); const pwdForm = h( 'form', { @@ -492,7 +519,7 @@ export const SettingsAccountTab = defineComponent({ onSubmit: submitPasswordDrawer, }, [ - pwdTabsWrap, + pwdModeSegment.el, h('div', { className: 'account-import-row' }, [ pwdCurrentInput, pwdNewInput, @@ -503,7 +530,7 @@ export const SettingsAccountTab = defineComponent({ ] ); - // --- Credential Import Drawer --- + // Credential Import Drawer const credential = h('input', { type: 'password', className: 'settings-text-input account-import-input', @@ -514,9 +541,9 @@ export const SettingsAccountTab = defineComponent({ }); const save = h('button', { type: 'submit', - className: 'btn-primary account-toolbar-btn', + className: 'btn-primary', }); - const importHint = h('div', { className: 'settings-action-desc account-import-hint' }); + const importHint = h('div', { className: 'settings-action-desc' }); const form = h( 'form', { @@ -531,26 +558,27 @@ export const SettingsAccountTab = defineComponent({ ] ); - const loginMessage = h('div', { - className: 'settings-action-desc account-flow-msg', + // OAuth Browser Login & Switch Notices (reusing `.settings-bridge-notice`) + const loginMessage = h('p', { + className: 'settings-bridge-desc settings-action-desc', role: 'status', hidden: true, }); const loginLink = h('a', { - className: 'account-flow-link', + className: 'settings-bridge-relaunch-btn', target: '_blank', rel: 'noopener noreferrer', hidden: true, }); - const loginStrip = h('div', { className: 'account-flow-strip', hidden: true }, [ - loginMessage, - loginLink, - ]); - const pending = h('div', { - className: 'settings-action-desc account-flow-strip is-warn', - role: 'status', - hidden: true, - }); + const loginStrip = h( + 'div', + { className: 'settings-bridge-notice is-restart', hidden: true }, + [loginMessage, h('div', { className: 'settings-bridge-actions' }, [loginLink])] + ); + const loginLinkWrap = loginLink.parentElement; + + const pendingDesc = h('p', { className: 'settings-bridge-desc', role: 'status' }); + const pending = h('div', { className: 'settings-bridge-notice', hidden: true }, [pendingDesc]); const toolbarPanel = h('section', { className: 'panel account-toolbar-panel' }, [ h('div', { className: 'account-toolbar-row' }, [ @@ -574,36 +602,261 @@ export const SettingsAccountTab = defineComponent({ pending, ]); - const cardsHost = h('div', { className: 'account-cards-stack' }); + const backupCardsHost = h('div', { className: 'account-cards-stack' }); const emptyPanel = h('section', { className: 'panel account-empty-panel', hidden: true }, [ h('div', { className: 'settings-action-desc account-empty-text' }), ]); const emptyTextEl = emptyPanel.firstChild; + // Risk Gate Stage (100% shared `.ctx-risk-stage` / `.ctx-risk-mask` with `SettingsOtherTab`) + const riskTitleEl = h('strong', { + className: 'ctx-risk-title', + text: t('settings.oth.ctxRiskTitle'), + }); + const riskBodyEl = h('p', { + className: 'ctx-risk-desc', + text: t(key('riskBody')), + }); + const riskAcceptBtnText = h('span', { text: t('settings.oth.ctxRiskAcceptBtn') }); + const riskAcceptBtn = h( + 'button', + { + type: 'button', + className: 'btn-secondary btn-danger-confirm ctx-risk-accept-btn settings-account-risk-accept', + onClick: async () => { + if (acceptingRisk || draftRiskAccepted) return; + acceptingRisk = true; + draftRiskAccepted = true; + render(); + try { + await saveSettingsPatch({ accountSwitchRiskAccepted: true }); + await request('list'); + } catch { + draftRiskAccepted = false; + setTransientStatus('storeFailed', 2800); + } finally { + acceptingRisk = false; + if (!disposed) render(); + } + }, + }, + [createSvgIcon('warning', { size: 12 }), riskAcceptBtnText] + ); + const riskMask = h( + 'div', + { + className: 'ctx-risk-mask settings-account-risk-mask', + role: 'region', + 'aria-label': t('settings.oth.ctxRiskTitle'), + }, + [ + h('div', { className: 'ctx-risk-head' }, [ + createSvgIcon('warning', { size: 14, className: 'ctx-risk-icon' }), + riskTitleEl, + ]), + riskBodyEl, + riskAcceptBtn, + ] + ); + const managedControlsBody = h('div', { className: 'ctx-controls-body is-locked' }, [ + toolbarPanel, + backupCardsHost, + ]); + const riskStage = h('div', { className: 'ctx-risk-stage' }, [managedControlsBody, riskMask]); + function resolveActiveEmail() { return account?.status === 'signedIn' ? (account.email || '').trim().toLowerCase() : ''; } - function buildUnifiedAccountEntries(activeEmail) { - const entries = []; - if (activeEmail) { - entries.push({ - email: activeEmail, - isActive: true, - isSaved: accounts.includes(activeEmail), - }); - } - for (let i = 0; i < accounts.length; i++) { - const addr = accounts[i]; - if (addr && addr !== activeEmail) { - entries.push({ - email: addr, - isActive: false, - isSaved: true, + function createAccountCardDom(entry) { + const emailEl = h('span', { + className: 'settings-account-identity account-card-email', + text: entry.email, + title: entry.email, + }); + const tierPill = h('span', { className: 'status-tag state-synced', hidden: true }); + const statePill = h('span', { className: 'status-tag', hidden: true }); + + const captureBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm', + hidden: true, + onClick: () => { + clearConfirmTarget(false); + void request('capture'); + }, + }); + const selectBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm', + hidden: true, + onClick: () => { + armOrRunConfirm('switch', entry.email, () => { + const curActive = resolveActiveEmail(); + void request('switch', { + email: entry.email, + ...(curActive ? { activeEmail: curActive } : {}), + }); }); - } + }, + }); + const loadQuotaBtn = h( + 'button', + { + type: 'button', + className: 'icon-btn settings-account-load-quota', + hidden: true, + onClick: () => { + clearConfirmTarget(false); + void request('quota', { email: entry.email, manual: true }); + }, + }, + [createSvgIcon('refresh', { size: 12 })] + ); + const removeBtn = h( + 'button', + { + type: 'button', + className: 'icon-btn slot-remove-btn settings-account-remove', + hidden: true, + onClick: () => { + armOrRunConfirm('remove', entry.email, () => { + void request('remove', { email: entry.email }); + }); + }, + }, + [createSvgIcon('close', { size: 12 })] + ); + const removeConfirmBtn = h('button', { + type: 'button', + className: 'btn-ghost-sm btn-danger-confirm settings-account-remove-confirm', + hidden: true, + onClick: () => { + armOrRunConfirm('remove', entry.email, () => { + void request('remove', { email: entry.email }); + }); + }, + }); + + const actionsWrap = h('div', { className: 'account-card-actions' }, [ + captureBtn, + selectBtn, + loadQuotaBtn, + removeBtn, + removeConfirmBtn, + ]); + const poolsWrap = h('div', { className: 'account-pools-stack' }); + const noteEl = h('div', { className: 'settings-action-desc account-card-note', hidden: true }); + + const cardEl = h('section', { className: 'panel account-card' }, [ + h('div', { className: 'panel-header account-card-head' }, [ + h('div', { className: 'account-card-meta' }, [emailEl, tierPill, statePill]), + actionsWrap, + ]), + poolsWrap, + noteEl, + ]); + return { + el: cardEl, + refs: { + cardEl, + emailEl, + tierPill, + statePill, + actionsWrap, + captureBtn, + selectBtn, + loadQuotaBtn, + removeBtn, + removeConfirmBtn, + poolsWrap, + noteEl, + }, + }; + } + + function updateAccountCardDom(row, entry, ctx) { + const addr = entry.email; + const active = entry.isActive; + const isSaved = entry.isSaved; + const written = addr === selectedEmail && !active && switchState?.state === 'written'; + const cached = active ? null : savedQuotas.get(addr); + const tierText = active + ? ctx.activeTierBadge + : cached?.tierName || cached?.tierId + ? formatAccountTierBadge({ + tierId: cached.tierId, + tierName: cached.tierName, + tierMultiplier: cached.tierMultiplier, + }) + : ''; + const isSpinning = !active && quotaVisibleLoading.has(addr); + const canSwitch = !active && isSaved && ctx.riskAccepted; + const armedSwitch = confirmTarget?.type === 'switch' && confirmTarget.key === addr; + const armedRemove = confirmTarget?.type === 'remove' && confirmTarget.key === addr; + + patchClasses(row.cardEl, { 'is-active-account': active }); + patchText(row.emailEl, addr); + patchAttr(row.emailEl, 'title', addr); + + patchText(row.tierPill, tierText); + patchAttr(row.tierPill, 'hidden', !tierText); + + patchText(row.statePill, active ? t(key('active')) : written ? t(key('written')) : ''); + patchClasses(row.statePill, { 'state-warn': written && !active }); + patchAttr(row.statePill, 'hidden', !active && !written); + + const showCapture = active && !isSaved && ctx.riskAccepted && supported !== false; + const showRemove = isSaved && ctx.riskAccepted; + const showQuotaRefresh = !active && isSaved && ctx.riskAccepted; + const hasAnyAction = showCapture || canSwitch || showQuotaRefresh || showRemove; + patchAttr(row.actionsWrap, 'hidden', !hasAnyAction); + + patchText(row.captureBtn, t(key('capture'))); + patchAttr(row.captureBtn, 'hidden', !showCapture); + patchAttr(row.captureBtn, 'disabled', !ctx.canWrite); + + patchText(row.selectBtn, t(key(armedSwitch ? 'switchConfirm' : 'switch'))); + patchAttr(row.selectBtn, 'title', t(key('switchHint'))); + patchClasses(row.selectBtn, { + 'settings-account-switch': canSwitch, + 'btn-danger-confirm': armedSwitch, + }); + patchAttr(row.selectBtn, 'hidden', !canSwitch); + patchAttr(row.selectBtn, 'disabled', !ctx.canWrite); + + patchAttr(row.loadQuotaBtn, 'title', t(key(isSpinning ? 'loadingQuota' : 'loadQuota'))); + patchAttr(row.loadQuotaBtn, 'aria-label', t(key('loadQuota'))); + patchClasses(row.loadQuotaBtn, { 'is-spinning': isSpinning }); + patchAttr(row.loadQuotaBtn, 'hidden', !showQuotaRefresh); + patchAttr(row.loadQuotaBtn, 'disabled', !ctx.canWrite || isSpinning); + + patchAttr(row.removeBtn, 'title', t(key('remove'))); + patchAttr(row.removeBtn, 'aria-label', t(key('remove'))); + patchAttr(row.removeBtn, 'hidden', !showRemove || armedRemove); + patchAttr(row.removeBtn, 'disabled', !ctx.canWrite); + + patchText(row.removeConfirmBtn, t(key('removeConfirm'))); + patchAttr(row.removeConfirmBtn, 'hidden', !showRemove || !armedRemove); + patchAttr(row.removeConfirmBtn, 'disabled', !ctx.canWrite); + + const displayedPools = active ? quotas?.pools || [] : cached?.pools || []; + const poolCount = renderQuotaRows(row.poolsWrap, displayedPools); + const quotaError = active ? '' : quotaErrors.get(addr); + + let noteText = ''; + if (quotaError) { + noteText = t(key(quotaError)); + } else if (active && poolCount > 0 && quotas?.meta?.isStale) { + noteText = t(key('quotaStale')); + } else if (active && poolCount === 0) { + noteText = t(key('noQuota')); + } else if (!active && cached && poolCount === 0) { + noteText = t(key('emptyCredentialQuota')); } - return entries; + patchText(row.noteEl, noteText); + patchClasses(row.noteEl, { 'is-error': Boolean(quotaError) }); + patchAttr(row.noteEl, 'hidden', !noteText); } function render() { @@ -612,8 +865,13 @@ export const SettingsAccountTab = defineComponent({ if (activeEmail && selectedEmail === activeEmail && switchState?.state !== 'starting') { selectedEmail = ''; } + const riskAccepted = Boolean(draftRiskAccepted); + if (!riskAccepted) { + refreshIntervalSelect.close?.(); + } const vaultLocked = isVaultLocked(); const canOperateVault = + riskAccepted && supported === true && !operation && status !== 'restarting' && @@ -624,6 +882,55 @@ export const SettingsAccountTab = defineComponent({ signedIn && (account?.tierName || account?.planName) ? formatAccountTierBadge(account) : ''; + const cardCtx = { riskAccepted, canWrite, activeTierBadge }; + + // 1. Active Logged-in Account Card + const activeEntries = activeEmail + ? [ + { + email: activeEmail, + isActive: true, + isSaved: riskAccepted && accounts.includes(activeEmail), + }, + ] + : []; + patchAttr(activeCardHost, 'hidden', activeEntries.length === 0); + patchKeyedList(activeCardHost, activeEntries, { + keyFn: (entry) => entry.email, + sigFn: (entry) => + JSON.stringify([ + locale, + entry.isSaved, + riskAccepted, + canWrite, + confirmTarget?.key === entry.email ? confirmTarget.type : '', + quotas, + activeTierBadge, + ]), + createRow: createAccountCardDom, + updateRow: (row, entry) => updateAccountCardDom(row, entry, cardCtx), + }); + + // 2. Quota Trend Line Chart Card (always visible; locked to activeEmail when !riskAccepted) + quotaTrendCard.refs.update({ + activeEmail, + activePools: quotas?.pools || [], + riskAccepted, + accounts, + savedQuotas, + quotaHistory, + }); + + // 3. Risk Gate Stage & Multi-Account Controls + patchClasses(managedControlsBody, { 'is-locked': !riskAccepted }); + patchAttr(managedControlsBody, 'inert', !riskAccepted); + patchAttr(managedControlsBody, 'aria-hidden', riskAccepted ? null : 'true'); + patchAttr(riskMask, 'hidden', riskAccepted); + patchAttr(riskMask, 'aria-label', t('settings.oth.ctxRiskTitle')); + patchText(riskTitleEl, t('settings.oth.ctxRiskTitle')); + patchText(riskBodyEl, t(key('riskBody'))); + patchText(riskAcceptBtnText, t('settings.oth.ctxRiskAcceptBtn')); + patchAttr(riskAcceptBtn, 'disabled', acceptingRisk); const loginActive = ['waiting', 'exchanging', 'saving'].includes(loginSession?.status); patchText(loginBtnLabel, t(key('googleLogin'))); @@ -648,32 +955,34 @@ export const SettingsAccountTab = defineComponent({ patchAttr(lockNowBtn, 'hidden', !vaultSecurity.enabled || vaultLocked || supported === false); patchAttr(lockNowBtn, 'disabled', !canOperateVault); - // Unlock strip (only when passphrase enabled & locked) - patchAttr(unlockBox, 'hidden', supported !== true || !vaultLocked); + // Unlock Notice + patchAttr(unlockBox, 'hidden', !riskAccepted || supported !== true || !vaultLocked); patchText(unlockHintLabel, t(key('vaultLockedHint'))); patchText(forgotToggleBtn, t(key('forgotPwdToggle'))); patchAttr(unlockInput, 'placeholder', t(key('unlockPlaceholder'))); patchAttr(unlockInput, 'disabled', !canOperateVault); patchText(unlockSubmitBtn, t(key('unlockBtn'))); patchAttr(unlockSubmitBtn, 'disabled', !canOperateVault); - patchAttr(resetRow, 'hidden', !forgotResetOpen || !vaultLocked); + const showResetSection = forgotResetOpen && vaultLocked; + patchAttr(resetHintEl, 'hidden', !showResetSection); + patchAttr(resetActionsWrap, 'hidden', !showResetSection); + patchAttr(resetVaultBtn, 'hidden', !showResetSection); patchText(resetHintEl, t(key('resetVaultHint'))); const armedVaultReset = confirmTarget?.type === 'resetVault' && confirmTarget.key === 'vault'; patchText(resetVaultBtn, t(key(armedVaultReset ? 'resetVaultConfirm' : 'resetVaultBtn'))); patchAttr(resetVaultBtn, 'disabled', !canOperateVault); - // Passphrase setup / manage drawer - const showPwdDrawer = pwdDrawerOpen && !vaultLocked && supported === true; + // Passphrase Drawer (`SegmentedControl` for Change / Disable) + const showPwdDrawer = riskAccepted && pwdDrawerOpen && !vaultLocked && supported === true; patchAttr(pwdForm, 'hidden', !showPwdDrawer); if (showPwdDrawer) { const isSetNew = !vaultSecurity.enabled; const isDisableMode = !isSetNew && pwdManageMode === 'disable'; - patchAttr(pwdTabsWrap, 'hidden', isSetNew); - patchText(pwdTabChangeBtn, t(key('pwdTabChange'))); - patchClasses(pwdTabChangeBtn, { 'is-active': pwdManageMode === 'change' }); - patchText(pwdTabDisableBtn, t(key('pwdTabDisable'))); - patchClasses(pwdTabDisableBtn, { 'is-active': isDisableMode }); + patchAttr(pwdModeSegment.el, 'hidden', isSetNew); + if (!isSetNew) { + pwdModeSegment.setProps({ modelValue: pwdManageMode, options: getPwdModeOptions() }); + } patchAttr(pwdCurrentInput, 'hidden', isSetNew); patchAttr(pwdCurrentInput, 'placeholder', t(key('pwdCurrentPlaceholder'))); @@ -694,7 +1003,7 @@ export const SettingsAccountTab = defineComponent({ patchClasses(pwdSubmitBtn, { 'btn-primary': !isDisableMode, 'btn-secondary': isDisableMode, - 'account-confirm-danger-btn': isDisableMode, + 'btn-danger-confirm': isDisableMode, }); patchAttr(pwdSubmitBtn, 'disabled', !canOperateVault); patchText(pwdHint, t(key(isSetNew ? 'pwdSetHint' : 'pwdManageHint'))); @@ -713,7 +1022,7 @@ export const SettingsAccountTab = defineComponent({ patchAttr(reload, 'disabled', Boolean(operation)); patchClasses(reload, { 'is-spinning': operation === 'list' }); - patchAttr(form, 'hidden', !importOpen || vaultLocked || supported === false); + patchAttr(form, 'hidden', !riskAccepted || !importOpen || vaultLocked || supported === false); patchAttr(credential, 'placeholder', t(key('credentialPlaceholder'))); patchAttr(credential, 'disabled', !canWrite); patchText(save, t(key('save'))); @@ -752,11 +1061,8 @@ export const SettingsAccountTab = defineComponent({ patchText(loginLink, t(key('openLoginPage'))); patchAttr(loginLink, 'href', authUrl || null); patchAttr(loginLink, 'hidden', !authUrl); - patchClasses(loginStrip, { - 'is-error': loginIsError, - 'is-ok': loginSession?.status === 'success', - 'is-info': loginActive, - }); + patchAttr(loginLinkWrap, 'hidden', !authUrl); + patchClasses(loginStrip, { 'is-restart': !loginIsError }); patchAttr(loginStrip, 'hidden', !hasLoginSession || !loginMessageKey); const failedSwitch = @@ -776,7 +1082,6 @@ export const SettingsAccountTab = defineComponent({ patchClasses(feedback, { 'is-error': isFeedbackError, 'is-ok': isFeedbackOk, - 'is-info': !isFeedbackError && !isFeedbackOk && Boolean(message), }); patchAttr(feedback, 'hidden', !message); @@ -785,16 +1090,38 @@ export const SettingsAccountTab = defineComponent({ activeEmail !== selectedEmail && switchState?.state === 'written' && status !== 'restarting'; - patchText(pending, waiting ? t(key('pending'), { email: selectedEmail }) : ''); + patchText(pendingDesc, waiting ? t(key('pending'), { email: selectedEmail }) : ''); patchAttr(pending, 'hidden', !waiting); - const entries = buildUnifiedAccountEntries(activeEmail); - patchAttr(cardsHost, 'hidden', entries.length === 0); + const backupEntries = riskAccepted + ? accounts + .filter((addr) => addr && addr !== activeEmail) + .map((addr) => ({ email: addr, isActive: false, isSaved: true })) + : []; + patchAttr(backupCardsHost, 'hidden', backupEntries.length === 0); + patchKeyedList(backupCardsHost, backupEntries, { + keyFn: (entry) => entry.email, + sigFn: (entry) => + JSON.stringify([ + locale, + selectedEmail, + switchState?.state || '', + canWrite, + confirmTarget?.key === entry.email ? confirmTarget.type : '', + savedQuotas.get(entry.email), + quotaVisibleLoading.has(entry.email), + quotaErrors.get(entry.email), + ]), + createRow: createAccountCardDom, + updateRow: (row, entry) => updateAccountCardDom(row, entry, cardCtx), + }); + + const showEmpty = !activeEmail && (!riskAccepted || backupEntries.length === 0); patchText( emptyTextEl, t( key( - supported === null + supported === null && !activeEmail ? 'loading' : account?.status === 'signedOut' ? 'signedOut' @@ -802,217 +1129,7 @@ export const SettingsAccountTab = defineComponent({ ) ) ); - patchAttr(emptyPanel, 'hidden', entries.length > 0); - - patchKeyedList(cardsHost, entries, { - keyFn: (entry) => entry.email, - sigFn: (entry) => - JSON.stringify([ - locale, - entry.isActive, - entry.isSaved, - selectedEmail, - switchState?.state || '', - canWrite, - confirmTarget?.key === entry.email ? confirmTarget.type : '', - entry.isActive ? null : savedQuotas.get(entry.email), - quotaVisibleLoading.has(entry.email), - entry.isActive ? null : quotaErrors.get(entry.email), - entry.isActive ? quotas : null, - entry.isActive ? activeTierBadge : '', - ]), - createRow: (entry) => { - const activeDot = h('span', { className: 'dot-swatch account-active-dot', hidden: true }); - const emailEl = h('span', { - className: 'settings-account-identity account-card-email', - text: entry.email, - title: entry.email, - }); - const tierPill = h('span', { className: 'account-tag is-tier', hidden: true }); - const statePill = h('span', { className: 'account-tag', hidden: true }); - - const captureBtn = h('button', { - type: 'button', - className: 'btn-secondary account-card-btn', - hidden: true, - onClick: () => { - clearConfirmTarget(false); - void request('capture'); - }, - }); - const selectBtn = h('button', { - type: 'button', - className: 'btn-secondary account-card-btn', - hidden: true, - onClick: () => { - armOrRunConfirm('switch', entry.email, () => { - const curActive = resolveActiveEmail(); - void request('switch', { - email: entry.email, - ...(curActive ? { activeEmail: curActive } : {}), - }); - }); - }, - }); - const loadQuotaBtn = h( - 'button', - { - type: 'button', - className: 'icon-btn settings-account-load-quota account-icon-btn', - hidden: true, - onClick: () => { - clearConfirmTarget(false); - void request('quota', { email: entry.email, manual: true }); - }, - }, - [createSvgIcon('refresh', { size: 12 })] - ); - const removeBtn = h( - 'button', - { - type: 'button', - className: 'icon-btn slot-remove-btn settings-account-remove account-icon-btn', - hidden: true, - onClick: () => { - armOrRunConfirm('remove', entry.email, () => { - void request('remove', { email: entry.email }); - }); - }, - }, - [createSvgIcon('close', { size: 12 })] - ); - const removeConfirmBtn = h('button', { - type: 'button', - className: - 'btn-secondary account-card-btn account-confirm-danger-btn settings-account-remove-confirm', - hidden: true, - onClick: () => { - armOrRunConfirm('remove', entry.email, () => { - void request('remove', { email: entry.email }); - }); - }, - }); - - const poolsWrap = h('div', { className: 'account-pools-stack' }); - const noteEl = h('div', { className: 'settings-action-desc account-card-note', hidden: true }); - - const cardEl = h('section', { className: 'panel account-card' }, [ - h('div', { className: 'panel-header account-card-head' }, [ - h('div', { className: 'account-card-meta' }, [ - activeDot, - emailEl, - tierPill, - statePill, - ]), - h('div', { className: 'account-card-actions' }, [ - captureBtn, - selectBtn, - loadQuotaBtn, - removeBtn, - removeConfirmBtn, - ]), - ]), - poolsWrap, - noteEl, - ]); - return { - el: cardEl, - refs: { - cardEl, - activeDot, - emailEl, - tierPill, - statePill, - captureBtn, - selectBtn, - loadQuotaBtn, - removeBtn, - removeConfirmBtn, - poolsWrap, - noteEl, - }, - }; - }, - updateRow: (row, entry) => { - const addr = entry.email; - const active = entry.isActive; - const isSaved = entry.isSaved; - const written = addr === selectedEmail && !active && switchState?.state === 'written'; - const cached = active ? null : savedQuotas.get(addr); - const tierText = active - ? activeTierBadge - : cached?.tierName || cached?.tierId - ? formatAccountTierBadge({ - tierId: cached.tierId, - tierName: cached.tierName, - tierMultiplier: cached.tierMultiplier, - }) - : ''; - const isSpinning = !active && quotaVisibleLoading.has(addr); - const canSwitch = !active && isSaved; - const armedSwitch = confirmTarget?.type === 'switch' && confirmTarget.key === addr; - const armedRemove = confirmTarget?.type === 'remove' && confirmTarget.key === addr; - - patchClasses(row.cardEl, { 'is-active-account': active }); - patchAttr(row.activeDot, 'hidden', !active); - patchText(row.emailEl, addr); - patchAttr(row.emailEl, 'title', addr); - - patchText(row.tierPill, tierText); - patchAttr(row.tierPill, 'hidden', !tierText); - - patchText(row.statePill, active ? t(key('active')) : written ? t(key('written')) : ''); - patchClasses(row.statePill, { 'is-active': active, 'is-written': written }); - patchAttr(row.statePill, 'hidden', !active && !written); - - patchText(row.captureBtn, t(key('capture'))); - patchAttr(row.captureBtn, 'hidden', !active || isSaved || supported === false); - patchAttr(row.captureBtn, 'disabled', !canWrite); - - patchText(row.selectBtn, t(key(armedSwitch ? 'switchConfirm' : 'switch'))); - patchAttr(row.selectBtn, 'title', t(key('switchHint'))); - patchClasses(row.selectBtn, { - 'settings-account-switch': canSwitch, - 'is-confirm-warn': armedSwitch, - }); - patchAttr(row.selectBtn, 'hidden', !canSwitch); - patchAttr(row.selectBtn, 'disabled', !canWrite); - - patchAttr(row.loadQuotaBtn, 'title', t(key(isSpinning ? 'loadingQuota' : 'loadQuota'))); - patchAttr(row.loadQuotaBtn, 'aria-label', t(key('loadQuota'))); - patchClasses(row.loadQuotaBtn, { 'is-spinning': isSpinning }); - // Active account exclusively uses host telemetry — hide OAuth quota refresh on active card - patchAttr(row.loadQuotaBtn, 'hidden', active || !isSaved); - patchAttr(row.loadQuotaBtn, 'disabled', !canWrite || isSpinning); - - patchAttr(row.removeBtn, 'title', t(key('remove'))); - patchAttr(row.removeBtn, 'aria-label', t(key('remove'))); - patchAttr(row.removeBtn, 'hidden', !isSaved || armedRemove); - patchAttr(row.removeBtn, 'disabled', !canWrite); - - patchText(row.removeConfirmBtn, t(key('removeConfirm'))); - patchAttr(row.removeConfirmBtn, 'hidden', !isSaved || !armedRemove); - patchAttr(row.removeConfirmBtn, 'disabled', !canWrite); - - const displayedPools = active ? quotas?.pools || [] : cached?.pools || []; - const poolCount = renderQuotaRows(row.poolsWrap, displayedPools); - const quotaError = active ? '' : quotaErrors.get(addr); - - let noteText = ''; - if (quotaError) { - noteText = t(key(quotaError)); - } else if (active && poolCount > 0 && quotas?.meta?.isStale) { - noteText = t(key('quotaStale')); - } else if (active && poolCount === 0) { - noteText = t(key('noQuota')); - } else if (!active && cached && poolCount === 0) { - noteText = t(key('emptyCredentialQuota')); - } - patchText(row.noteEl, noteText); - patchClasses(row.noteEl, { 'is-error': Boolean(quotaError) }); - patchAttr(row.noteEl, 'hidden', !noteText); - }, - }); + patchAttr(emptyPanel, 'hidden', !showEmpty); props.onLayoutChange?.(); } @@ -1048,6 +1165,7 @@ export const SettingsAccountTab = defineComponent({ } async function request(action, fields = {}) { + if (action !== 'list' && !draftRiskAccepted) return; if (action === 'quota') return refreshQuota(fields.email, Boolean(fields.manual)); if (operation) return; operation = action; @@ -1066,6 +1184,9 @@ export const SettingsAccountTab = defineComponent({ if (typeof result?.actionNonce === 'string' && result.actionNonce) { actionNonce = result.actionNonce; } + if (result?.quotaHistory && typeof result.quotaHistory === 'object') { + quotaHistory = { ...quotaHistory, ...result.quotaHistory }; + } if (result?.error) { if (result.error === 'unsupported') supported = false; if (result.error === 'vaultLocked') { @@ -1081,6 +1202,7 @@ export const SettingsAccountTab = defineComponent({ return; } supported = result?.supported === true; + riskLockedFromServer = Boolean(result?.riskLocked); accounts = result?.accounts || []; selectedEmail = result?.selectedEmail || ''; switchState = result?.switchState || null; @@ -1101,7 +1223,7 @@ export const SettingsAccountTab = defineComponent({ savedQuotas.set(addr, snapshot); } syncWarmCache(); - if (action !== 'switch' && !isVaultLocked()) { + if (action !== 'switch' && draftRiskAccepted && !riskLockedFromServer && !isVaultLocked()) { void refreshSavedQuotas(); } if (action === 'save' || action === 'capture') { @@ -1157,7 +1279,7 @@ export const SettingsAccountTab = defineComponent({ return; } await request('list'); - if (!disposed && !isVaultLocked() && email !== resolveActiveEmail()) { + if (!disposed && draftRiskAccepted && !isVaultLocked() && email !== resolveActiveEmail()) { void refreshQuota(email, false); } } @@ -1192,13 +1314,12 @@ export const SettingsAccountTab = defineComponent({ } async function refreshSavedQuotas() { - if (refreshingSavedQuotas || isVaultLocked()) return; + if (refreshingSavedQuotas || !draftRiskAccepted || isVaultLocked()) return; refreshingSavedQuotas = true; try { const activeEmail = resolveActiveEmail(); const ttlMs = refreshMinutes * 60_000; const staleTargets = accounts.filter((addr) => { - // Active account exclusively uses host telemetry — never poll via OAuth if (!addr || addr === activeEmail) return false; const cached = savedQuotas.get(addr); if (needsLegacyUltraTierRefresh(cached)) return true; @@ -1206,7 +1327,7 @@ export const SettingsAccountTab = defineComponent({ }); const BATCH_SIZE = 3; for (let i = 0; i < staleTargets.length; i += BATCH_SIZE) { - if (disposed || operation === 'switch' || isVaultLocked()) break; + if (disposed || operation === 'switch' || !draftRiskAccepted || isVaultLocked()) break; const batch = staleTargets.slice(i, i + BATCH_SIZE); await Promise.all(batch.map((addr) => refreshQuota(addr, false))); } @@ -1217,7 +1338,14 @@ export const SettingsAccountTab = defineComponent({ async function refreshQuota(addr, manual = false) { const activeEmail = resolveActiveEmail(); - if (!addr || addr === activeEmail || isVaultLocked() || quotaInFlight.has(addr) || disposed) { + if ( + !addr || + addr === activeEmail || + !draftRiskAccepted || + isVaultLocked() || + quotaInFlight.has(addr) || + disposed + ) { return; } const hasCachedPools = Boolean(savedQuotas.get(addr)?.pools?.length); @@ -1249,9 +1377,10 @@ export const SettingsAccountTab = defineComponent({ const nextTierId = result.tierId || prev.tierId || ''; const nextTierName = result.tierName || prev.tierName || ''; const nextTierMult = result.tierMultiplier || prev.tierMultiplier || ''; + const fetchedAt = result.fetchedAt || Date.now(); savedQuotas.set(addr, { pools: result.pools, - fetchedAt: result.fetchedAt || Date.now(), + fetchedAt, ...(nextTierId ? { tierId: nextTierId } : {}), ...(nextTierName ? { tierName: nextTierName } : {}), ...(nextTierMult ? { tierMultiplier: nextTierMult } : {}), @@ -1340,24 +1469,41 @@ export const SettingsAccountTab = defineComponent({ settingsData?.accountQuotaRefreshMinutes !== undefined ? normalizeAccountQuotaRefreshMinutes(settingsData.accountQuotaRefreshMinutes) : refreshMinutes; - if (account === value && quotas === quotaData && refreshMinutes === nextRefreshMins) return; + const nextRiskAccepted = + acceptingRisk || settingsData?.accountSwitchRiskAccepted === undefined + ? draftRiskAccepted + : Boolean(settingsData.accountSwitchRiskAccepted); + if ( + account === value && + quotas === quotaData && + refreshMinutes === nextRefreshMins && + draftRiskAccepted === nextRiskAccepted + ) { + return; + } + const becameRiskUnlocked = !draftRiskAccepted && nextRiskAccepted; account = value; quotas = quotaData; refreshMinutes = nextRefreshMins; + draftRiskAccepted = nextRiskAccepted; render(); + if (becameRiskUnlocked && riskLockedFromServer && !operation) { + void request('list'); + } } const quotaScheduleTimer = setInterval(() => { - if (!disposed && accounts.length > 0 && !operation && !isVaultLocked()) { + if (!disposed && draftRiskAccepted && accounts.length > 0 && !operation && !isVaultLocked()) { void refreshSavedQuotas(); } }, 60_000); quotaScheduleTimer.unref?.(); const root = h('div', { className: 'settings-tab-stack settings-account-root' }, [ - toolbarPanel, - cardsHost, + quotaTrendCard.el, + activeCardHost, emptyPanel, + riskStage, ]); render(); onCleanup(() => { @@ -1367,6 +1513,8 @@ export const SettingsAccountTab = defineComponent({ if (statusClearTimer) clearTimeout(statusClearTimer); if (confirmClearTimer) clearTimeout(confirmClearTimer); refreshIntervalSelect.destroy(); + pwdModeSegment.destroy(); + quotaTrendCard.destroy(); loginGeneration += 1; credential.value = ''; clearPasswordInputs(); @@ -1375,9 +1523,17 @@ export const SettingsAccountTab = defineComponent({ el: root, refs: { renderAccount, - refreshLabels: render, + refreshLabels: () => { + quotaTrendCard.refs.refreshLabels(); + render(); + }, preload: () => (supported === null ? request('list') : Promise.resolve()), - load: () => (supported === null ? request('list') : refreshSavedQuotas()), + load: () => + supported === null || (draftRiskAccepted && riskLockedFromServer) + ? request('list') + : draftRiskAccepted + ? refreshSavedQuotas() + : Promise.resolve(), }, }; }, diff --git a/sidecars/panel/styles.css b/sidecars/panel/styles.css index 0f49bde..7095733 100644 --- a/sidecars/panel/styles.css +++ b/sidecars/panel/styles.css @@ -6271,15 +6271,15 @@ html[data-standalone="true"] .app-shell { } .account-toolbar-panel { - padding: 9px 12px; - gap: 8px; + padding: var(--space-3) var(--space-5); + gap: var(--space-2); } .account-toolbar-row { display: flex; align-items: center; justify-content: space-between; - gap: 8px; + gap: var(--space-2); flex-wrap: wrap; } @@ -6287,6 +6287,7 @@ html[data-standalone="true"] .app-shell { .account-toolbar-right { display: inline-flex; align-items: center; + flex-wrap: wrap; gap: 6px; min-width: 0; } @@ -6295,32 +6296,19 @@ html[data-standalone="true"] .app-shell { margin-left: auto; } -.account-toolbar-btn { - display: inline-flex; - align-items: center; - gap: 5px; - min-height: calc(25px * var(--ui-font-scale, 1)); - height: auto; +.account-toolbar-panel .btn-primary, +.account-toolbar-panel .btn-secondary { + min-height: calc(26px * var(--ui-font-scale, 1)); padding: 2px 10px; font-size: var(--fs-md); - font-weight: 500; - border-radius: var(--radius-xs); white-space: nowrap; } -.account-toolbar-btn.is-open { +.account-toolbar-panel .btn-secondary.is-open { color: var(--accent-primary); background: var(--accent-primary-soft); } -.account-reload-btn { - width: calc(25px * var(--ui-font-scale, 1)); - height: calc(25px * var(--ui-font-scale, 1)); - padding: 0; - border-radius: var(--radius-xs); - flex-shrink: 0; -} - .account-refresh-select { flex: 0 0 auto; width: auto; @@ -6329,24 +6317,11 @@ html[data-standalone="true"] .app-shell { .account-refresh-select .select-trigger { width: auto; - min-height: calc(25px * var(--ui-font-scale, 1)); + min-height: calc(26px * var(--ui-font-scale, 1)); height: auto; padding: 2px 8px; gap: 5px; font-size: var(--fs-sm); - border: 1px solid var(--border-hairline); - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -[data-modern-ui="true"] .account-refresh-select .select-trigger { - border: none; - background: var(--modern-surface-subtle); -} - -[data-modern-ui="true"] .account-refresh-select .select-trigger:hover, -[data-modern-ui="true"] .account-refresh-select.is-open .select-trigger { - background: var(--modern-surface-hover); } .account-refresh-select .select-menu { @@ -6373,17 +6348,18 @@ html[data-standalone="true"] .app-shell { color: var(--metric-danger); } -.account-status-inline.is-info { - color: var(--text-secondary); -} - .account-import-drawer { display: flex; flex-direction: column; - gap: 5px; + align-items: stretch; + gap: 6px; padding-top: 2px; } +.account-import-drawer > .segmented { + align-self: flex-start; +} + .account-import-row { display: flex; align-items: center; @@ -6397,176 +6373,20 @@ html[data-standalone="true"] .app-shell { min-height: calc(26px * var(--ui-font-scale, 1)); } -.account-import-hint { - font-size: var(--fs-xs); - color: var(--text-muted); -} - -.account-unlock-box { - display: flex; - flex-direction: column; - gap: 6px; - padding: 8px 10px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -.account-unlock-head { - display: flex; - align-items: center; - justify-content: space-between; - gap: 8px; -} - -.account-unlock-title { - display: inline-flex; - align-items: center; - gap: 6px; - font-size: var(--fs-sm); - color: var(--text-secondary); -} - -.account-unlock-title .svg-icon { - color: var(--metric-warn); - flex-shrink: 0; -} - -.account-link-btn { - padding: 0; - border: none; - background: transparent; - font-size: var(--fs-xs); - color: var(--text-muted); - cursor: pointer; - white-space: nowrap; -} - -.account-link-btn:hover { - color: var(--text-primary); - text-decoration: underline; -} - -.account-reset-row { - display: flex; - align-items: center; - justify-content: space-between; - flex-wrap: wrap; - gap: 6px 10px; - padding-top: 4px; -} - -.account-reset-row .settings-action-desc { - flex: 1 1 180px; - font-size: var(--fs-xs); -} - -.account-pwd-tabs { - display: inline-flex; - align-items: center; - align-self: flex-start; - gap: 4px; - padding: 2px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); -} - -.account-pwd-tab { - padding: 2px 8px; - border: none; - border-radius: var(--radius-xs); - background: transparent; - font-size: var(--fs-xs); - font-weight: 500; - color: var(--text-secondary); - cursor: pointer; -} - -.account-pwd-tab.is-active { - background: var(--bg-elevated); - color: var(--text-primary); -} - -.account-card-btn.is-confirm-warn { - color: var(--accent-primary); - background: var(--accent-primary-soft); - font-weight: 600; -} - -.account-confirm-danger-btn { - color: var(--metric-danger); - background: color-mix(in srgb, var(--metric-danger) 12%, transparent); - font-weight: 600; -} - -.account-confirm-danger-btn:hover:not(:disabled) { - background: color-mix(in srgb, var(--metric-danger) 20%, transparent); -} - -.account-flow-strip { - display: flex; - align-items: center; - justify-content: space-between; - flex-wrap: wrap; - gap: 6px 10px; - padding: 6px 9px; - border-radius: var(--radius-xs); - background: var(--surface-subtle); - font-size: var(--fs-sm); -} - -.account-flow-strip.is-info { - background: var(--accent-primary-soft); -} - -.account-flow-strip.is-info .account-flow-msg { - color: var(--accent-primary); -} - -.account-flow-strip.is-ok { - background: color-mix(in srgb, var(--metric-ok) 10%, transparent); -} - -.account-flow-strip.is-ok .account-flow-msg { - color: var(--metric-ok); -} - -.account-flow-strip.is-warn { - background: color-mix(in srgb, var(--metric-warn) 10%, transparent); - color: var(--metric-warn); -} - -.account-flow-strip.is-error { - background: color-mix(in srgb, var(--metric-danger) 10%, transparent); -} - -.account-flow-strip.is-error .account-flow-msg { - color: var(--metric-danger); -} - -.account-flow-link { - font-size: var(--fs-sm); - font-weight: 500; - color: var(--accent-primary); - text-decoration: none; - white-space: nowrap; -} - -.account-flow-link:hover { - text-decoration: underline; -} - .account-cards-stack { display: flex; flex-direction: column; gap: var(--space-3); } +/* Optically symmetrical vertical padding: 11px + 3px header leading == 14px bottom bar-to-edge */ .account-card { gap: 10px; - padding: 11px 14px; + padding: 11px var(--space-5) 14px; } .account-card-head { + min-height: calc(22px * var(--ui-font-scale, 1)); gap: 8px; flex-wrap: wrap; } @@ -6580,14 +6400,10 @@ html[data-standalone="true"] .app-shell { flex: 1 1 150px; } -.account-active-dot { - background: var(--metric-ok); - box-shadow: 0 0 0 2px color-mix(in srgb, var(--metric-ok) 20%, transparent); -} - .account-card-email { font-size: var(--fs-base); font-weight: 600; + line-height: 1.3; color: var(--text-primary); overflow: hidden; text-overflow: ellipsis; @@ -6595,34 +6411,6 @@ html[data-standalone="true"] .app-shell { max-width: 100%; } -.account-tag { - display: inline-flex; - align-items: center; - min-height: calc(18px * var(--ui-font-scale, 1)); - padding: 1px 6px; - font-size: var(--fs-xs); - font-weight: 600; - line-height: 1.2; - border-radius: var(--radius-xs); - border: none; - white-space: nowrap; -} - -.account-tag.is-active { - color: var(--metric-ok); - background: color-mix(in srgb, var(--metric-ok) 12%, transparent); -} - -.account-tag.is-tier { - color: var(--accent-primary); - background: var(--accent-primary-soft); -} - -.account-tag.is-written { - color: var(--metric-warn); - background: color-mix(in srgb, var(--metric-warn) 12%, transparent); -} - .account-card-actions { display: inline-flex; align-items: center; @@ -6631,21 +6419,16 @@ html[data-standalone="true"] .app-shell { margin-left: auto; } -.account-card-btn { - min-height: calc(23px * var(--ui-font-scale, 1)); - height: auto; +.account-card-actions .btn-ghost-sm { + min-height: calc(22px * var(--ui-font-scale, 1)); padding: 1px 8px; - font-size: var(--fs-sm); - font-weight: 500; - border-radius: var(--radius-xs); - white-space: nowrap; } -.account-icon-btn { - width: calc(23px * var(--ui-font-scale, 1)); - height: calc(23px * var(--ui-font-scale, 1)); +.account-card-actions .icon-btn { + width: calc(22px * var(--ui-font-scale, 1)); + height: calc(22px * var(--ui-font-scale, 1)); + min-width: calc(22px * var(--ui-font-scale, 1)); padding: 0; - border-radius: var(--radius-xs); } .account-pools-stack { @@ -6654,18 +6437,11 @@ html[data-standalone="true"] .app-shell { gap: 9px; } -.account-pool-group { - display: flex; - flex-direction: column; - gap: 4px; -} - -.account-pool-name { +.account-pool-label { font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted); line-height: 1.25; - letter-spacing: 0.01em; } .account-pool-meters { @@ -6691,34 +6467,61 @@ html[data-standalone="true"] .app-shell { min-width: 0; } -.account-meter-head { +/* Quota Trend Card minor layout helpers */ +.account-trend-toprow { + gap: var(--space-2); + flex-wrap: wrap; +} + +.account-trend-toprow .cons-cycle-pool-select { + flex: 0 0 auto; +} + +.account-trend-subbar { + gap: 6px 8px; + flex-wrap: wrap; +} + +.account-trend-email-select { + max-width: 156px; +} + +.account-trend-legend { display: flex; - align-items: baseline; - justify-content: space-between; - gap: 6px; - min-width: 0; + align-items: center; + flex-wrap: wrap; + gap: 6px 12px; + padding-top: 2px; } -.settings-account-quota { - font-size: var(--fs-sm); - font-weight: 600; +.account-trend-leg-item { + display: inline-flex; + align-items: center; + gap: 5px; + font-size: var(--fs-xs); + color: var(--text-secondary); +} + +.account-trend-leg-item strong { color: var(--text-primary); - white-space: nowrap; } -.account-meter-reset { - font-size: var(--fs-2xs); - color: var(--text-muted); - white-space: nowrap; - flex-shrink: 0; +.account-trend-tip-rows { + display: flex; + flex-direction: column; + gap: 4px; } -.account-progress-track { - height: 5px; +.account-trend-tip-row { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + font-size: var(--fs-xs); } .account-empty-panel { - padding: 18px 14px; + padding: 16px var(--space-5); align-items: center; justify-content: center; } diff --git a/sidecars/panel/test/account-switch.test.mjs b/sidecars/panel/test/account-switch.test.mjs index a66f73f..590188f 100644 --- a/sidecars/panel/test/account-switch.test.mjs +++ b/sidecars/panel/test/account-switch.test.mjs @@ -24,7 +24,15 @@ import { resetVaultUnlockFailures, verifyAccountActionNonce, } from '../backend/account-vault-crypto.mjs'; +import { + createAccountQuotaHistoryTracker, + sanitizeQuotaHistoryPoints, +} from '../backend/account-quota-history.mjs'; import { executeAccountCredentialAction } from '../backend/api-actions.mjs'; +import { + buildQuotaWindowSeries, + resolveQuotaWindowBounds, +} from '../src/components/widgets/AccountQuotaTrendCard.js'; test('accepts a shared OAuth credential or refresh token, and rejects other credential types', () => { const refreshToken = '1//synthetic-refresh-token-for-test'; @@ -412,13 +420,70 @@ test('enforces OAuth client_id allowlist, minimizes at-rest vault payload, and v assert.equal(verifyAccountActionNonce(`${nonce}x`, 1_700_000_000_000), false); assert.equal(verifyAccountActionNonce(nonce, 1_700_000_000_000 + 45 * 60_000), false); - const missingNonceRes = await executeAccountCredentialAction({ + // Default risk gate blocks mutations until accepted, and list returns riskLocked without vault access + const riskBlockedRes = await executeAccountCredentialAction({ action: 'remove', email: 'a@example.com', }); + assert.equal(JSON.parse(riskBlockedRes.body).error, 'riskNotAccepted'); + + const riskLockedList = JSON.parse((await executeAccountCredentialAction(undefined)).body); + assert.equal(riskLockedList.riskLocked, true); + assert.deepEqual(riskLockedList.accounts, []); + + // Once risk check passes, mutations still require a valid HMAC actionNonce + const missingNonceRes = await executeAccountCredentialAction( + { action: 'remove', email: 'a@example.com' }, + { bypassRiskCheck: true } + ); const missingBody = JSON.parse(missingNonceRes.body); assert.equal(missingBody.error, 'invalidActionNonce'); assert.equal(typeof missingBody.actionNonce, 'string'); + + // Quota history tracker compresses flat heartbeats while preserving real dips and QR seeds + let persistedHistory = null; + const histTracker = createAccountQuotaHistoryTracker({ + loadState: () => null, + saveState: (p) => { + persistedHistory = p; + }, + loadSeedEvents: () => ({ + events: [ + { ts: '2026-11-24T08:00:00.000Z', pool: 'Gemini', win: '5h', usedPct: 4.0 }, + { ts: '2026-11-24T09:00:00.000Z', pool: 'Gemini', win: '5h', usedPct: 10.4 }, + ], + }), + }); + const baseTs = Date.parse('2026-11-24T10:00:00.000Z'); + histTracker.recordSnapshot( + 'owner@example.com', + [ + { poolKey: 'Gemini', remainingPct: 89.6, weeklyRemainingPct: 89.4 }, + { poolKey: 'Claude & GPT', remainingPct: 100, weeklyRemainingPct: 100 }, + ], + baseTs, + { isActiveHost: true } + ); + histTracker.recordSnapshot( + 'owner@example.com', + [{ poolKey: 'Claude & GPT', remainingPct: 100, weeklyRemainingPct: 100 }], + baseTs + 20 * 60_000 + ); + histTracker.recordSnapshot( + 'owner@example.com', + [{ poolKey: 'Claude & GPT', remainingPct: 100, weeklyRemainingPct: 100 }], + baseTs + 40 * 60_000 + ); + histTracker.flush(baseTs + 40 * 60_000); + const payload = histTracker.getHistoryPayload({ allowedEmails: ['owner@example.com'] }); + assert.equal( + payload['owner@example.com'].pools['Claude & GPT'].points.length, + 2, + 'Collinear flat heartbeats must compress in-place to start + latest endpoints' + ); + assert.equal(payload['owner@example.com'].pools.Gemini.points[0][1], 96); + assert.equal(payload['owner@example.com'].pools.Gemini.points.at(-1)[1], 89.6); + assert.ok(persistedHistory?.accounts?.['owner@example.com']); }); test('secondary passphrase encrypts credentials with scrypt + AES-256-GCM, preserves cached list while locked, throttles brute force, and rotates/resets cleanly', { skip: process.platform !== 'win32' }, async (t) => { @@ -595,3 +660,108 @@ test('secondary passphrase encrypts credentials with scrypt + AES-256-GCM, prese ); assert.deepEqual(resetRes.accounts, []); }); + +test('cycle quota chart strips pre-reset tail points and cross-account impulse spikes', () => { + const cycleStartMs = 1795518262000; // 20:04:22 + const reset5hMs = cycleStartMs + 5 * 3600_000; // 01:04:22 + const nowMs = cycleStartMs + 3.5 * 3600_000; + + // Unused 100% pool appears first with rolling reset (now + 5h), active Gemini pool second + const pools = [ + { + poolKey: 'Argon', + label: 'Argon', + reset5hMs: nowMs + 5 * 3600_000 - 15_000, + resetWeeklyMs: nowMs + 7 * 86400_000, + live5h: 100, + liveWk: 100, + points: [[nowMs, 100, 100]], + }, + { + poolKey: 'Gemini', + label: 'Gemini', + reset5hMs, + resetWeeklyMs: nowMs + 6 * 86400_000, + live5h: 88.2, + liveWk: 89.2, + points: [ + [cycleStartMs - 47_000, 84.5, 92.4], // 20:03:35 pre-reset old cycle point + [cycleStartMs + 3_582, 100, 92.4], // 20:04:25 fresh cycle reset + [cycleStartMs + 58 * 60_000, 94.8, 90.6], // 21:02:00 + [cycleStartMs + 61 * 60_000, 100, 100], // 21:05:19 isolated cross-account init spike + [cycleStartMs + 63 * 60_000, 94.7, 90.6], // 21:07:33 back to real slope + [nowMs, 88.2, 89.2], + ], + }, + ]; + + const bounds = resolveQuotaWindowBounds({ + pools, + selectedRange: 'cycle', + selectedWin: '5h', + selectedPool: 'all', + nowMs, + }); + assert.equal(bounds.startMs, cycleStartMs, 'Active consumed pool must anchor cycle start'); + + const built = buildQuotaWindowSeries({ + visiblePools: pools, + rawBounds: bounds, + selectedRange: 'cycle', + selectedWin: '5h', + nowMs, + }); + const geminiSeries = built.rawBuilt.find((s) => s.poolKey === 'Gemini'); + assert.ok(geminiSeries); + assert.equal( + geminiSeries.rawPoints[0].val, + 100, + 'Cycle start must begin at 100% without carrying 84.5% from the expired cycle' + ); + for (let i = 1; i < geminiSeries.rawPoints.length; i++) { + const jump = geminiSeries.rawPoints[i].val - geminiSeries.rawPoints[i - 1].val; + assert.ok(jump < 1, `Unexpected upward spike inside cycle series: +${jump}`); + } + + // Even when a stale pre-reset poll lands slightly inside cycleStartMs due to clock jitter, + // cycle mode must slice after the reset jump instead of drawing an upward vertical wall. + const jitterBuilt = buildQuotaWindowSeries({ + visiblePools: [ + { + poolKey: 'Gemini', + label: 'Gemini', + reset5hMs, + resetWeeklyMs: 0, + live5h: 95, + liveWk: null, + points: [ + [cycleStartMs + 2_000, 84.5, 92.4], + [cycleStartMs + 15_000, 100, 92.4], + [nowMs, 95, 91.8], + ], + }, + ], + rawBounds: bounds, + selectedRange: 'cycle', + selectedWin: '5h', + nowMs, + }); + assert.equal(jitterBuilt.rawBuilt[0].rawPoints[0].val, 100); + + // Backend history sanitizer removes isolated cross-account impulse spikes while keeping real resets + const persistedPts = [ + [cycleStartMs - 60_000, 84.5, 92.4], + [cycleStartMs + 3_000, 100, 92.4], + [cycleStartMs + 58 * 60_000, 94.8, 90.6], + [cycleStartMs + 61 * 60_000, 100, 100], + [cycleStartMs + 63 * 60_000, 94.7, 90.6], + ]; + assert.equal(sanitizeQuotaHistoryPoints(persistedPts), true); + assert.equal(persistedPts.length, 4); + assert.equal(persistedPts[1][1], 100, 'Genuine 5h reset stays intact for multi-cycle views'); + assert.equal( + persistedPts.some((p) => p[0] === cycleStartMs + 61 * 60_000), + false, + 'Isolated cross-account [100, 100] spike is removed' + ); +}); diff --git a/sidecars/panel/test/backend-harness.mjs b/sidecars/panel/test/backend-harness.mjs index fb4dfda..641ef63 100644 --- a/sidecars/panel/test/backend-harness.mjs +++ b/sidecars/panel/test/backend-harness.mjs @@ -33,16 +33,17 @@ const BACKEND_MODULE_FILES = [ '../shared/prompt-clean.mjs', '../backend/fs-atomic.mjs', '../backend/context-windows.mjs', - '../backend/account-oauth.mjs', - '../backend/account-login.mjs', - '../backend/account-vault-crypto.mjs', - '../backend/account-switch.mjs', '../backend/http-helpers.mjs', '../backend/settings-schema.mjs', '../backend/sqlite-client.mjs', '../backend/trajectory-sqlite-ledger.mjs', '../backend/trajectory-cache-store.mjs', '../backend/config.mjs', + '../backend/account-quota-history.mjs', + '../backend/account-oauth.mjs', + '../backend/account-login.mjs', + '../backend/account-vault-crypto.mjs', + '../backend/account-switch.mjs', '../backend/endpoint-registry.mjs', '../backend/proto-wire.mjs', '../backend/cli-sqlite-reader.mjs', diff --git a/sidecars/panel/test/window-bridge.test.mjs b/sidecars/panel/test/window-bridge.test.mjs index 437e030..be84a3f 100644 --- a/sidecars/panel/test/window-bridge.test.mjs +++ b/sidecars/panel/test/window-bridge.test.mjs @@ -2062,6 +2062,30 @@ test('Compiled app.js mounts dual Shadow DOM hosts, tracks /c/ routes, pro await new Promise((r) => setTimeout(r, 35)); assert.equal(accountPane.hasAttribute('hidden'), false); assert.equal(accountPane.querySelector('.settings-account-identity').textContent, 'test@example.com'); + assert.equal(accountPane.querySelector('.account-active-dot'), null, 'Active account must not render a green dot'); + + // Quota trend chart is visible even before risk acceptance, using full 184px .trend-svg height and all 8 presets + const quotaTrendPanel = accountPane.querySelector('.account-quota-trend-panel'); + assert.ok(quotaTrendPanel && !quotaTrendPanel.hasAttribute('hidden'), 'Quota trend chart card must be visible'); + const quotaTrendSvg = quotaTrendPanel.querySelector('svg.trend-svg'); + assert.ok(quotaTrendSvg, 'Quota trend chart must reuse 184px .trend-svg'); + assert.equal(quotaTrendSvg.getAttribute('viewBox'), '0 0 460 184'); + const trendSegLabels = quotaTrendPanel.querySelectorAll('.seg-btn').map((b) => b.textContent); + for (const expectedRange of ['周期', '今天', '1天', '7天', '14天', '30天', '全部', '自定义']) { + assert.ok(trendSegLabels.includes(expectedRange), `Quota trend chart must include ${expectedRange}`); + } + + // Default risk gate locks multi-account management until explicitly accepted + const accountRiskMask = accountPane.querySelector('.settings-account-risk-mask'); + assert.ok(accountRiskMask && !accountRiskMask.hasAttribute('hidden'), 'Multi-account risk mask must be visible by default'); + assert.equal(accountRequests.some((item) => item.action === 'quota'), false, 'Must not poll backup quotas before risk acceptance'); + + const accountRiskAcceptBtn = accountPane.querySelector('.settings-account-risk-accept'); + accountRiskAcceptBtn.dispatchEvent({ type: 'click', stopPropagation() {} }); + await new Promise((r) => setTimeout(r, 35)); + assert.equal(mockSettings.accountSwitchRiskAccepted, true); + assert.equal(accountRiskMask.hasAttribute('hidden'), true); + assert.ok(accountPane.querySelectorAll('.settings-account-quota').some((el) => el.textContent === '5 小时 70%')); assert.ok(accountRequests.some((item) => item.action === 'quota' && item.email === 'next@example.com')); const googleLogin = accountPane.querySelector('.settings-account-login');