채팅: 가리기 삭제, 100줄이 찬 뒤에도 맨 아래 고정(+새 메시지 수), 이름 줄과 말풍선으로 이름·내용 구분
- 공통 ChatMessage(이름 줄 + 말풍선, 내 말은 오른쪽, 이어 말하면 이름 한 번) — 방 채팅·마피아·라이어 게임, 그림 맞히기 이름 표시 - useStickToBottom: 개수 대신 목록 내용 변화를 보고 맨 아래 유지, 위로 올려 읽는 중엔 그대로, 보내면 다시 맨 아래 - 휴대폰 채팅 버튼의 새 메시지 수도 100줄 뒤에 0이 되던 문제(마지막 본 id 기준으로) - e2e/chat.e2e.ts: PC 대기실·게임 옆 칸·휴대폰 시트 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,13 +5,15 @@
|
||||
* (`ownChat`), so nobody can hint the word around the game. The word sits on a hold-to-see card (screen sharing,
|
||||
* people next to you). Descriptions are a separate, round-by-round list next to the free-talk chat.
|
||||
*/
|
||||
import { useEffect, useRef, useState, type KeyboardEvent, type ReactNode } from 'react';
|
||||
import { useEffect, useRef, useState, type CSSProperties, type KeyboardEvent, type ReactNode } from 'react';
|
||||
import type { LiarFinal, LiarOptions, LiarView, PublicEvent } from '@bg/games/liar';
|
||||
import { TOPIC_IDS, TOPIC_KO } from '@bg/games/liar/topics';
|
||||
import type { PublicUser } from '@bg/shared/lite';
|
||||
import { Avatar } from '../../components/ui';
|
||||
import { useServerNow } from '../../components/TurnTimer';
|
||||
import { haptic, sounds } from '../../lib/feedback';
|
||||
import { ChatMessage, nameHue, sameRun } from '../../components/ChatMessage';
|
||||
import { useStickToBottom } from '../../lib/useStickToBottom';
|
||||
import type { BoardProps, GameUI, OptionsProps } from '../types';
|
||||
import { helpNow } from './help';
|
||||
import { quickStart } from './quick';
|
||||
@@ -99,17 +101,14 @@ button.lg-tile { cursor: pointer; }
|
||||
.lg-tag.liar { background: #b91c1c; }
|
||||
.lg-desc { margin: 0; padding: 0; list-style: none; display: grid; gap: 5px; }
|
||||
.lg-desc li { background: var(--surface-2); border-radius: 10px; padding: 6px 10px; line-height: 1.4; overflow-wrap: anywhere; }
|
||||
.lg-desc li strong { margin-right: 6px; }
|
||||
.lg-desc li.mine strong { color: var(--primary); }
|
||||
.lg-desc li strong { display: block; font-size: .8rem; font-weight: 800; color: hsl(var(--name-h, 212) 70% var(--chat-name-l)); }
|
||||
.lg-desc li.mine { background: var(--primary-soft); }
|
||||
.lg-desc li.skip { color: var(--text-sub); font-style: italic; }
|
||||
.lg-desc li.now { border: 2px dashed #7c3aed; background: transparent; color: var(--text-sub); }
|
||||
.lg-round { font-size: .82rem; font-weight: 800; color: var(--text-sub); margin-top: 2px; }
|
||||
.lg-chat { flex: 1; min-height: 0; padding: 0; overflow: hidden; gap: 0; }
|
||||
.lg-chat h2 { padding: 10px 12px 6px; border-bottom: 1px solid var(--border); }
|
||||
.lg-msgs { flex: 1; min-height: 140px; overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column; gap: 5px; }
|
||||
.lg-msg { line-height: 1.4; overflow-wrap: anywhere; }
|
||||
.lg-msg strong { margin-right: 6px; }
|
||||
.lg-msg.mine strong { color: var(--primary); }
|
||||
.lg-msg.sys { font-size: .86rem; font-weight: 700; color: var(--text-sub); background: var(--surface-2); border-radius: 8px; padding: 3px 8px; }
|
||||
.lg-form { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--border); flex-wrap: nowrap; }
|
||||
.lg-form .input { flex: 1; min-height: 44px; min-width: 0; }
|
||||
@@ -359,10 +358,7 @@ function Players({ v, players, me, pick, pending }: { v: LiarView; players: Reco
|
||||
|
||||
function Descriptions({ v, players, me }: { v: LiarView; players: Record<string, PublicUser>; me: string | null }) {
|
||||
const rounds = Array.from({ length: v.rounds }, (_, i) => i + 1).filter((r) => r === 1 || v.descriptions.some((d) => d.round === r) || (v.phase.kind === 'describe' && v.phase.round >= r));
|
||||
const listRef = useRef<HTMLDivElement>(null);
|
||||
useEffect(() => {
|
||||
listRef.current?.scrollTo({ top: listRef.current.scrollHeight });
|
||||
}, [v.descriptions.length]);
|
||||
const { ref: listRef } = useStickToBottom<HTMLDivElement>();
|
||||
return (
|
||||
<section className="lg-box" aria-label="설명 기록">
|
||||
<h2>📝 설명 기록</h2>
|
||||
@@ -375,13 +371,13 @@ function Descriptions({ v, players, me }: { v: LiarView; players: Record<string,
|
||||
{v.descriptions
|
||||
.filter((d) => d.round === r)
|
||||
.map((d) => (
|
||||
<li key={d.n} className={`${d.from === me ? 'mine' : ''}${d.text === null ? ' skip' : ''}`}>
|
||||
<li key={d.n} className={`${d.from === me ? 'mine' : ''}${d.text === null ? ' skip' : ''}`} style={{ '--name-h': nameHue(d.from) } as CSSProperties}>
|
||||
<strong>{nick(players, d.from)}</strong>
|
||||
{d.text ?? '(시간이 지나 넘어갔어요)'}
|
||||
</li>
|
||||
))}
|
||||
{v.phase.kind === 'describe' && v.phase.round === r && v.phase.speaker && (
|
||||
<li className="now">
|
||||
<li className="now" style={{ '--name-h': nameHue(v.phase.speaker) } as CSSProperties}>
|
||||
<strong>{nick(players, v.phase.speaker)}</strong>
|
||||
{v.phase.speaker === me ? '내 차례예요. 한 문장으로 설명해 주세요…' : '설명하는 중…'}
|
||||
</li>
|
||||
@@ -395,18 +391,16 @@ function Descriptions({ v, players, me }: { v: LiarView; players: Record<string,
|
||||
}
|
||||
|
||||
type Line = ({ k: 'chat'; text: string; from: string; key: string } | { k: 'sys'; e: PublicEvent }) & { at: number };
|
||||
const runKey = (l: Line | undefined) => l && { from: l.k === 'chat' ? l.from : null, at: l.at };
|
||||
|
||||
function ChatBox({ v, players, me, act }: { v: LiarView; players: Record<string, PublicUser>; me: string | null; act: (a: unknown) => void }) {
|
||||
const [text, setText] = useState('');
|
||||
const listRef = useRef<HTMLDivElement>(null);
|
||||
const { ref: listRef, pin } = useStickToBottom<HTMLDivElement>();
|
||||
const lines: Line[] = [
|
||||
...v.events.filter((e) => e.t !== 'phase' || e.kind === 'discussion' || e.kind === 'vote' || e.kind === 'guess').map((e) => ({ k: 'sys' as const, e, at: e.at })),
|
||||
...v.chat.map((m) => ({ k: 'chat' as const, text: m.text, from: m.from, key: `c${m.n}`, at: m.at })),
|
||||
// Stable sort: same moment keeps the event order, announcements before chat.
|
||||
].sort((a, b) => a.at - b.at || (a.k === b.k ? 0 : a.k === 'sys' ? -1 : 1));
|
||||
useEffect(() => {
|
||||
listRef.current?.scrollTo({ top: listRef.current.scrollHeight });
|
||||
}, [v.chat.length, v.events.length]);
|
||||
const talk = v.phase.kind === 'discussion' || v.phase.kind === 'vote';
|
||||
const canWrite = !!v.my?.canChat && talk;
|
||||
const why = !me || !v.my ? '관전 중에는 채팅을 쓸 수 없어요.' : talk ? (v.my.chatWhy ?? '') : v.phase.kind === 'describe' ? '토론 채팅은 설명이 다 끝나면 열려요.' : (v.my.chatWhy ?? '');
|
||||
@@ -415,16 +409,15 @@ function ChatBox({ v, players, me, act }: { v: LiarView; players: Record<string,
|
||||
<h2>💬 토론 채팅</h2>
|
||||
<div ref={listRef} className="lg-msgs" aria-live="polite">
|
||||
{lines.length === 0 && <p className="muted small" style={{ margin: 0 }}>아직 메시지가 없어요.</p>}
|
||||
{lines.map((l) =>
|
||||
{lines.map((l, i) =>
|
||||
l.k === 'sys' ? (
|
||||
<div key={`e${l.e.n}`} className="lg-msg sys">
|
||||
📢 {eventText(l.e, players)}
|
||||
</div>
|
||||
) : (
|
||||
<div key={l.key} className={`lg-msg${l.from === me ? ' mine' : ''}`}>
|
||||
<strong>{nick(players, l.from)}</strong>
|
||||
<ChatMessage key={l.key} className="lg-msg" from={l.from} name={nick(players, l.from)} mine={l.from === me} cont={sameRun(runKey(lines[i - 1]), runKey(l))}>
|
||||
{l.text}
|
||||
</div>
|
||||
</ChatMessage>
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
@@ -437,6 +430,7 @@ function ChatBox({ v, players, me, act }: { v: LiarView; players: Record<string,
|
||||
if (!t) return;
|
||||
act({ type: 'chat', text: t.slice(0, 200) });
|
||||
setText('');
|
||||
pin();
|
||||
}}
|
||||
>
|
||||
<input className="input" value={text} onChange={(e) => setText(e.target.value)} maxLength={200} placeholder="누가 라이어일까요?" aria-label="토론 채팅 메시지" enterKeyHint="send" />
|
||||
|
||||
Reference in New Issue
Block a user