`;
}).join("");
}
async function apiJson(url, opts = {}) {
const res = await fetch(url, { credentials: "include", ...opts });
const text = await res.text();
let data = null;
try {
data = text ? JSON.parse(text) : null;
} catch {
data = null;
}
if (!res.ok) throw new Error((data && data.error) || text || res.statusText);
return data;
}
function toInt(v, fallback) {
const n = Number(v);
if (!Number.isFinite(n)) return fallback;
return Math.round(n);
}
async function refresh() {
setStatus(t("settings.loading"));
const data = await apiJson("/api/settings");
const s = data && typeof data === "object" ? data : {};
if (inputMissionRuns) inputMissionRuns.value = String(toInt(s.retention_mission_runs, 2000));
if (inputSystemLog) inputSystemLog.value = String(toInt(s.retention_system_log, 2000));
if (inputErrorLogs) inputErrorLogs.value = String(toInt(s.retention_error_logs, 200));
if (selectLogLevel) selectLogLevel.value = String(s.log_level || "info");
setStatus(t("settings.loaded"), "ok");
}
async function apply() {
const payload = {
retention_mission_runs: toInt(inputMissionRuns?.value, 2000),
retention_system_log: toInt(inputSystemLog?.value, 2000),
retention_error_logs: toInt(inputErrorLogs?.value, 200),
log_level: String(selectLogLevel?.value || "info"),
};
setStatus(t("settings.saving"));
const res = await apiJson("/api/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const ok = res && typeof res === "object";
setStatus(ok ? t("settings.saved") : t("settings.saveFailed"), ok ? "ok" : "error");
await refresh();
}
function bind() {
renderGrid();
gridEl?.addEventListener("click", (evt) => {
const btn = evt.target.closest("[data-settings-tile]");
if (!btn?.dataset.settingsTile) return;
const tile = TILES.find((x) => x.id === btn.dataset.settingsTile);
if (!tile) return;
if (tile.action === "open_logging_retention") {
showDetail();
refresh().catch((e) => setStatus(e.message, "error"));
}
});
el("settingsRefreshBtn")?.addEventListener("click", () => refresh().catch((e) => setStatus(e.message, "error")));
el("settingsApplyBtn")?.addEventListener("click", () => apply().catch((e) => setStatus(e.message, "error")));
el("settingsBackBtn")?.addEventListener("click", () => showGrid());
}
function onPageShow() {
showGrid();
refresh().catch((e) => setStatus(e.message, "error"));
}
function onPageHide() {}
window.SettingsApp = { onPageShow, onPageHide, refresh };
function boot() {
bind();
}
if (window.AuthApp?.isReady()) boot();
else window.addEventListener("lm:auth-ready", boot, { once: true });
})();