Vanilla ES modules under a strict CSP (bars and charts are SVG, no inline styles), cookie session with CSRF header, first sign-in password change, live updates over the events WebSocket, language menu fed by /api/v1/i18n. Every label and tooltip is an Admin.* resource (en, it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
202 lines
7.7 KiB
JavaScript
202 lines
7.7 KiB
JavaScript
// Titano Server admin panel — vanilla ES modules, no build step (BRIEF §14.8).
|
|
import { api, ApiFailure } from "./api.js";
|
|
import { t, loadStrings, applyStrings, setLanguage, languages, culture } from "./i18n.js";
|
|
import { icons } from "./icons.js";
|
|
import { escapeHtml, attr, errorText, nativeName } from "./ui.js";
|
|
|
|
const app = document.getElementById("app");
|
|
const pages = ["dashboard", "users", "archive", "jobs", "settings", "logs", "about"];
|
|
|
|
let info = null;
|
|
let events = null;
|
|
const listeners = new Set();
|
|
|
|
async function start() {
|
|
await loadStrings();
|
|
info = await api.get("/api/v1/server/info").catch(() => null);
|
|
await api.resume();
|
|
window.addEventListener("hashchange", render);
|
|
window.addEventListener("titano:signed-out", () => {
|
|
events?.stop();
|
|
events = null;
|
|
render();
|
|
});
|
|
render();
|
|
}
|
|
|
|
function currentPage() {
|
|
const p = location.hash.replace(/^#\/?/, "").split("?")[0];
|
|
return pages.includes(p) ? p : "dashboard";
|
|
}
|
|
|
|
function render() {
|
|
const user = api.user();
|
|
if (!user) return renderLogin();
|
|
if (user.mustChangePassword) return renderPasswordChange();
|
|
renderShell(currentPage());
|
|
}
|
|
|
|
function brand() {
|
|
return `<div class="brand"><img src="favicon.svg" alt=""><div><strong>Titano Server</strong><span class="num">${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}</span></div></div>`;
|
|
}
|
|
|
|
function renderLogin(message = "") {
|
|
app.innerHTML = `
|
|
<main class="login">
|
|
<form novalidate>
|
|
${brand()}
|
|
<label><span data-i18n="Admin.Login.User"></span><input class="input" name="username" autocomplete="username" required data-i18n-title="Admin.Login.User.Tip"></label>
|
|
<label><span data-i18n="Admin.Login.Password"></span><input class="input" name="password" type="password" autocomplete="current-password" required data-i18n-title="Admin.Login.Password.Tip"></label>
|
|
<div class="error" role="alert">${escapeHtml(message)}</div>
|
|
<button class="btn primary" type="submit" data-i18n="Admin.Login.SignIn" data-i18n-title="Admin.Login.SignIn.Tip"></button>
|
|
</form>
|
|
</main>`;
|
|
applyStrings(app);
|
|
const form = app.querySelector("form");
|
|
const error = form.querySelector(".error");
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
error.textContent = "";
|
|
const data = new FormData(form);
|
|
try {
|
|
const r = await api.signIn(String(data.get("username")), String(data.get("password")));
|
|
if (r.user.role !== "admin") {
|
|
await api.signOut();
|
|
error.textContent = t("Admin.Login.NotAdmin");
|
|
return;
|
|
}
|
|
render();
|
|
} catch (err) {
|
|
error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : errorText(err);
|
|
}
|
|
});
|
|
form.querySelector("input").focus();
|
|
}
|
|
|
|
function renderPasswordChange() {
|
|
app.innerHTML = `
|
|
<main class="login">
|
|
<form novalidate>
|
|
${brand()}
|
|
<h1 data-i18n="Admin.Password.Title"></h1>
|
|
<label><span data-i18n="Admin.Password.Current"></span><input class="input" name="current" type="password" autocomplete="current-password" required data-i18n-title="Admin.Password.Current.Tip"></label>
|
|
<label><span data-i18n="Admin.Password.New"></span><input class="input" name="next" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.New.Tip"></label>
|
|
<label><span data-i18n="Admin.Password.Repeat"></span><input class="input" name="repeat" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.Repeat.Tip"></label>
|
|
<div class="error" role="alert"></div>
|
|
<div class="row">
|
|
<button class="btn" type="button" id="cancel" data-i18n="Admin.SignOut" data-i18n-title="Admin.SignOut.Tip"></button>
|
|
<span class="spacer"></span>
|
|
<button class="btn primary" type="submit" data-i18n="Admin.Password.Save" data-i18n-title="Admin.Password.Save.Tip"></button>
|
|
</div>
|
|
</form>
|
|
</main>`;
|
|
applyStrings(app);
|
|
const form = app.querySelector("form");
|
|
const error = form.querySelector(".error");
|
|
app.querySelector("#cancel").addEventListener("click", async () => {
|
|
await api.signOut();
|
|
render();
|
|
});
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
const data = new FormData(form);
|
|
if (data.get("next") !== data.get("repeat")) {
|
|
error.textContent = t("Admin.Password.Mismatch");
|
|
return;
|
|
}
|
|
try {
|
|
await api.changePassword(String(data.get("current")), String(data.get("next")));
|
|
render();
|
|
} catch (err) {
|
|
error.textContent = errorText(err);
|
|
}
|
|
});
|
|
form.querySelector("input").focus();
|
|
}
|
|
|
|
function renderShell(page) {
|
|
const nav = pages.map((p) => `
|
|
<a href="#/${p}" ${p === page ? 'aria-current="page"' : ""} data-i18n-title="Admin.Nav.${cap(p)}.Tip">${icons[p]}<span data-i18n="Admin.Nav.${cap(p)}"></span><span class="badge" id="badge-${p}" hidden></span></a>`).join("");
|
|
const user = api.user();
|
|
const options = languages().map((code) => `<option value="${attr(code)}" ${code === culture() ? "selected" : ""}>${escapeHtml(nativeName(code))}</option>`).join("");
|
|
app.innerHTML = `
|
|
<div class="shell">
|
|
<aside class="sidebar">
|
|
${brand()}
|
|
<nav class="nav">${nav}</nav>
|
|
<div class="user">
|
|
<div class="avatar">${escapeHtml(user.username.slice(0, 1).toUpperCase())}</div>
|
|
<div class="who"><strong>${escapeHtml(user.username)}</strong><span data-i18n="Admin.Role.Admin"></span></div>
|
|
<button class="btn icon" id="signout" data-i18n-title="Admin.SignOut.Tip">${icons.power}</button>
|
|
</div>
|
|
</aside>
|
|
<div class="main">
|
|
<header class="header">
|
|
<h1 data-i18n="Admin.Nav.${cap(page)}"></h1>
|
|
<span class="meta" id="meta"></span>
|
|
<span class="spacer"></span>
|
|
<span class="actions" id="actions"></span>
|
|
<select class="input language" id="language" data-i18n-title="Admin.Language.Tip">${options}</select>
|
|
</header>
|
|
<section class="content" id="content"></section>
|
|
</div>
|
|
</div>`;
|
|
applyStrings(app);
|
|
app.querySelector("#signout").addEventListener("click", async () => {
|
|
events?.stop();
|
|
events = null;
|
|
await api.signOut();
|
|
render();
|
|
});
|
|
app.querySelector("#language").addEventListener("change", async (e) => {
|
|
await setLanguage(e.target.value);
|
|
render();
|
|
});
|
|
events ??= api.events((e) => listeners.forEach((fn) => fn(e)));
|
|
listeners.clear();
|
|
updateBadges();
|
|
listeners.add((e) => {
|
|
if (e.type === "jobState") updateBadges();
|
|
});
|
|
|
|
const content = app.querySelector("#content");
|
|
const context = {
|
|
api,
|
|
info,
|
|
meta: app.querySelector("#meta"),
|
|
actions: app.querySelector("#actions"),
|
|
onEvent: (fn) => listeners.add(fn),
|
|
};
|
|
import(`./pages/${page}.js`)
|
|
.then((m) => m.render(content, context))
|
|
.catch((err) => {
|
|
content.innerHTML = `<div class="panel empty"><span>${escapeHtml(errorText(err))}</span></div>`;
|
|
});
|
|
}
|
|
|
|
let badgeTimer = null;
|
|
|
|
/** Running jobs and accounts, as nav badges (refreshed at most every two seconds). */
|
|
function updateBadges() {
|
|
if (badgeTimer) return;
|
|
badgeTimer = setTimeout(() => {
|
|
badgeTimer = null;
|
|
}, 2000);
|
|
api.get("/api/v1/jobs?state=running&limit=100").then((page) => setBadge("jobs", page.items.length, true)).catch(() => {});
|
|
api.get("/api/v1/users").then((users) => setBadge("users", users.length, false)).catch(() => {});
|
|
}
|
|
|
|
function setBadge(page, count, accent) {
|
|
const badge = document.getElementById(`badge-${page}`);
|
|
if (!badge) return;
|
|
badge.hidden = count === 0;
|
|
badge.textContent = String(count);
|
|
badge.classList.toggle("quiet", !accent);
|
|
}
|
|
|
|
function cap(s) {
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
}
|
|
|
|
start();
|