/** * 방 설정 (docs/04, docs/12): host edits, everyone else sees the same panel read-only. * Every change is sent at once as a partial patch; the server validates and broadcasts. */ import { useEffect, useRef, useState, type ReactNode } from 'react'; import { COUNTS, HINT_TIMES, LEVELS, LISTEN_SECS, LYRICS_SECS, MODES, REVEAL_SECS, VOTE_RULES, listenLabel, voteLabel, type RoomConfig, type VoteRule, } from '@mq/shared/lite'; import { apiCall } from '../net/api'; export interface CatalogMeta { genres: { id: string; name: string; grp: 'region' | 'style' | 'theme' }[]; years: { min: number; max: number }; } type Patch = Partial; function Seg({ value, options, onChange, disabled, label }: { value: T; options: { v: T; label: ReactNode }[]; onChange: (v: T) => void; disabled: boolean; label: string }) { return (
{options.map((o) => ( ))}
); } function Section({ title, children, tip }: { title: string; children: ReactNode; tip?: string }) { return (
{title} {tip && ( ! )}
{children}
); } const STAR_COLOR = (n: number) => (n <= 3 ? 'var(--star-1)' : n <= 6 ? 'var(--star-2)' : n <= 8 ? 'var(--star-3)' : 'var(--star-4)'); function Stars({ level, onChange, disabled }: { level: number; onChange: (n: number) => void; disabled: boolean }) { const [hover, setHover] = useState(null); const shown = hover ?? level; const [name, desc] = LEVELS[shown - 1]!; return (
setHover(null)}> {Array.from({ length: 10 }, (_, i) => { const n = i + 1; const filled = n <= shown; return ( ); })}
{shown}단계 · {name} {desc}
); } /** Two-handle year range (docs/04 연도, 참고 이미지 1.png) + 부터/까지 selects + decade chips. */ function YearRange({ from, to, min, max, onChange, disabled }: { from: number | null; to: number | null; min: number; max: number; onChange: (f: number | null, t: number | null) => void; disabled: boolean }) { const f = from ?? min; const t = to ?? max; const pos = (y: number) => ((y - min) / Math.max(1, max - min)) * 100; const years = Array.from({ length: max - min + 1 }, (_, i) => min + i); const set = (a: number, b: number) => onChange(a <= min ? null : a, b >= max ? null : b); const decades = [1980, 1990, 2000, 2010, 2020].filter((d) => d + 9 >= min && d <= max); return (
{f} {t} set(Math.min(Number(e.target.value), t), t)} /> set(f, Math.max(Number(e.target.value), f))} />
{decades.map((d) => ( ))}
); } /** 가수 선택 (docs/04 가수노래): search → add chip; tapping a chip removes it. */ function ArtistPicker({ selected, onChange, disabled }: { selected: { id: number; name: string }[]; onChange: (ids: number[]) => void; disabled: boolean }) { const [q, setQ] = useState(''); const [hits, setHits] = useState<{ id: number; name: string; songs: number }[]>([]); const timer = useRef | null>(null); useEffect(() => { if (timer.current) clearTimeout(timer.current); if (!q.trim()) return setHits([]); timer.current = setTimeout(() => { apiCall<{ artists: { id: number; name: string; songs: number }[] }>('GET', `/api/artists?q=${encodeURIComponent(q.trim())}`) .then((r) => setHits(r.artists)) .catch(() => setHits([])); }, 200); }, [q]); const ids = selected.map((a) => a.id); return (
{selected.length === 0 && 아직 고른 가수가 없어요 (1명 이상 필요)} {selected.map((a) => ( ))}
{!disabled && ( <> setQ(e.target.value)} aria-label="가수 검색" /> {hits.length > 0 && (
{hits.map((h) => ( ))}
)} )}
); } export function RoomSettings({ config, editable, meta, artists, pool, hasPassword, onPatch, onPassword, }: { config: RoomConfig; editable: boolean; meta: CatalogMeta | null; artists: { id: number; name: string }[]; pool: { playable: number; total: number }; hasPassword: boolean; onPatch: (p: Patch) => void; onPassword: (v: string | null) => void; }) { const ro = !editable; const c = config; const [title, setTitle] = useState(c.title); const [pw, setPw] = useState(''); useEffect(() => setTitle(c.title), [c.title]); const groups: [string, 'region' | 'style' | 'theme'][] = [ ['지역', 'region'], ['장르', 'style'], ['테마', 'theme'], ]; const toggleGenre = (id: string) => onPatch({ genres: c.genres.includes(id) ? c.genres.filter((g) => g !== id) : [...c.genres, id] }); const vote = (rules: readonly VoteRule[]) => rules.map((v) => ({ v, label: voteLabel(v) })); const listening = c.mode !== 'lyrics'; return (

방 설정

{editable ? '바꾸면 바로 모두에게 보여요' : '방장만 바꿀 수 있어요'}
setTitle(e.target.value)} onBlur={() => title.trim() !== c.title && onPatch({ title: title.trim() })} onKeyDown={(e) => e.key === 'Enter' && (e.target as HTMLInputElement).blur()} aria-label="방 이름" />
onPatch({ mode })} options={MODES.map((m) => ({ v: m.id, label: `${m.icon} ${m.label}` }))} />
{listening ? ( <>
듣고맞출시간
onPatch({ listenSec })} options={LISTEN_SECS.map((v) => ({ v, label: listenLabel(v) }))} />
재생위치
onPatch({ startAt })} options={[ { v: 'start', label: '처음부터' }, { v: 'highlight', label: '하이라이트부터' }, ]} />
{c.mode === 'artist' && (
가수 선택
onPatch({ artistIds })} />
)} ) : (
보고맞출시간
onPatch({ lyricsSec })} options={LYRICS_SECS.map((v) => ({ v, label: v ? `${v}초` : '무제한' }))} />
)}
전체
{groups.map(([label, grp]) => ( {meta?.genres .filter((g) => g.grp === grp) .map((g) => ( ))} ))}

같은 줄은 "또는", 다른 줄끼리는 "그리고"예요. 예) K-POP + 발라드 = 국내 발라드

{meta && onPatch({ yearFrom, yearTo })} />}
onPatch({ level })} />
1~3 쉬움 4~6 보통 7~8 어려움 9~10 극악
onPatch({ answer })} options={[ { v: 'title', label: '제목만' }, { v: 'artist', label: '가수만' }, { v: 'both', label: '제목+가수 (순서 무관)' }, ]} />
{( [ ['space', '띄어쓰기'], ['number', '숫자'], ['symbol', '특수문자'], ] as const ).map(([k, label]) => ( ))}
onPatch({ hint })} options={[{ v: 'none' as const, label: '없음' }, ...HINT_TIMES.map((v) => ({ v, label: `${v}초` })), { v: 'manual' as const, label: '직접선택' }]} /> {c.hint === 'manual' && (
힌트인원(이상)
onPatch({ hintVotes: hintVotes as RoomConfig['hintVotes'] })} options={vote(VOTE_RULES.filter((v) => v !== 'none'))} />
)}
onPatch({ skipVotes })} options={vote(VOTE_RULES)} />
{listening && (
onPatch({ replayVotes })} options={vote(VOTE_RULES)} />
)}
onPatch({ revealSec })} options={REVEAL_SECS.map((v) => ({ v, label: `${v}초` }))} />
onPatch({ nextVotes })} options={vote(VOTE_RULES)} />
onPatch({ count })} options={COUNTS.map((v) => ({ v, label: v ? String(v) : `무제한(${pool.playable.toLocaleString()})` }))} />
onPatch({ visibility })} options={[ { v: 'public', label: '공개' }, { v: 'private', label: '비공개(코드로만)' }, { v: 'password', label: '비밀번호' }, ]} /> {c.visibility === 'password' && editable && (
{ e.preventDefault(); if (pw.trim()) onPassword(pw.trim()); setPw(''); }} > setPw(e.target.value)} aria-label="방 비밀번호" /> {hasPassword ? 비밀번호 있음 : 비밀번호를 정해야 잠겨요}
)} {c.visibility === 'password' && !editable && hasPassword &&

🔒 비밀번호로 잠긴 방이에요

} {c.visibility === 'private' &&

방 목록에 나오지 않아요. 코드나 링크로만 들어올 수 있어요.

}
); } function GenreRow({ label, children }: { label: string; children: ReactNode }) { return ( <> {label}
{children}
); }