Files
opencode2api/webui/js/theme.js
T
Jason Xu 58f56676ba feat(webui): theme toggle, paged ledger, working refresh status, and collapsed proxy chips
- Dark theme: an auto/light/dark cycle in the topbar, persisted per
  browser and applied before first paint by an inline head script; the
  dark palette moves behind data-theme so the runtime choice wins over
  the media query (#122).
- Request routing and upstream attempts tables are paged (50/100/200/500
  rows per page) instead of a hard last-100 cap, with translated pager
  controls (#122).
- Free-model refresh status now calls POST /api/models/probe, which
  marks every automatic model due and wakes the probe loop, then reloads
  the snapshot; the button re-probes instead of only rereading the cache
  (#122).
- The config page renders long key/proxy lists collapsed behind a
  100-chip cap with an expand toggle, so a large proxy pool no longer
  freezes the page (#146).
- New strings translated for en/vi/it; READMEs document the new
  admin endpoint.
2026-10-30 04:16:18 +08:00

54 lines
1.7 KiB
JavaScript

// Theme handling: an auto/light/dark cycle persisted per browser. The inline
// head script applies the stored choice before first paint; this module owns
// the topbar toggle and keeps "auto" tracking the system preference.
import { t, onChange } from "./i18n.js";
const STORAGE_KEY = "opencode2api.ui.theme";
const MODES = ["auto", "light", "dark"];
const media = window.matchMedia("(prefers-color-scheme: dark)");
const stored = () => {
try {
return localStorage.getItem(STORAGE_KEY) || "";
} catch {
return "";
}
};
let mode = MODES.includes(stored()) ? stored() : "auto";
const isDark = () => mode === "dark" || (mode === "auto" && media.matches);
function label() {
if (mode === "light") return t("浅色", "theme.light");
if (mode === "dark") return t("深色", "theme.dark");
return t("跟随系统", "theme.auto");
}
function apply() {
document.documentElement.setAttribute("data-theme", isDark() ? "dark" : "light");
const button = document.getElementById("theme-toggle");
if (button) {
button.textContent = label();
button.title = t("切换主题", "theme.toggle");
}
}
// init binds the toggle and re-renders the label after language switches.
// Cycles auto → light → dark so system-dark users keep their look until they
// make an explicit choice.
export function init() {
const button = document.getElementById("theme-toggle");
if (button)
button.addEventListener("click", () => {
mode = MODES[(MODES.indexOf(mode) + 1) % MODES.length];
try {
localStorage.setItem(STORAGE_KEY, mode);
} catch (_) {}
apply();
});
media.addEventListener("change", apply);
onChange(apply);
apply();
}