diff --git a/changelog.d/fixed-15880.md b/changelog.d/fixed-15880.md new file mode 100644 index 0000000000..d5d832924a --- /dev/null +++ b/changelog.d/fixed-15880.md @@ -0,0 +1 @@ +- dashboard: merge banners drain the toast countdown bar (#15880) diff --git a/src/pkg/dashboard/stage_tracker_cues_test.go b/src/pkg/dashboard/stage_tracker_cues_test.go index b1a1a37a4d..5d504617ca 100644 --- a/src/pkg/dashboard/stage_tracker_cues_test.go +++ b/src/pkg/dashboard/stage_tracker_cues_test.go @@ -359,6 +359,8 @@ func stageTrackerCelebrateJS(t *testing.T, html string) string { jsFunc(t, html, "celebrateDetached"), jsFunc(t, html, "stageTrackerMergeBannerNumber"), jsFunc(t, html, "stageTrackerMergeBannerText"), + jsFunc(t, html, "toastCountdownBar"), + jsFunc(t, html, "toastCountdownCancel"), jsFunc(t, html, "stageTrackerMergeBanner"), jsFunc(t, html, "stageTrackerMergeBannerDismiss"), jsFunc(t, html, "hamBannerDismiss"), @@ -417,6 +419,7 @@ function el(tag, attrs) { querySelector(sel) { return e.querySelectorAll(sel)[0] || null; }, querySelectorAll(sel) { const out = []; const unesc = v => v.replace(/\\(.)/g, '$1'); const m = sel.match(/^\[([\w-]+)="(.*)"\]$/); const word = sel.match(/^\[([\w-]+)~="(.*)"\]\[data-ham-key\]$/); const cls = sel.match(/^\.([\w-]+)/); const capOfRow = sel === '.ham-row[data-ham-key] .ham-seg.cap'; const walk = n => { for (const c of n.children) { if (capOfRow) { if (c.classList.contains('cap') && c.closest('.ham-row') && c.closest('.ham-row').classList.contains('ham-row')) out.push(c); } else if (m && c.attrs[m[1]] === unesc(m[2])) out.push(c); else if (word && c.attrs['data-ham-key'] != null && String(c.attrs[word[1]] || '').split(' ').includes(unesc(word[2]))) out.push(c); else if (!m && !word && cls && c.classList.contains(cls[1]) && (!sel.includes('[data-at]') || c.attrs['data-at'] != null)) out.push(c); walk(c); } }; walk(e); return out; }, closest(sel) { let n = e; while (n) { if (n.attrs && n.attrs['data-ham-key'] != null) return n; n = n.parentNode; } return null; }, + matches() { return false; }, contains(x) { return x === e || e.children.some(c => c.contains(x)); }, }; return e; } @@ -424,7 +427,7 @@ global.CSS = { escape: s => String(s).replace(/([^\w-])/g, '\\$1') }; const body = el('body'); const panel = el('div'); const chip = el('button', { id: 'activity-monitor-merged' }); const n = el('span'); n.classList.add('ham-merged-n'); n.textContent = '0'; chip.appendChild(n); const sub = el('span'); sub.classList.add('ham-merged-sub'); chip.appendChild(sub); chip.rect = { left: 500, top: 10, width: 60, height: 20 }; const pop = el('div', { id: 'activity-monitor-merged-pop' }); pop.hidden = true; const banners = el('div'); const live = el('span'); const byId = { 'activity-monitor-panel': panel, 'activity-monitor-merged': chip, 'activity-monitor-merged-pop': pop, 'merge-banners': banners, 'activity-monitor-merged-live': live }; -global.document = { body, hidden: false, getElementById(id) { return byId[id] || null; }, createElement(tag) { if (tag === 'template') { const tpl = el('template'); Object.defineProperty(tpl, 'innerHTML', { set(v) { const c = el('p'); c.classList.add('cue-event'); c.attrs['data-cue-key'] = (v.match(/data-cue-key="([^"]*)"/) || [])[1]; c.textContent = v.replace(/<[^>]+>/g, ''); const age = el('span'); age.classList.add('cue-event-age'); age.attrs['data-at'] = (v.match(/data-at="(\d+)"/) || [])[1]; c.appendChild(age); tpl.content = { firstElementChild: c }; } }); return tpl; } return el(tag); } }; +global.document = { body, hidden: false, activeElement: null, getElementById(id) { return byId[id] || null; }, createElement(tag) { if (tag === 'template') { const tpl = el('template'); Object.defineProperty(tpl, 'innerHTML', { set(v) { const c = el('p'); c.classList.add('cue-event'); c.attrs['data-cue-key'] = (v.match(/data-cue-key="([^"]*)"/) || [])[1]; c.textContent = v.replace(/<[^>]+>/g, ''); const age = el('span'); age.classList.add('cue-event-age'); age.attrs['data-at'] = (v.match(/data-at="(\d+)"/) || [])[1]; c.appendChild(age); tpl.content = { firstElementChild: c }; } }); return tpl; } return el(tag); } }; const store = {}; global.localStorage = { getItem: k => (k in store ? store[k] : null), setItem(k, v) { store[k] = String(v); } }; const session = {}; global.sessionStorage = { getItem: k => (k in session ? session[k] : null), setItem(k, v) { session[k] = String(v); } }; let reduced = false; function stageTrackerReducedMotion() { return reduced; } diff --git a/src/pkg/dashboard/stage_tracker_merge_banner_countdown.test.js b/src/pkg/dashboard/stage_tracker_merge_banner_countdown.test.js new file mode 100644 index 0000000000..6ae96f5a36 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_merge_banner_countdown.test.js @@ -0,0 +1,129 @@ +// Activity Monitor "#N merged" banners: the auto-dismiss goes through +// toastCountdownBar, so the banner drains the same .toast-bar every other +// timed toast shows (CSS keys on .toast-bar + --toast-ttl), hover holds it, +// and the manual × dismiss clears the pending timer. The functions are +// lifted straight out of index.html so this runs against the shipped source. +const test = require('node:test'); +const assert = require('node:assert'); +const fs = require('node:fs'); +const path = require('node:path'); + +const html = fs.readFileSync(path.join(__dirname, 'static', 'index.html'), 'utf8'); + +function pick(re, what) { + const m = html.match(re); + assert.ok(m, what + ' not found in index.html'); + return m[0]; +} + +// A minimal DOM: enough for toastCountdownBar and the banner builder. +function makeDom() { + const timers = []; + class El { + constructor(tag) { + this.tagName = tag; this.children = []; this.parentNode = null; this.attrs = {}; this.listeners = {}; + this.style = { props: {}, setProperty(k, v) { this.props[k] = v; } }; + this.className = ''; this.innerHTML = ''; this.id = ''; + } + get classList() { const self = this; return { add(c) { self.className += ' ' + c; }, remove() {}, contains(c) { return self.className.split(' ').includes(c); } }; } + appendChild(c) { if (c.parentNode) c.parentNode.removeChild(c); c.parentNode = this; this.children.push(c); return c; } + removeChild(c) { this.children = this.children.filter(x => x !== c); c.parentNode = null; } + remove() { if (this.parentNode) this.parentNode.removeChild(this); } + setAttribute(k, v) { this.attrs[k] = String(v); } + getAttribute(k) { return this.attrs[k] === undefined ? null : this.attrs[k]; } + addEventListener(n, fn) { (this.listeners[n] = this.listeners[n] || []).push(fn); } + fire(n, e) { for (const fn of this.listeners[n] || []) fn(e || {}); } + matches() { return false; } + contains(x) { return x === this || this.children.some(c => c.contains(x)); } + querySelector() { return null; } + closest(sel) { for (let n = this; n; n = n.parentNode) if (sel === '.merge-banner' && n.className && n.className.split(' ').includes('merge-banner')) return n; return null; } + } + const body = new El('body'); + const byId = {}; + const document = { + body, activeElement: null, + createElement: t => new El(t), + getElementById: id => byId[id] || null, + }; + const origAppend = body.appendChild.bind(body); + body.appendChild = c => { if (c.id) byId[c.id] = c; return origAppend(c); }; + const setTimeout = (fn, ms) => { const t = { fn, ms, cleared: false }; timers.push(t); return t; }; + const clearTimeout = t => { if (t) t.cleared = true; }; + return { document, timers, setTimeout, clearTimeout, El }; +} + +const BANNER_MS = 30000; +const src = [ + 'const window = {};', + 'function esc(s) { return typeof s === "string" ? s.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """) : ""; }', + 'const _stageTrackerMerged = { server: { recent: [] } };', + 'const _stageTrackerPRTitle = {};', + 'function stageTrackerMergedIdent(repo, n) { return repo + "#" + n; }', + 'function stageTrackerMergedByText() { return ""; }', + 'function stageTrackerReducedMotion() { return true; }', + 'function stageTrackerAfter(el, ev, ms, fn) { fn(); }', + `function stageTrackerMergeBannerMs() { return ${BANNER_MS}; }`, + pick(/const STAGE_TRACKER_MERGE_BANNER_MAX = [^\n]*\n/, 'STAGE_TRACKER_MERGE_BANNER_MAX'), + pick(/function toastCountdownBar\(el, ms, onExpire\) \{[\s\S]*?\n \}\n/, 'toastCountdownBar'), + pick(/function toastCountdownCancel\(el\) \{[\s\S]*?\n \}\n/, 'toastCountdownCancel'), + pick(/function stageTrackerMergeBannerNumber\(b\) \{[\s\S]*?\n \}\n/, 'stageTrackerMergeBannerNumber'), + pick(/function stageTrackerMergeBannerText\(b, nowMs\) \{[\s\S]*?\n \}\n/, 'stageTrackerMergeBannerText'), + pick(/function stageTrackerMergeBanner\(b\) \{[\s\S]*?\n \}\n/, 'stageTrackerMergeBanner'), + pick(/function stageTrackerMergeBannerDismiss\(el\) \{[\s\S]*?\n \}\n/, 'stageTrackerMergeBannerDismiss'), + pick(/function hamBannerDismiss\(btn\) \{[\s\S]*?\n \}\n/, 'hamBannerDismiss'), +].join('\n'); + +function load() { + const dom = makeDom(); + const api = new Function('document', 'setTimeout', 'clearTimeout', src + '; return { banner: stageTrackerMergeBanner, dismiss: stageTrackerMergeBannerDismiss, hamBannerDismiss };')(dom.document, dom.setTimeout, dom.clearTimeout); + return Object.assign(dom, api); +} + +const merge = { key: 'hivecommons/hive/pr#15834', repo: 'hivecommons/hive', number: 15834, title: 'fix the thing', at: Date.now() }; + +test('merge banner carries the countdown bar and --toast-ttl the CSS drains over', () => { + const d = load(); + const el = d.banner(merge); + const bar = el.children.find(c => c.className === 'toast-bar'); + assert.ok(bar, 'a .toast-bar child is appended to the banner'); + assert.strictEqual(bar.getAttribute('aria-hidden'), 'true'); + assert.strictEqual(el.style.props['--toast-ttl'], BANNER_MS + 'ms', 'bar duration equals the banner duration'); + assert.ok(el._countdown, 'countdown state is armed'); + assert.strictEqual(d.timers.length, 1); + assert.strictEqual(d.timers[0].ms, BANNER_MS, 'dismiss timer uses stageTrackerMergeBannerMs()'); + assert.strictEqual(el._timer, undefined, 'no bare setTimeout handle remains'); +}); + +test('timer expiry removes the banner', () => { + const d = load(); + const el = d.banner(merge); + assert.ok(el.parentNode); + d.timers[0].fn(); + assert.strictEqual(el.parentNode, null, 'banner left the stack when the countdown fired'); +}); + +test('hover holds the countdown and the bar together; leaving re-arms the remainder', () => { + const d = load(); + const el = d.banner(merge); + const bar = el.children.find(c => c.className === 'toast-bar'); + assert.strictEqual(d.timers.length, 1); + el.fire('mouseenter'); + assert.ok(d.timers[0].cleared, 'timer cleared on hover'); + assert.strictEqual(bar.style.animationPlayState, 'paused'); + el.fire('mouseleave'); + assert.strictEqual(d.timers.length, 2, 'a fresh timer is armed on leave'); + assert.ok(d.timers[1].ms <= BANNER_MS && d.timers[1].ms > 0, 'remainder re-armed'); + assert.strictEqual(bar.style.animationPlayState, 'running'); +}); + +test('manual × dismiss clears the pending timer and the bar', () => { + const d = load(); + const el = d.banner(merge); + const close = new d.El('button'); + el.appendChild(close); + d.hamBannerDismiss(close); + assert.strictEqual(el.parentNode, null, 'banner removed'); + assert.ok(d.timers[0].cleared, 'auto-dismiss timer cleared so it cannot fire stale'); + assert.strictEqual(el._countdown, null); + assert.ok(!el.children.some(c => c.className === 'toast-bar'), 'bar removed with the countdown'); +}); diff --git a/src/pkg/dashboard/static-src/css/tracker.css b/src/pkg/dashboard/static-src/css/tracker.css index bdcf204c50..cdf28842e7 100644 --- a/src/pkg/dashboard/static-src/css/tracker.css +++ b/src/pkg/dashboard/static-src/css/tracker.css @@ -571,7 +571,8 @@ (just the ⟳ and the actor glyph). */ .ham-seg.active .ham-seg-t.ham-queue-stalled { color: inherit; } .merge-banners { position: fixed; right: var(--sp-6); bottom: var(--sp-6); z-index: var(--z-merge-banner); display: flex; flex-direction: column; gap: var(--sp-3); max-width: min(26rem, calc(100vw - 2 * var(--sp-6))); pointer-events: none; } - .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; animation: merge-banner-in 220ms ease-out both; } + .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; position: relative; overflow: hidden; animation: merge-banner-in 220ms ease-out both; } + .merge-banner .toast-bar { background: color-mix(in srgb, var(--green) 40%, transparent); } .merge-banner.merge-banner-out { animation: merge-banner-out 180ms ease-in both; } .merge-banner-ico { color: var(--green); font-weight: var(--fw-bold); } .merge-banner-head { font-weight: var(--fw-semibold); } diff --git a/src/pkg/dashboard/static-src/js/tracker-merged.js b/src/pkg/dashboard/static-src/js/tracker-merged.js index d100c047b4..47d6744662 100644 --- a/src/pkg/dashboard/static-src/js/tracker-merged.js +++ b/src/pkg/dashboard/static-src/js/tracker-merged.js @@ -925,7 +925,10 @@ // Stateful: a persistent banner, bottom right, for one merge — stays // until dismissed or stageTrackerMergeBannerMs() (twice the info toast // duration, STAGE_TRACKER_MERGE_BANNER_MS at the default), stacked when - // several, with a "show row" link while the row is on the board. + // several, with a "show row" link while the row is on the board. The + // auto-dismiss goes through toastCountdownBar like every other timed + // toast, so the banner drains a .toast-bar along its bottom edge and + // hover / focus inside it holds both timer and bar together. function stageTrackerMergeBanner(b) { if (!document.body) return null; let box = document.getElementById('merge-banners'); @@ -943,12 +946,12 @@ el.innerHTML = `
${num ? `#${esc(num)} ` : ''}merged${info.preview ? ' (test)' : ''}
${esc(t.text)}
`; box.appendChild(el); while (box.children.length > STAGE_TRACKER_MERGE_BANNER_MAX) box.children[0].remove(); - el._timer = setTimeout(() => stageTrackerMergeBannerDismiss(el), stageTrackerMergeBannerMs()); + toastCountdownBar(el, stageTrackerMergeBannerMs(), () => stageTrackerMergeBannerDismiss(el)); return el; } function stageTrackerMergeBannerDismiss(el) { if (!el || !el.parentNode) return; - if (el._timer) { clearTimeout(el._timer); el._timer = 0; } + toastCountdownCancel(el); if (stageTrackerReducedMotion()) { el.remove(); return; } el.classList.add('merge-banner-out'); stageTrackerAfter(el, 'animationend', 180, () => el.remove()); diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 4791999463..e6570e6fd8 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1908,7 +1908,8 @@ (just the ⟳ and the actor glyph). */ .ham-seg.active .ham-seg-t.ham-queue-stalled { color: inherit; } .merge-banners { position: fixed; right: var(--sp-6); bottom: var(--sp-6); z-index: var(--z-merge-banner); display: flex; flex-direction: column; gap: var(--sp-3); max-width: min(26rem, calc(100vw - 2 * var(--sp-6))); pointer-events: none; } - .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; animation: merge-banner-in 220ms ease-out both; } + .merge-banner { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: var(--sp-4); align-items: start; padding: var(--sp-4) var(--sp-5); border: var(--line-width) solid var(--line); border-left: 3px solid var(--green); border-radius: var(--r); background: var(--panel-strong); color: var(--text); box-shadow: var(--shadow-raised); font-size: var(--fs-sm); pointer-events: auto; position: relative; overflow: hidden; animation: merge-banner-in 220ms ease-out both; } + .merge-banner .toast-bar { background: color-mix(in srgb, var(--green) 40%, transparent); } .merge-banner.merge-banner-out { animation: merge-banner-out 180ms ease-in both; } .merge-banner-ico { color: var(--green); font-weight: var(--fw-bold); } .merge-banner-head { font-weight: var(--fw-semibold); } @@ -22812,7 +22813,10 @@

All labels

// Stateful: a persistent banner, bottom right, for one merge — stays // until dismissed or stageTrackerMergeBannerMs() (twice the info toast // duration, STAGE_TRACKER_MERGE_BANNER_MS at the default), stacked when - // several, with a "show row" link while the row is on the board. + // several, with a "show row" link while the row is on the board. The + // auto-dismiss goes through toastCountdownBar like every other timed + // toast, so the banner drains a .toast-bar along its bottom edge and + // hover / focus inside it holds both timer and bar together. function stageTrackerMergeBanner(b) { if (!document.body) return null; let box = document.getElementById('merge-banners'); @@ -22830,12 +22834,12 @@

All labels

el.innerHTML = `
${num ? `#${esc(num)} ` : ''}merged${info.preview ? ' (test)' : ''}
${esc(t.text)}
`; box.appendChild(el); while (box.children.length > STAGE_TRACKER_MERGE_BANNER_MAX) box.children[0].remove(); - el._timer = setTimeout(() => stageTrackerMergeBannerDismiss(el), stageTrackerMergeBannerMs()); + toastCountdownBar(el, stageTrackerMergeBannerMs(), () => stageTrackerMergeBannerDismiss(el)); return el; } function stageTrackerMergeBannerDismiss(el) { if (!el || !el.parentNode) return; - if (el._timer) { clearTimeout(el._timer); el._timer = 0; } + toastCountdownCancel(el); if (stageTrackerReducedMotion()) { el.remove(); return; } el.classList.add('merge-banner-out'); stageTrackerAfter(el, 'animationend', 180, () => el.remove());