import { restartCountdownMessage } from "/shared/deploy/schedule.js"; const DEFAULT_DURATION_MS = 3500; const LEAVE_MS = 280; const PERSISTENT_RESTART_ID = "server-restart"; /** * Temporary top-of-screen toast messages (play HUD). */ export function initToasts ({ onSuppressWorldClick } = {}) { const root = document.getElementById("toast-stack"); /** @type {Map} */ const persistent = new Map(); let restartTick = null; let restartScheduledAt = null; function suppressWorldClick (event) { event?.preventDefault?.(); event?.stopPropagation?.(); onSuppressWorldClick?.(); } if (!root) { return { show () { return { dismiss () {} }; }, clearPersistent () {}, applyRestartStatus () {} }; } function show (message, options = {}) { const text = String(message ?? "").trim(); if (!text) { return { dismiss () {} }; } const sticky = !!options.sticky; const durationMs = sticky ? null : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS); const onClick = typeof options.onClick === "function" ? options.onClick : null; const actions = Array.isArray(options.actions) ? options.actions : []; const el = document.createElement("div"); el.className = "toast"; el.setAttribute("role", "status"); const textEl = document.createElement("div"); textEl.className = "toast-text"; textEl.textContent = text; el.append(textEl); let dismissed = false; let timer = null; const dismiss = () => { if (dismissed) { return; } dismissed = true; if (timer != null) { window.clearTimeout(timer); timer = null; } el.classList.remove("toast-visible"); el.classList.add("toast-leaving"); window.setTimeout(() => el.remove(), LEAVE_MS); }; if (actions.length) { el.classList.add("toast-actions"); // Consume LMB before the game loop treats the press as a world order. el.addEventListener("mousedown", suppressWorldClick); const row = document.createElement("div"); row.className = "toast-action-row"; for (const action of actions) { const btn = document.createElement("button"); btn.type = "button"; btn.className = "toast-action"; btn.textContent = action.label ?? "OK"; btn.addEventListener("mousedown", suppressWorldClick); btn.addEventListener("click", (event) => { suppressWorldClick(event); action.onClick?.(); dismiss(); }); row.append(btn); } el.append(row); } else if (onClick) { el.classList.add("toast-clickable"); el.style.cursor = "pointer"; el.addEventListener("mousedown", suppressWorldClick); el.addEventListener("click", (event) => { suppressWorldClick(event); onClick(); dismiss(); }); } root.appendChild(el); requestAnimationFrame(() => { el.classList.add("toast-visible"); }); if (durationMs != null) { timer = window.setTimeout(dismiss, durationMs); } return { dismiss }; } function clearPersistent (id) { const entry = persistent.get(id); if (!entry) { return; } persistent.delete(id); entry.el.classList.remove("toast-visible"); entry.el.classList.add("toast-leaving"); window.setTimeout(() => entry.el.remove(), LEAVE_MS); } function showPersistent (id, text) { const existing = persistent.get(id); if (existing) { existing.el.textContent = text; return; } const el = document.createElement("div"); el.className = "toast toast-persistent"; el.setAttribute("role", "status"); el.textContent = text; root.appendChild(el); requestAnimationFrame(() => { el.classList.add("toast-visible"); }); persistent.set(id, { el, timer: null }); } function applyRestartStatus (payload) { const scheduledAt = payload?.scheduledAt ?? null; if (!scheduledAt) { restartScheduledAt = null; if (restartTick != null) { clearInterval(restartTick); restartTick = null; } clearPersistent(PERSISTENT_RESTART_ID); return; } restartScheduledAt = scheduledAt; const tick = () => { showPersistent( PERSISTENT_RESTART_ID, restartCountdownMessage(Math.max(0, restartScheduledAt - Date.now())) ); }; tick(); if (restartTick == null) { restartTick = window.setInterval(tick, 1000); } } return { show, clearPersistent, applyRestartStatus }; }