120 lines
4.0 KiB
TypeScript
120 lines
4.0 KiB
TypeScript
/** Who am I + display settings (docs/05 §7, docs/07 §2). */
|
|
import { create } from 'zustand';
|
|
import type { CatalogEntry, PublicUser } from '@bg/shared/lite';
|
|
import { api, type Settings } from '../net/api';
|
|
import { socket } from '../net/socket';
|
|
import { setGameSpeed } from '../lib/motion';
|
|
import { setChatSound } from '../lib/feedback';
|
|
|
|
const LS_KEY = 'bg.settings';
|
|
|
|
export const DEFAULT_SETTINGS: Required<Settings> = {
|
|
fontScale: 'normal',
|
|
theme: 'auto',
|
|
sound: true,
|
|
volume: 50,
|
|
vibrate: true,
|
|
colorBlind: false,
|
|
// Two-step placing is the default only on touch devices; on PC one click places (docs/07 §3.5).
|
|
tapConfirm: typeof matchMedia !== 'undefined' ? matchMedia('(pointer: coarse)').matches : true,
|
|
};
|
|
|
|
/** Settings that belong to this device and are never synced to the account. */
|
|
const DEVICE_ONLY = ['tapConfirm', 'volume'] as const;
|
|
const withoutDeviceOnly = (s: Settings): Settings => {
|
|
const out: Record<string, unknown> = { ...s };
|
|
for (const k of DEVICE_ONLY) delete out[k];
|
|
return out as Settings;
|
|
};
|
|
|
|
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;
|
|
html.dataset.colorBlind = String(s.colorBlind);
|
|
const dark = s.theme === 'dark' || (s.theme === 'auto' && matchMedia('(prefers-color-scheme: dark)').matches);
|
|
document.querySelector('meta[name="theme-color"]')?.setAttribute('content', dark ? '#16181d' : '#F7F5F0');
|
|
}
|
|
|
|
interface SessionState {
|
|
loaded: boolean;
|
|
me: PublicUser | null;
|
|
activeRoom: string | null;
|
|
discordEnabled: boolean;
|
|
catalog: (CatalogEntry & { notice?: string })[];
|
|
role: 'user' | 'admin' | 'superadmin';
|
|
siteName: string;
|
|
announcement: string;
|
|
maintenance: boolean;
|
|
settings: Required<Settings>;
|
|
useAvatar: boolean;
|
|
hasDiscordAvatar: boolean;
|
|
refresh(): Promise<void>;
|
|
setMe(me: PublicUser | null): void;
|
|
updateSettings(patch: Settings): void;
|
|
}
|
|
|
|
export const useSession = create<SessionState>((set, get) => ({
|
|
loaded: false,
|
|
me: null,
|
|
activeRoom: null,
|
|
discordEnabled: false,
|
|
catalog: [],
|
|
role: 'user',
|
|
siteName: '같이 놀자 보드게임',
|
|
announcement: '',
|
|
maintenance: false,
|
|
settings: loadLocal(),
|
|
useAvatar: true,
|
|
hasDiscordAvatar: false,
|
|
async refresh() {
|
|
const [cfg, me] = await Promise.all([
|
|
api.config().catch(() => ({ discord: false, catalog: [] as CatalogEntry[], siteName: '같이 놀자 보드게임', announcement: '', maintenance: false, gameSpeed: 1, chatSound: undefined })),
|
|
api.me().catch(() => ({ me: null }) as Awaited<ReturnType<typeof api.me>>),
|
|
]);
|
|
const settings = { ...get().settings, ...withoutDeviceOnly(me.settings ?? {}) };
|
|
applySettings(settings);
|
|
localStorage.setItem(LS_KEY, JSON.stringify(settings));
|
|
document.title = cfg.siteName;
|
|
setGameSpeed(cfg.gameSpeed);
|
|
setChatSound(cfg.chatSound);
|
|
set({
|
|
loaded: true,
|
|
me: me.me,
|
|
activeRoom: me.activeRoom ?? null,
|
|
discordEnabled: cfg.discord,
|
|
catalog: cfg.catalog,
|
|
role: me.role ?? 'user',
|
|
siteName: cfg.siteName,
|
|
announcement: cfg.announcement,
|
|
maintenance: cfg.maintenance,
|
|
settings,
|
|
useAvatar: me.useAvatar ?? true,
|
|
hasDiscordAvatar: me.hasDiscordAvatar ?? false,
|
|
});
|
|
// Holding a seat: stay connected on every page so the server knows the site is still open (a game whose
|
|
// players all closed the site gets closed, docs/06 §11).
|
|
if (me.activeRoom) socket.start();
|
|
},
|
|
setMe(me) {
|
|
set({ me });
|
|
},
|
|
updateSettings(patch) {
|
|
const settings = { ...get().settings, ...patch };
|
|
applySettings(settings);
|
|
localStorage.setItem(LS_KEY, JSON.stringify(settings));
|
|
set({ settings });
|
|
if (get().me) api.updateMe({ settings: withoutDeviceOnly(settings) }).catch(() => {});
|
|
},
|
|
}));
|
|
|
|
applySettings(loadLocal());
|