/** * Public event presentation config: the colour theme and the comments feature flag, * served unauthenticated from `GET /api/v1/event`. Loaded once on boot (and refreshed * on the `event-updated` SSE) so the theme applies before the user has even joined and * the comment UI reflects the instance setting. * * The resolved theme CSS is cached in localStorage so the boot script in app.html can * paint the right colours before the JS bundle loads (no flash of the default palette). */ import { writable } from 'svelte/store'; import { buildPaletteCss, applyPaletteCss, type ThemeConfig } from './theme/palette'; export const PALETTE_CACHE_KEY = 'eventsnap_palette_css'; export type EventConfig = { name: string; slug: string; commentsEnabled: boolean; theme: ThemeConfig; }; export const eventConfig = writable(null); /** Convenience flag for the comment UI. Optimistic `true` until /event resolves. */ export const commentsEnabled = writable(true); /** Fetch /event, apply the theme, and cache the resolved CSS for the next boot. */ export async function loadEventConfig(): Promise { try { const res = await fetch('/api/v1/event'); if (!res.ok) return; const b = await res.json(); const theme: ThemeConfig = { preset: b.theme_preset ?? 'champagne-gold', primary: b.theme_primary ?? '#8a6a2b', accent: b.theme_accent ?? '#8a6a2b' }; eventConfig.set({ name: b.name, slug: b.slug, commentsEnabled: b.comments_enabled ?? true, theme }); commentsEnabled.set(b.comments_enabled ?? true); const css = buildPaletteCss(theme); applyPaletteCss(css); try { localStorage.setItem(PALETTE_CACHE_KEY, css); } catch { /* private mode / quota — the theme still applied for this session */ } } catch { /* offline / server down — keep whatever the boot script already painted */ } } /** Apply a theme immediately without persisting — used by the admin live preview. */ export function previewTheme(theme: ThemeConfig): void { applyPaletteCss(buildPaletteCss(theme)); }