chore(ui): add module file and main stub
- chore(ui): add module file and main stub - feat(ui): add layout, pages and partials - feat(ui): add stylesheet and scripts - feat(ui): add page renderer and static file handler - test(ui): cover rendering, escaping and static serving
This commit is contained in:
1 parent
7089a6f408
commit
5ac8f98f10
26 files changed
+3921
No files matched your search
@@ -0,0 +1,128 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
import { $, api, appData, busy, clear, clearErrors, handleError, h, openDialog, parseNumber, plural, setupDialogs, showFlash, toast, zoneLink } from "./common.js";
|
||||
|
||||
const { isAdmin } = appData();
|
||||
const state = { zones: [], counts: null, countsFailed: false, search: "" };
|
||||
|
||||
function problemCell(zone) {
|
||||
const cell = h("td");
|
||||
if (state.countsFailed) {
|
||||
cell.textContent = "unavailable";
|
||||
return cell;
|
||||
}
|
||||
if (state.counts === null) {
|
||||
cell.textContent = "loading...";
|
||||
return cell;
|
||||
}
|
||||
const counts = state.counts.get(zone.name) || { error: 0, warning: 0 };
|
||||
if (counts.error === 0 && counts.warning === 0) {
|
||||
cell.append(h("span", { class: "badge badge-ok", text: "none" }));
|
||||
return cell;
|
||||
}
|
||||
if (counts.error > 0) cell.append(h("span", { class: "badge badge-error", text: plural(counts.error, "error", "errors") }), " ");
|
||||
if (counts.warning > 0) cell.append(h("span", { class: "badge badge-warning", text: plural(counts.warning, "warning", "warnings") }));
|
||||
return cell;
|
||||
}
|
||||
|
||||
function render() {
|
||||
const body = $("#zones-body");
|
||||
const needle = state.search.trim().toLowerCase();
|
||||
const zones = state.zones.filter((zone) => zone.name.toLowerCase().includes(needle));
|
||||
clear(body);
|
||||
for (const zone of zones) {
|
||||
const serial = zone.serial === null || zone.serial === undefined ? "dynamic" : String(zone.serial);
|
||||
body.append(h("tr", null,
|
||||
h("th", { scope: "row" }, zoneLink(zone.name)),
|
||||
h("td", { class: "num", text: zone.records }),
|
||||
h("td", { class: "mono", text: serial }),
|
||||
problemCell(zone)));
|
||||
}
|
||||
const status = $("#zones-status");
|
||||
if (state.zones.length === 0) {
|
||||
status.textContent = isAdmin ? "There are no zones yet." : "No zone is delegated to you.";
|
||||
} else if (zones.length === 0) {
|
||||
status.textContent = "No zone matches the search.";
|
||||
} else {
|
||||
status.textContent = plural(zones.length, "zone", "zones") + ".";
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCounts() {
|
||||
try {
|
||||
const { data } = await api("GET", "/check");
|
||||
const counts = new Map();
|
||||
for (const problem of data.problems || []) {
|
||||
const entry = counts.get(problem.zone) || { error: 0, warning: 0 };
|
||||
if (problem.severity === "error") entry.error += 1;
|
||||
else entry.warning += 1;
|
||||
counts.set(problem.zone, entry);
|
||||
}
|
||||
state.counts = counts;
|
||||
} catch (err) {
|
||||
state.countsFailed = true;
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const { data } = await api("GET", "/zones");
|
||||
state.zones = data.zones || [];
|
||||
} catch (err) {
|
||||
$("#zones-status").textContent = "The zones could not be loaded.";
|
||||
handleError(err);
|
||||
return;
|
||||
}
|
||||
state.counts = null;
|
||||
state.countsFailed = false;
|
||||
render();
|
||||
loadCounts();
|
||||
}
|
||||
|
||||
function zoneBody(form) {
|
||||
const el = form.elements;
|
||||
return {
|
||||
name: el.name.value.trim(),
|
||||
ns: el.ns.value.trim(),
|
||||
mbox: el.mbox.value.trim(),
|
||||
extra_ns: el.extra_ns.value.split("\n").map((line) => line.trim()).filter(Boolean),
|
||||
refresh: parseNumber(el.refresh.value),
|
||||
retry: parseNumber(el.retry.value),
|
||||
expire: parseNumber(el.expire.value),
|
||||
minttl: parseNumber(el.minttl.value),
|
||||
ttl: parseNumber(el.ttl.value),
|
||||
};
|
||||
}
|
||||
|
||||
function setupCreate() {
|
||||
const dialog = $("#zone-dialog");
|
||||
const form = $("#zone-form");
|
||||
$("#new-zone").addEventListener("click", () => openDialog(dialog));
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
clearErrors(form);
|
||||
await busy(form.querySelector("button[type=submit]"), async () => {
|
||||
try {
|
||||
await api("POST", "/zones", { body: zoneBody(form) });
|
||||
} catch (err) {
|
||||
handleError(err, { form });
|
||||
return;
|
||||
}
|
||||
dialog.close();
|
||||
form.reset();
|
||||
toast("Zone created. The DNS server serves a new zone after its next zone list reload, which happens every 10 minutes by default.");
|
||||
await load();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
setupDialogs();
|
||||
showFlash();
|
||||
$("#zone-search").addEventListener("input", (event) => {
|
||||
state.search = event.target.value;
|
||||
render();
|
||||
});
|
||||
if (isAdmin) setupCreate();
|
||||
load();
|
||||
Reference in new issue
Block a user