4단계 방·대기실: 방 만들기(게스트 포함)·6자리 코드·열린 방 목록(🔒 비밀번호 방), 인원 무제한 대기실(방장·준비·넘기기·내보내기·60초 자리 유지), 방 설정 전체 즉시 반영, 조건에 맞는 곡 N곡 + 원곡 확인 중 곡 수(1분마다 다시 셈), 대기실 채팅, 서버 재시작 안내
- 서버 테스트 49개, 브라우저 e2e(e2e/rooms.e2e.ts, 3명) 11단계 - 사용자 결정(추천안)은 docs/12 "4단계 확인" Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
114
apps/web/src/components/ChatPanel.tsx
Normal file
114
apps/web/src/components/ChatPanel.tsx
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Room chat (docs/06): one-line log, system lines centred, my unconfirmed lines at the bottom.
|
||||
* Stage 4 uses it in the waiting room; the answer block and "입력 중" come with the game chat (stage 6).
|
||||
*/
|
||||
import { useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { CHAT_MAX, type ChatLine } from '@mq/shared/lite';
|
||||
|
||||
/** Fixed colour per user (joke-app --name-h). */
|
||||
function hue(id: string): number {
|
||||
let h = 0;
|
||||
for (let i = 0; i < id.length; i++) h = (h * 31 + id.charCodeAt(i)) >>> 0;
|
||||
return h % 360;
|
||||
}
|
||||
|
||||
export function ChatPanel({ lines, pending, meId, myName, onSend }: { lines: ChatLine[]; pending: { n: string; text: string }[]; meId: string; myName: string; onSend: (text: string) => void }) {
|
||||
const [text, setText] = useState('');
|
||||
const [stuck, setStuck] = useState(true);
|
||||
const [unseen, setUnseen] = useState(false);
|
||||
const logRef = useRef<HTMLDivElement>(null);
|
||||
const lastSent = useRef('');
|
||||
// Changes whenever a line arrives, also once the log is capped at 200 lines.
|
||||
const tick = `${lines.at(-1)?.seq ?? 0}:${pending.length}`;
|
||||
|
||||
// Auto-scroll only while the reader is at the bottom (docs/06 위로 스크롤하면 멈춤).
|
||||
useLayoutEffect(() => {
|
||||
const el = logRef.current;
|
||||
if (!el) return;
|
||||
if (stuck) el.scrollTop = el.scrollHeight;
|
||||
else setUnseen(true);
|
||||
}, [tick]);
|
||||
useEffect(() => {
|
||||
if (stuck) setUnseen(false);
|
||||
}, [stuck]);
|
||||
|
||||
const toBottom = () => {
|
||||
const el = logRef.current;
|
||||
if (el) el.scrollTop = el.scrollHeight;
|
||||
setStuck(true);
|
||||
};
|
||||
const submit = () => {
|
||||
const t = text.trim();
|
||||
if (!t) return;
|
||||
onSend(t.slice(0, CHAT_MAX));
|
||||
lastSent.current = t;
|
||||
setText('');
|
||||
toBottom();
|
||||
};
|
||||
|
||||
return (
|
||||
<section className="card chat" aria-label="채팅">
|
||||
<div
|
||||
className="chat-log"
|
||||
ref={logRef}
|
||||
role="log"
|
||||
aria-live="polite"
|
||||
onScroll={(e) => {
|
||||
const el = e.currentTarget;
|
||||
setStuck(el.scrollHeight - el.scrollTop - el.clientHeight < 24);
|
||||
}}
|
||||
>
|
||||
{lines.length === 0 && pending.length === 0 && <div className="chat-sys">아직 대화가 없어요. 먼저 인사해 보세요!</div>}
|
||||
{lines.map((l) =>
|
||||
l.userId === null ? (
|
||||
<div key={l.seq} className="chat-sys">
|
||||
— {l.text} —
|
||||
</div>
|
||||
) : (
|
||||
<div key={l.seq} className={`chat-line ${l.userId === meId ? 'me' : ''}`}>
|
||||
<b style={{ ['--name-h' as string]: hue(l.userId) }}>{l.nickname}</b>
|
||||
<span>{l.text}</span>
|
||||
</div>
|
||||
),
|
||||
)}
|
||||
{pending.map((p) => (
|
||||
<div key={p.n} className="chat-line me pending" aria-label="보내는 중">
|
||||
<b style={{ ['--name-h' as string]: hue(meId) }}>{myName}</b>
|
||||
<span>{p.text}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
{unseen && !stuck && (
|
||||
<button type="button" className="btn btn-sm chat-new" onClick={toBottom}>
|
||||
새 메시지 ↓
|
||||
</button>
|
||||
)}
|
||||
<form
|
||||
className="chat-input"
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
submit();
|
||||
}}
|
||||
>
|
||||
<input
|
||||
className="input"
|
||||
value={text}
|
||||
maxLength={CHAT_MAX}
|
||||
placeholder="메시지 입력 (Enter)"
|
||||
aria-label="채팅 입력"
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Escape') setText('');
|
||||
else if (e.key === 'ArrowUp' && !text && lastSent.current) {
|
||||
e.preventDefault();
|
||||
setText(lastSent.current);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<button className="btn btn-primary" disabled={!text.trim()}>
|
||||
보내기
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user