/** * One chat message, shared by the room chat and the games with their own chat (마피아, 라이어 게임): the name sits on * its own small coloured line above a bubble with the text, so who said it and what they said never run together. * My own messages sit on the right without a name; a run of messages from the same person shows the name once. * Hidden names stay in the DOM for screen readers (`.sr-only`). */ import type { CSSProperties, ReactNode } from 'react'; /** Hues that stay readable as text on the panel in both themes (lightness comes from `--chat-name-l`). */ const HUES = [212, 152, 28, 275, 335, 188, 48, 0, 248, 100]; export function nameHue(id: string): number { let h = 0; for (let i = 0; i < id.length; i++) h = (h * 31 + id.charCodeAt(i)) >>> 0; return HUES[h % HUES.length]!; } export function ChatMessage({ from, name, mine = false, cont = false, className, children }: { from: string; name: string; mine?: boolean; cont?: boolean; className?: string; children: ReactNode }) { return (
{mine ? '나' : name} {children}
); } /** Same sender as the message right before, within 2 minutes: the name is not repeated. */ type RunKey = { from?: string | null; at: number } | undefined; export const sameRun = (prev: RunKey, cur: RunKey): boolean => !!prev && !!cur?.from && prev.from === cur.from && cur.at - prev.at < 120_000;