Files
glchat/web/src/stores/ui.ts
T
grendervill 642d0f720e feat(web): заготовка веб-клиента на Vite + React 19 + TypeScript strict
- стек по §5.2: React 19, React Router, TanStack Query, Zustand, Tailwind CSS 4,
  i18next (ru/en), Vitest + Testing Library
- дизайн-токены §3 docs/client-web.md (тёмная тема по умолчанию, светлая, accent)
- типизированный API-клиент §8.1: единый формат ошибок, ApiError с кодом
- App: экран состояния фундамента с readz/meta, локализация ошибок по code
- тесты: клиент API, форматирование, рендер App (успех/ошибка кода/сеть)
- eslint + prettier + tsc strict; initial JS 97.6 KB gzip
2026-09-19 18:20:59 +03:00

44 lines
976 B
TypeScript

import { create } from 'zustand';
export type Theme = 'dark' | 'light';
interface UiState {
theme: Theme;
setTheme: (theme: Theme) => void;
toggleTheme: () => void;
}
const STORAGE_KEY = 'glchat.theme';
function initialTheme(): Theme {
if (typeof window === 'undefined') {
return 'dark';
}
const stored = window.localStorage.getItem(STORAGE_KEY);
if (stored === 'light' || stored === 'dark') {
return stored;
}
return 'dark';
}
export function applyTheme(theme: Theme): void {
if (typeof document === 'undefined') {
return;
}
document.documentElement.dataset['theme'] = theme;
}
export const useUiStore = create<UiState>((set, get) => ({
theme: initialTheme(),
setTheme: (theme) => {
if (typeof window !== 'undefined') {
window.localStorage.setItem(STORAGE_KEY, theme);
}
applyTheme(theme);
set({ theme });
},
toggleTheme: () => {
get().setTheme(get().theme === 'dark' ? 'light' : 'dark');
},
}));