BlueGit

timfa questward

Files

questward / client / src / play / toasts.js

toasts.js

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