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 () { const root = document.getElementById("toast-stack"); /** @type {Map} */ const persistent = new Map(); let restartTick = null; let restartScheduledAt = null; if (!root) { return { show () {}, clearPersistent () {}, applyRestartStatus () {} }; } function show (message, options = {}) { const text = String(message ?? "").trim(); if (!text) { return; } const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS); const onClick = typeof options.onClick === "function" ? options.onClick : null; const el = document.createElement("div"); el.className = "toast"; el.setAttribute("role", "status"); el.textContent = text; let dismissed = false; const dismiss = () => { if (dismissed) { return; } dismissed = true; el.classList.remove("toast-visible"); el.classList.add("toast-leaving"); window.setTimeout(() => el.remove(), LEAVE_MS); }; if (onClick) { el.classList.add("toast-clickable"); el.style.cursor = "pointer"; el.addEventListener("click", () => { onClick(); dismiss(); }); } root.appendChild(el); requestAnimationFrame(() => { el.classList.add("toast-visible"); }); window.setTimeout(dismiss, durationMs); } 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 message = String(text ?? "").trim(); if (!message) { clearPersistent(id); return; } let entry = persistent.get(id); if (!entry) { const el = document.createElement("div"); el.className = "toast toast-persistent"; el.setAttribute("role", "status"); el.dataset.toastId = id; root.appendChild(el); entry = { el }; persistent.set(id, entry); requestAnimationFrame(() => { el.classList.add("toast-visible"); }); } entry.el.textContent = message; } function stopRestartCountdown () { if (restartTick != null) { window.clearInterval(restartTick); restartTick = null; } restartScheduledAt = null; clearPersistent(PERSISTENT_RESTART_ID); } function refreshRestartToast () { if (restartScheduledAt == null) { return; } const remaining = restartScheduledAt - Date.now(); if (remaining <= 0) { showPersistent(PERSISTENT_RESTART_ID, "Server restart for a game update is starting…"); return; } showPersistent(PERSISTENT_RESTART_ID, restartCountdownMessage(remaining)); } /** * @param {{ scheduled?: boolean, warning?: boolean, scheduledAt?: number | null } | null | undefined} status */ function applyRestartStatus (status) { if (!status?.scheduled || !status.scheduledAt) { stopRestartCountdown(); return; } if (!status.warning) { stopRestartCountdown(); return; } restartScheduledAt = Number(status.scheduledAt); refreshRestartToast(); if (restartTick == null) { restartTick = window.setInterval(refreshRestartToast, 1000); } } return { show, showPersistent, clearPersistent, applyRestartStatus }; }