| 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 ({ onSuppressWorldClick } = {}) |
| 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 | function suppressWorldClick (event) |
| 19 | { |
| 20 | event?.preventDefault?.(); |
| 21 | event?.stopPropagation?.(); |
| 22 | onSuppressWorldClick?.(); |
| 23 | } |
| 24 | |
| 25 | if (!root) |
| 26 | { |
| 27 | return { |
| 28 | show () |
| 29 | { return { dismiss () {} }; }, |
| 30 | clearPersistent () |
| 31 | {}, |
| 32 | applyRestartStatus () |
| 33 | {} |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | function show (message, options = {}) |
| 38 | { |
| 39 | const text = String(message ?? "").trim(); |
| 40 | if (!text) |
| 41 | { return { dismiss () {} }; } |
| 42 | const sticky = !!options.sticky; |
| 43 | const durationMs = sticky |
| 44 | ? null |
| 45 | : Math.max(800, options.durationMs ?? DEFAULT_DURATION_MS); |
| 46 | const onClick = typeof options.onClick === "function" ? options.onClick : null; |
| 47 | const actions = Array.isArray(options.actions) ? options.actions : []; |
| 48 | const el = document.createElement("div"); |
| 49 | el.className = "toast"; |
| 50 | el.setAttribute("role", "status"); |
| 51 | const textEl = document.createElement("div"); |
| 52 | textEl.className = "toast-text"; |
| 53 | textEl.textContent = text; |
| 54 | el.append(textEl); |
| 55 | let dismissed = false; |
| 56 | let timer = null; |
| 57 | const dismiss = () => |
| 58 | { |
| 59 | if (dismissed) |
| 60 | { return; } |
| 61 | dismissed = true; |
| 62 | if (timer != null) |
| 63 | { |
| 64 | window.clearTimeout(timer); |
| 65 | timer = null; |
| 66 | } |
| 67 | el.classList.remove("toast-visible"); |
| 68 | el.classList.add("toast-leaving"); |
| 69 | window.setTimeout(() => el.remove(), LEAVE_MS); |
| 70 | }; |
| 71 | if (actions.length) |
| 72 | { |
| 73 | el.classList.add("toast-actions"); |
| 74 | // Consume LMB before the game loop treats the press as a world order. |
| 75 | el.addEventListener("mousedown", suppressWorldClick); |
| 76 | const row = document.createElement("div"); |
| 77 | row.className = "toast-action-row"; |
| 78 | for (const action of actions) |
| 79 | { |
| 80 | const btn = document.createElement("button"); |
| 81 | btn.type = "button"; |
| 82 | btn.className = "toast-action"; |
| 83 | btn.textContent = action.label ?? "OK"; |
| 84 | btn.addEventListener("mousedown", suppressWorldClick); |
| 85 | btn.addEventListener("click", (event) => |
| 86 | { |
| 87 | suppressWorldClick(event); |
| 88 | action.onClick?.(); |
| 89 | dismiss(); |
| 90 | }); |
| 91 | row.append(btn); |
| 92 | } |
| 93 | el.append(row); |
| 94 | } |
| 95 | else if (onClick) |
| 96 | { |
| 97 | el.classList.add("toast-clickable"); |
| 98 | el.style.cursor = "pointer"; |
| 99 | el.addEventListener("mousedown", suppressWorldClick); |
| 100 | el.addEventListener("click", (event) => |
| 101 | { |
| 102 | suppressWorldClick(event); |
| 103 | onClick(); |
| 104 | dismiss(); |
| 105 | }); |
| 106 | } |
| 107 | root.appendChild(el); |
| 108 | requestAnimationFrame(() => |
| 109 | { |
| 110 | el.classList.add("toast-visible"); |
| 111 | }); |
| 112 | if (durationMs != null) |
| 113 | { timer = window.setTimeout(dismiss, durationMs); } |
| 114 | return { dismiss }; |
| 115 | } |
| 116 | |
| 117 | function clearPersistent (id) |
| 118 | { |
| 119 | const entry = persistent.get(id); |
| 120 | if (!entry) |
| 121 | { return; } |
| 122 | persistent.delete(id); |
| 123 | entry.el.classList.remove("toast-visible"); |
| 124 | entry.el.classList.add("toast-leaving"); |
| 125 | window.setTimeout(() => entry.el.remove(), LEAVE_MS); |
| 126 | } |
| 127 | |
| 128 | function showPersistent (id, text) |
| 129 | { |
| 130 | const existing = persistent.get(id); |
| 131 | if (existing) |
| 132 | { |
| 133 | existing.el.textContent = text; |
| 134 | return; |
| 135 | } |
| 136 | const el = document.createElement("div"); |
| 137 | el.className = "toast toast-persistent"; |
| 138 | el.setAttribute("role", "status"); |
| 139 | el.textContent = text; |
| 140 | root.appendChild(el); |
| 141 | requestAnimationFrame(() => |
| 142 | { |
| 143 | el.classList.add("toast-visible"); |
| 144 | }); |
| 145 | persistent.set(id, { el, timer: null }); |
| 146 | } |
| 147 | |
| 148 | function applyRestartStatus (payload) |
| 149 | { |
| 150 | const scheduledAt = payload?.scheduledAt ?? null; |
| 151 | if (!scheduledAt) |
| 152 | { |
| 153 | restartScheduledAt = null; |
| 154 | if (restartTick != null) |
| 155 | { |
| 156 | clearInterval(restartTick); |
| 157 | restartTick = null; |
| 158 | } |
| 159 | clearPersistent(PERSISTENT_RESTART_ID); |
| 160 | return; |
| 161 | } |
| 162 | restartScheduledAt = scheduledAt; |
| 163 | const tick = () => |
| 164 | { |
| 165 | showPersistent( |
| 166 | PERSISTENT_RESTART_ID, |
| 167 | restartCountdownMessage(Math.max(0, restartScheduledAt - Date.now())) |
| 168 | ); |
| 169 | }; |
| 170 | tick(); |
| 171 | if (restartTick == null) |
| 172 | { restartTick = window.setInterval(tick, 1000); } |
| 173 | } |
| 174 | |
| 175 | return { show, clearPersistent, applyRestartStatus }; |
| 176 | } |