Files
tiny-worlds/web/chrome.mjs
T

55 lines
1.7 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).
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
export function setWho(me) {
$('who').textContent = me ? `${me.name}${me.superuser ? ' \u00b7 op' : ''}` : '';
$('who').title = me?.id ?? '';
}
export function setSessionInfo(text) {
$('session-info').textContent = text;
}
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%` : '';
}
export function setBusy(on, text = 'her turn...') {
$('busy').hidden = !on; // model calls take their time -- say so
if (on) $('busy').textContent = text;
}
let bannerTimer = null;
export function banner(text, kind = 'error') {
const b = $('banner');
b.textContent = text;
b.className = kind;
b.hidden = false;
clearTimeout(bannerTimer);
bannerTimer = setTimeout(() => { b.hidden = true; }, 10000);
}
export function hideBanner() {
$('banner').hidden = true;
}