Files
opencode2api/webui/js/main.js
T
michaelliunsky a0da31ed08 feat(webui): rebuild console with tabbed pages
Replace the single-file frontend (app.js, styles.css) with a multi-file
ES module structure served from go:embed, and reorganize the 13 pages
into 8 tabbed pages under 4 navigation groups: runtime (overview,
diagnostics), integrations (access guide, API keys), models (quotas,
rotation), and system (configuration, account).

The redesign adopts a monochrome light/dark design system driven by
prefers-color-scheme, replaces prompt()/confirm() flows with in-page
dialogs, and keeps every management API call and deep link (old hashes
alias to the new pages).

Behavior changes: the log SSE connection now spans the whole console
session and buffers while the stream tab is hidden; quota polling runs
only while its tab is active; the first-run checklist appears on the
overview page and hides once ready.

Also fixes a crash on the rotation page: the persisted state serializes
empty orders and maps as JSON null, which is now normalized before
rendering, with the same defensive handling in the quota and API key
pages.

CI now syntax-checks the ES modules via node --input-type=module, and
the README structure notes are updated.
2026-10-16 18:09:42 +08:00

123 lines
3.5 KiB
JavaScript

// Console shell: boot, login, navigation and shared data loading.
import { api, setCsrf, clearCsrf, setUnauthorizedHandler } from "./api.js";
import { refreshMonitor, loadConfig, loadDebugModels } from "./data.js";
import * as store from "./store.js";
import { $, $$, toast } from "./ui.js";
import * as logstream from "./logstream.js";
import overview from "./pages/overview.js";
import diagnostics from "./pages/diagnostics.js";
import access from "./pages/access.js";
import apiKeys from "./pages/api-keys.js";
import quotas from "./pages/model-quotas.js";
import rotation from "./pages/rotation.js";
import configPage from "./pages/config.js";
import account from "./pages/account.js";
const PAGES = [overview, diagnostics, access, apiKeys, quotas, rotation, configPage, account];
const byId = new Map(PAGES.map((page) => [page.id, page]));
// Hrefs from the previous 13-page layout keep working.
const HASH_ALIASES = {
guide: "overview",
usage: "overview",
playground: "diagnostics",
logs: "diagnostics",
availability: "model-quotas",
};
let current = null;
let monitorTimer = null;
function openPage(id) {
const page = byId.get(id);
if (!page) return;
current?.hide?.();
history.replaceState(null, "", `#${id}`);
$$(".nav-item").forEach((button) =>
button.classList.toggle("active", button.dataset.page === id),
);
$$(".page").forEach((node) => node.classList.toggle("hidden", node.id !== `page-${id}`));
$("#page-title").textContent = page.title;
$("#page-sub").textContent = page.sub;
current = page;
page.show?.();
}
function showLogin() {
clearCsrf();
current?.hide?.();
current = null;
$("#app").classList.add("hidden");
$("#login").classList.remove("hidden");
clearInterval(monitorTimer);
monitorTimer = null;
logstream.disconnect();
for (const page of PAGES) page.reset?.();
}
async function enterConsole(session) {
setCsrf(session.csrf_token);
$("#login").classList.add("hidden");
$("#app").classList.remove("hidden");
await loadConfig();
await Promise.all([refreshMonitor(), loadDebugModels()]);
clearInterval(monitorTimer);
monitorTimer = setInterval(refreshMonitor, 3000);
logstream.connect();
const hash = location.hash.slice(1);
const id = byId.has(hash) ? hash : HASH_ALIASES[hash];
openPage(id && byId.has(id) ? id : "overview");
}
async function boot() {
try {
await enterConsole(await api("/api/auth/session"));
} catch {
showLogin();
}
}
setUnauthorizedHandler(showLogin);
store.on("monitor", (data) => {
$("#version").textContent = `v${data.version}`;
});
for (const page of PAGES) page.init?.();
$$(".nav-item").forEach((button) => {
button.addEventListener("click", () => openPage(button.dataset.page));
});
$("#refresh-now").addEventListener("click", async () => {
await Promise.all([refreshMonitor(), loadConfig(), loadDebugModels()]);
await current?.refresh?.();
toast("状态已刷新");
});
$("#logout").addEventListener("click", async () => {
try {
await api("/api/auth/logout", { method: "POST", body: "{}" });
} finally {
showLogin();
}
});
$("#login-form").addEventListener("submit", async (event) => {
event.preventDefault();
$("#login-error").textContent = "";
try {
const session = await api("/api/auth/login", {
method: "POST",
body: JSON.stringify({ username: $("#login-user").value, password: $("#login-pass").value }),
});
$("#login-pass").value = "";
await enterConsole(session);
} catch (error) {
$("#login-error").textContent = error.message;
}
});
boot();