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