Files
codecraft/web/src/prefsSync.ts
T
Richard ZinkandClaude Opus 5.5 aa1f8cf016 Add API tokens, CLI server API and shared core crate
- Personal API tokens (full/read scope, expiry, revoke) and CLI password login
- Cargo workspace: codecraft-core (types, providers, permission rules) shared by
  server and the upcoming terminal client; server re-exports the old paths
- CLI server API: /cli/config, /cli/skill, /cli/tool (server MCP tools),
  /llm/stream model gateway with budgets and usage per user
- Theme/language preferences mirrored to the server; personal memory endpoint
- Statistics: provider and mode tables stacked; CLI usage labelled
- cli/: work in progress (terminal client is being rewritten as a local TUI)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 23:18:50 +01:00

38 lines
1.3 KiB
TypeScript

import { get, put } from "./api";
import { getLang, onLangChange, setLang } from "./i18n";
import { applyTheme, getTheme, getThemeMode, onThemeChange, THEMES, type Theme, type ThemeMode } from "./theme";
/**
* Theme and language are stored per device (localStorage) and mirrored to the
* server, so the command line client uses the same colours and language and a
* new device starts with your settings.
*/
let timer: ReturnType<typeof setTimeout> | undefined;
let started = false;
function push() {
clearTimeout(timer);
timer = setTimeout(() => {
void put("/account/prefs", { theme: getTheme(), theme_mode: getThemeMode(), lang: getLang() }).catch(() => undefined);
}, 800);
}
export async function startPrefsSync() {
if (started) return;
started = true;
// A device without own settings takes the server's.
const fresh = !localStorage.getItem("codecraft.theme") && !localStorage.getItem("codecraft.themeMode");
try {
const p = await get<{ theme?: string; theme_mode?: string; lang?: string }>("/account/prefs");
if (fresh && p.theme && THEMES.some((t) => t.id === p.theme)) {
applyTheme(p.theme as Theme, (p.theme_mode as ThemeMode) ?? "auto");
}
if (fresh && p.lang) setLang(p.lang);
if (!p.theme) push();
} catch {
/* offline or old server */
}
onThemeChange(push);
onLangChange(push);
}