// Small DOM and formatting helpers shared by the tabs. export const $ = (selector, root = document) => root.querySelector(selector); // h("button", {class: "x", onclick: fn}, "text", child) -> element export function h(tag, props, ...children) { const el = document.createElement(tag); for (const [key, value] of Object.entries(props || {})) { if (value === null || value === undefined || value === false) continue; if (key.startsWith("on")) el.addEventListener(key.slice(2), value); else if (key === "class") el.className = value; else if (key === "dataset") Object.assign(el.dataset, value); else if (key === "style") el.style.cssText = value; else if (key === "value") el.value = value; // a textarea has no value attribute else if (key in el && typeof value !== "string") el[key] = value; else el.setAttribute(key, value === true ? "" : 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 : String(child)); } } export function clear(el, ...children) { el.replaceChildren(); append(el, children); return el; } let toastTimer = null; export function toast(text, kind = "") { const el = $("#toast"); el.textContent = text; el.className = `show ${kind}`; clearTimeout(toastTimer); toastTimer = setTimeout(() => { el.className = ""; }, kind === "err" ? 6000 : 3000); } export function debounce(fn, ms) { let timer = null; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), ms); }; } export const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; export const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; export function fmtTime(iso) { if (!iso) return ""; const d = new Date(iso); const today = new Date(); const time = d.toLocaleTimeString([], {hour: "2-digit", minute: "2-digit"}); if (d.toDateString() === today.toDateString()) return `Today ${time}`; const tomorrow = new Date(today); tomorrow.setDate(today.getDate() + 1); if (d.toDateString() === tomorrow.toDateString()) return `Tomorrow ${time}`; const opts = {weekday: "short", day: "numeric", month: "short"}; if (d.getFullYear() !== today.getFullYear()) opts.year = "numeric"; return `${d.toLocaleDateString([], opts)} ${time}`; } export function relative(iso) { if (!iso) return ""; const minutes = Math.round((new Date(iso) - Date.now()) / 60000); const abs = Math.abs(minutes); let text; if (abs < 1) return "now"; if (abs < 60) text = `${abs} min`; else if (abs < 60 * 36) text = `${Math.round(abs / 60)} h`; else text = `${Math.round(abs / 1440)} days`; return minutes > 0 ? `in ${text}` : `${text} ago`; } // A