Files
Jason Xu e2b2a7c281 refactor: harden gateway and document Go/Zen OAuth
Preserve Go and Zen OAuth routing while fixing authentication, secret redaction, streaming, transport, and configuration issues.

Refresh README and management guides in four languages while preserving sponsors, LinuxDo acknowledgements, and Linux/Docker quick start instructions.
2026-11-18 03:01:56 +08:00

54 lines
1.7 KiB
JavaScript

// Theme handling: an auto/light/dark cycle persisted per browser. The
// theme-init 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();
}