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:
bryanpedini committed 2026-11-19 15:46:12 +01:00
1 parent 7089a6f408
commit 5ac8f98f10
26 files changed
+3921

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2026 Bryan Joshua Pedini
import { $, api, busy, formatTime, h, handleError, icon } from "./common.js";
const state = { nextBeforeId: null, count: 0, filters: {}, counter: 0 };
function flatten(value, prefix, out) {
if (Array.isArray(value)) {
value.forEach((item, index) => flatten(item, prefix + "[" + index + "]", out));
} else if (value !== null && typeof value === "object") {
for (const [key, item] of Object.entries(value)) flatten(item, prefix ? prefix + "." + key : key, out);
} else {
out.set(prefix, value);
}
return out;
}
function show(value) {
return value === undefined ? "(none)" : JSON.stringify(value);
}
export function diffRows(before, after) {
const left = flatten(before === undefined ? null : before, "", new Map());
const right = flatten(after === undefined ? null : after, "", new Map());
const paths = [...new Set([...left.keys(), ...right.keys()])].filter((path) => path !== "");
return paths.map((path) => {
const old = left.get(path);
const now = right.get(path);
let kind = "same";
if (!left.has(path)) kind = "added";
else if (!right.has(path)) kind = "removed";
else if (JSON.stringify(old) !== JSON.stringify(now)) kind = "changed";
return { path, kind, before: left.has(path) ? old : undefined, after: right.has(path) ? now : undefined };
});
}
function diffTable(entry) {
const rows = diffRows(entry.before, entry.after);
if (rows.length === 0) return h("p", { class: "muted", text: "No field-level data." });
return h("table", { class: "table diff" },
h("thead", null, h("tr", null, h("th", { scope: "col", text: "Field" }), h("th", { scope: "col", text: "Before" }), h("th", { scope: "col", text: "After" }))),
h("tbody", null, rows.map((row) => h("tr", { class: "diff-" + row.kind },
h("th", { scope: "row", class: "mono", text: row.path }),
h("td", { class: "mono", text: show(row.before) }),
h("td", { class: "mono", text: show(row.after) })))));
}
function entryRows(entry) {
state.counter += 1;
const detailId = "audit-detail-" + state.counter;
const hasDiff = entry.before !== null && entry.before !== undefined || entry.after !== null && entry.after !== undefined;
const target = [];
if (entry.zone) target.push(entry.zone);
if (entry.record_id !== null && entry.record_id !== undefined) target.push("record " + entry.record_id);
if (entry.target_user) target.push("user " + entry.target_user);
const failed = typeof entry.status === "number" && entry.status >= 400;
const detail = h("tr", { class: "detail-row", id: detailId, hidden: true }, h("td", { colspan: 7 }));
const toggle = h("button", {
type: "button",
class: "btn btn-small",
"aria-expanded": "false",
"aria-controls": detailId,
onclick: () => {
const open = toggle.getAttribute("aria-expanded") !== "true";
toggle.setAttribute("aria-expanded", String(open));
detail.hidden = !open;
if (open && detail.firstChild.childNodes.length === 0) detail.firstChild.append(diffTable(entry));
},
}, icon("chevron"), " Changes");
const row = h("tr", { class: failed ? "row-error" : null },
h("td", { class: "mono", text: formatTime(entry.time) }),
h("td", { text: entry.actor }),
h("td", { class: "mono", text: entry.origin }),
h("td", { class: "mono", text: entry.operation }),
h("td", null,
entry.status === null || entry.status === undefined ? "-" : h("span", { class: "badge " + (failed ? "badge-error" : "badge-ok"), text: String(entry.status) }),
entry.error ? h("div", { class: "muted", text: entry.error }) : null),
h("td", { text: target.length ? target.join(", ") : "-" }),
h("td", null, hasDiff ? toggle : "-"));
return [row, detail];
}
async function load(append) {
const body = $("#audit-body");
const status = $("#audit-status");
const query = { ...state.filters };
if (append && state.nextBeforeId !== null) query.before_id = state.nextBeforeId;
if (!append) {
body.replaceChildren();
state.count = 0;
status.textContent = "Loading...";
}
try {
const { data } = await api("GET", "/audit", { query });
const entries = data.entries || [];
for (const entry of entries) body.append(...entryRows(entry));
state.count += entries.length;
state.nextBeforeId = data.next_before_id === undefined ? null : data.next_before_id;
status.textContent = state.count === 0 ? "No audit rows match." : state.count + " rows shown.";
$("#audit-more").hidden = state.nextBeforeId === null;
} catch (err) {
status.textContent = "The audit could not be loaded.";
handleError(err);
}
}
function readFilters() {
const form = $("#audit-filters");
const filters = {};
for (const name of ["zone", "actor", "operation", "kind", "outcome"]) {
const value = form.elements[name].value.trim();
if (value) filters[name] = value;
}
return filters;
}
$("#audit-filters").addEventListener("submit", (event) => {
event.preventDefault();
state.filters = readFilters();
state.nextBeforeId = null;
load(false);
});
$("#audit-filters").addEventListener("reset", () => {
setTimeout(() => {
state.filters = {};
state.nextBeforeId = null;
load(false);
}, 0);
});
$("#audit-more").addEventListener("click", (event) => busy(event.target, () => load(true)));
load(false);