// DOM helpers. Server data only ever reaches the page as text nodes or attribute // values, never as HTML, so track names can't inject markup. export function h(tag, attrs = {}, ...children) { const el = document.createElement(tag); for (const [key, value] of Object.entries(attrs ?? {})) { if (value === false || value === null || value === undefined) continue; if (key.startsWith("on") && typeof value === "function") el.addEventListener(key.slice(2), value); else if (key === "class") el.className = value; // CSSOM, not a style attribute, so it's allowed by the UI's CSP (style-src 'self'). else if (key === "style") el.style.cssText = value; else if (key === "dataset") Object.assign(el.dataset, value); else el.setAttribute(key, value === true ? "" : String(value)); } append(el, children); return el; } function append(el, children) { for (const child of children.flat(Infinity)) { if (child === null || child === undefined || child === false) continue; el.append(child instanceof Node ? child : document.createTextNode(String(child))); } } export function replace(el, ...children) { el.replaceChildren(); append(el, children); return el; } const numberFormat = new Intl.NumberFormat(); export const num = (n) => numberFormat.format(n); const relative = new Intl.RelativeTimeFormat(undefined, { numeric: "auto" }); const UNITS = [ ["year", 365 * 86400], ["month", 30 * 86400], ["week", 7 * 86400], ["day", 86400], ["hour", 3600], ["minute", 60], ]; export function timeAgo(value) { const seconds = (new Date(value).getTime() - Date.now()) / 1000; for (const [unit, size] of UNITS) { if (Math.abs(seconds) >= size) return relative.format(Math.round(seconds / size), unit); } return "just now"; } export const formatTime = (value) => new Date(value).toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" }); export const formatDay = (value) => new Date(value).toLocaleDateString(undefined, { weekday: "long", day: "numeric", month: "long", year: "numeric" }); export const formatDate = (value) => new Date(value).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" }); export function formatDuration(seconds) { const s = Math.max(0, Math.floor(seconds)); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; } export function errorBox(message) { return h("p", { class: "notice notice-error", role: "alert" }, message); }