| 1 | import { restartCountdownMessage } from "/shared/deploy/schedule.js"; |
| 2 | |
| 3 | const DEFAULT_DURATION_MS = 3500; |
| 4 | const LEAVE_MS = 280; |
| 5 | const PERSISTENT_RESTART_ID = "server-restart"; |
| 6 | |
| 7 | /** |
| 8 | * Temporary top-of-screen toast messages (play HUD). |
| 9 | */ |
| 10 | export function initToasts () |
| 11 | { |
| 12 | const root = document.getElementById("toast-stack"); |
| 13 | /** @type {Map<string, { el: HTMLElement, timer: number | null }>} */ |
| 14 | const persistent = new Map(); |
| 15 | let restartTick = null; |
| 16 | let restartScheduledAt = null; |
| 17 | |
| 18 | if (!root) |
| 19 | { |
| 20 | return { |
| 21 | show () |
| 22 | {}, |
| 23 | clearPersistent () |
| 24 | {}, |
| 25 | applyRestartStatus () |
| 26 | {} |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | function show (message, options = {}) |
| 31 | { |
| 32 | const text = String(message ?? "").trim(); |
| 33 | if (!text) |
| 34 | { return; } |
| 35 | const durationMs = Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS); |
| 36 | const onClick = typeof options.onClick === "function" ? options.onClick : null; |
| 37 | const el = document.createElement("div"); |
| 38 | el.className = "toast"; |
| 39 | el.setAttribute("role", "status"); |
| 40 | el.textContent = text; |
| 41 | let dismissed = false; |
| 42 | const dismiss = () => |
| 43 | { |
| 44 | if (dismissed) |
| 45 | { return; } |
| 46 | dismissed = true; |
| 47 | el.classList.remove("toast-visible"); |
| 48 | el.classList.add("toast-leaving"); |
| 49 | window.setTimeout(() => el.remove(), LEAVE_MS); |
| 50 | }; |
| 51 | if (onClick) |
| 52 | { |
| 53 | el.classList.add("toast-clickable"); |
| 54 | el.style.cursor = "pointer"; |
| 55 | el.addEventListener("click", () => |
| 56 | { |
| 57 | onClick(); |
| 58 | dismiss(); |
| 59 | }); |
| 60 | } |
| 61 | root.appendChild(el); |
| 62 | requestAnimationFrame(() => |
| 63 | { |
| 64 | el.classList.add("toast-visible"); |
| 65 | }); |
| 66 | window.setTimeout(dismiss, durationMs); |
| 67 | } |
| 68 | |
| 69 | function clearPersistent (id) |
| 70 | { |
| 71 | const entry = persistent.get(id); |
| 72 | if (!entry) |
| 73 | { return; } |
| 74 | persistent.delete(id); |
| 75 | entry.el.classList.remove("toast-visible"); |
| 76 | entry.el.classList.add("toast-leaving"); |
| 77 | window.setTimeout(() => entry.el.remove(), LEAVE_MS); |
| 78 | } |
| 79 | |
| 80 | function showPersistent (id, text) |
| 81 | { |
| 82 | const message = String(text ?? "").trim(); |
| 83 | if (!message) |
| 84 | { |
| 85 | clearPersistent(id); |
| 86 | return; |
| 87 | } |
| 88 | let entry = persistent.get(id); |
| 89 | if (!entry) |
| 90 | { |
| 91 | const el = document.createElement("div"); |
| 92 | el.className = "toast toast-persistent"; |
| 93 | el.setAttribute("role", "status"); |
| 94 | el.dataset.toastId = id; |
| 95 | root.appendChild(el); |
| 96 | entry = { el }; |
| 97 | persistent.set(id, entry); |
| 98 | requestAnimationFrame(() => |
| 99 | { |
| 100 | el.classList.add("toast-visible"); |
| 101 | }); |
| 102 | } |
| 103 | entry.el.textContent = message; |
| 104 | } |
| 105 | |
| 106 | function stopRestartCountdown () |
| 107 | { |
| 108 | if (restartTick != null) |
| 109 | { |
| 110 | window.clearInterval(restartTick); |
| 111 | restartTick = null; |
| 112 | } |
| 113 | restartScheduledAt = null; |
| 114 | clearPersistent(PERSISTENT_RESTART_ID); |
| 115 | } |
| 116 | |
| 117 | function refreshRestartToast () |
| 118 | { |
| 119 | if (restartScheduledAt == null) |
| 120 | { return; } |
| 121 | const remaining = restartScheduledAt - Date.now(); |
| 122 | if (remaining <= 0) |
| 123 | { |
| 124 | showPersistent(PERSISTENT_RESTART_ID, "Server restart for a game update is starting…"); |
| 125 | return; |
| 126 | } |
| 127 | showPersistent(PERSISTENT_RESTART_ID, restartCountdownMessage(remaining)); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param {{ scheduled?: boolean, warning?: boolean, scheduledAt?: number | null } | null | undefined} status |
| 132 | */ |
| 133 | function applyRestartStatus (status) |
| 134 | { |
| 135 | if (!status?.scheduled || !status.scheduledAt) |
| 136 | { |
| 137 | stopRestartCountdown(); |
| 138 | return; |
| 139 | } |
| 140 | if (!status.warning) |
| 141 | { |
| 142 | stopRestartCountdown(); |
| 143 | return; |
| 144 | } |
| 145 | restartScheduledAt = Number(status.scheduledAt); |
| 146 | refreshRestartToast(); |
| 147 | if (restartTick == null) |
| 148 | { restartTick = window.setInterval(refreshRestartToast, 1000); } |
| 149 | } |
| 150 | |
| 151 | return { |
| 152 | show, |
| 153 | showPersistent, |
| 154 | clearPersistent, |
| 155 | applyRestartStatus |
| 156 | }; |
| 157 | } |