Files
tiny-worlds/web/chrome.mjs
T

64 lines
2.1 KiB
JavaScript
Raw Normal View History

// web/chrome.mjs -- the shared page chrome: header identity, session
// info, the context meter, the busy note, the notice banner.
//
// In-page notices only -- no JS alert()/confirm(): they block the
// embedded browser pane. Errors and notices land in the banner
// instead; destructive confirms are two-click armed (widgets.mjs).
/** @typedef {import('../server/shapes.mjs').User} User */
import { $ } from './dom.mjs';
// identity chrome: who is signed in. Signing in is permanent and
// per-browser (the token lives in localStorage); there is no sign-out
// -- the lifecycle story is a roadmap extension point
/** @param {?User} [me] */
export function setWho(me) {
$('who').textContent = me ? `${me.name}${me.superuser ? ' \u00b7 op' : ''}` : '';
$('who').title = me?.id ?? '';
}
/** @param {string} text */
export function setSessionInfo(text) {
$('session-info').textContent = text;
}
/** The run detail (GET /runs/:id) carries the meter's numbers; null
* is the router's "no run addressed" reset.
* @param {?{ context?: ?{ used?: ?number, pct?: ?number, contwin?: ?number } }} [detail] */
export function renderMeter(detail) {
const fill = $('context-fill');
const label = $('context-label');
const c = detail?.context;
if (!c || c.used == null) {
fill.style.width = '0';
label.textContent = '';
return;
}
const pct = c.pct ?? 0;
fill.style.width = `${Math.min(100, pct)}%`;
fill.classList.toggle('hot', pct > 85);
label.textContent = c.contwin ? `${pct}% -- close & housekeep past 85%` : '';
}
/** @param {boolean} on @param {string} [text] */
export function setBusy(on, text = 'her turn...') {
$('busy').hidden = !on; // model calls take their time -- say so
if (on) $('busy').textContent = text;
}
/** @type {?ReturnType<typeof setTimeout>} */ let bannerTimer = null;
/** @param {string} text @param {string} [kind] */
export function banner(text, kind = 'error') {
const b = $('banner');
b.textContent = text;
b.className = kind;
b.hidden = false;
clearTimeout(bannerTimer ?? undefined);
bannerTimer = setTimeout(() => { b.hidden = true; }, 10000);
}
export function hideBanner() {
$('banner').hidden = true;
}