import { useEffect } from 'react'; import type { CSSProperties } from 'react'; import type { Category, Role, Theme } from '@two-keys/shared'; /** "#7b1e3a" → "123 30 58", the form Tailwind's rgb(var(--x) / alpha) wants. */ export function hexToRgb(hex: string): string { const n = parseInt(hex.slice(1), 16); return `${(n >> 16) & 255} ${(n >> 8) & 255} ${n & 255}`; } export function themeVars(theme: Theme): CSSProperties { return { '--primary': hexToRgb(theme.primary), '--accent': hexToRgb(theme.accent), '--bg': hexToRgb(theme.background), '--heading-font': `'${theme.headingFont}'`, } as CSSProperties; } /** Applies a game's theme to the whole page while a room is open. */ export function useTheme(theme: Theme | undefined) { const key = theme ? JSON.stringify(theme) : ''; useEffect(() => { if (!key) return; const root = document.documentElement; const vars = themeVars(JSON.parse(key)) as Record; for (const [name, value] of Object.entries(vars)) root.style.setProperty(name, value); return () => { for (const name of Object.keys(vars)) root.style.removeProperty(name); }; }, [key]); } export const CATEGORY_LABEL: Record = { 'murder-mystery': 'Murder Mystery', 'escape-room': 'Escape Room', 'co-op': 'Co-op', }; /** A is warm (amber), B is cool (teal). */ export const ROLE_TEXT: Record = { A: 'text-roleA', B: 'text-roleB' }; export const ROLE_BORDER: Record = { A: 'border-roleA/60', B: 'border-roleB/60' }; export const ROLE_BG: Record = { A: 'bg-roleA/15', B: 'bg-roleB/15' }; export const MAX_DIFFICULTY = 5; export const DIFFICULTY_LABEL: Record = { 1: 'Easy', 2: 'Medium', 3: 'Hard', 4: 'Expert', 5: 'Nightmare' }; export const difficultyKeys = (n: number) => '🔑'.repeat(Math.max(0, n));