Files
music_quiz/apps/web/src/store/session.ts
EJClaw 391bbe69e5 1단계 뼈대: joke-app에서 로그인(디스코드·게스트)·세션·권한·관리자·DB를 가져와 음악퀴즈용으로 정리, 새 디자인·아이콘, WS 연결·시계 맞추기, 테스트·e2e
- apps/server: Hono API(auth/me/config/admin), Bun WebSocket(welcome·ping/pong, 압축 끔), SQLite 마이그레이션
- 사이트 설정: 채팅 도배 방지 기본 꺼짐(관리자가 켬, 초당·순간 횟수)
- apps/web: 첫 화면·로그인·관리자(대시보드·사이트·사용자·관리자·기록), 라이트/다크, 휴대폰, noindex
- packages/shared: 닉네임 규칙, 프로토콜(zod), 시계 차이 계산
- 테스트 20개 + 브라우저 e2e 6단계 통과

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 19:44:47 +09:00

78 lines
2.4 KiB
TypeScript

/** Who am I + display settings (joke-app pattern). */
import { create } from 'zustand';
import type { PublicUser, Role } from '@mq/shared/lite';
import { api, type Settings } from '../net/api';
const LS_KEY = 'mq.settings';
export const DEFAULT_SETTINGS: Required<Settings> = { fontScale: 'normal', theme: 'auto' };
function loadLocal(): Required<Settings> {
try {
return { ...DEFAULT_SETTINGS, ...JSON.parse(localStorage.getItem(LS_KEY) ?? '{}') };
} catch {
return DEFAULT_SETTINGS;
}
}
export function applySettings(s: Required<Settings>): void {
const html = document.documentElement;
html.dataset.fontScale = s.fontScale;
html.dataset.theme = s.theme;
const dark = s.theme === 'dark' || (s.theme === 'auto' && matchMedia('(prefers-color-scheme: dark)').matches);
document.querySelector('meta[name="theme-color"]')?.setAttribute('content', dark ? '#17150f' : '#f5efe3');
}
interface SessionState {
loaded: boolean;
me: PublicUser | null;
role: Role;
discordEnabled: boolean;
siteName: string;
announcement: string;
maintenance: boolean;
settings: Required<Settings>;
refresh(): Promise<void>;
updateSettings(patch: Settings): void;
}
export const useSession = create<SessionState>((set, get) => ({
loaded: false,
me: null,
role: 'user',
discordEnabled: false,
siteName: '음악퀴즈',
announcement: '',
maintenance: false,
settings: loadLocal(),
async refresh() {
const [cfg, me] = await Promise.all([
api.config().catch(() => ({ discord: false, siteName: '음악퀴즈', announcement: '', maintenance: false })),
api.me().catch(() => ({ me: null }) as Awaited<ReturnType<typeof api.me>>),
]);
const settings = { ...get().settings, ...(me.settings ?? {}) };
applySettings(settings);
localStorage.setItem(LS_KEY, JSON.stringify(settings));
document.title = cfg.siteName;
set({
loaded: true,
me: me.me,
role: me.role ?? 'user',
discordEnabled: cfg.discord,
siteName: cfg.siteName,
announcement: cfg.announcement,
maintenance: cfg.maintenance,
settings,
});
},
updateSettings(patch) {
const settings = { ...get().settings, ...patch };
applySettings(settings);
localStorage.setItem(LS_KEY, JSON.stringify(settings));
set({ settings });
if (get().me) api.updateMe({ settings }).catch(() => {});
},
}));
applySettings(loadLocal());