/** Chat list + quick reactions + input. Inline on PC, inside a sheet on phones. */ import { useEffect, useRef, useState } from 'react'; import { CHAT_MAX, EMOTE_LABEL, EMOTES, type PublicUser, type RoomView } from '@bg/shared/lite'; import { create } from 'zustand'; import { socket, useRoom } from '../net/socket'; import { useChatOutbox, useTypingSignal } from '../lib/chat-input'; import { useStickToBottom } from '../lib/useStickToBottom'; import { BotActivity } from './BotActivity'; import { ChatMessage, sameRun } from './ChatMessage'; /** Id of the newest chat line shown in an open chat panel (ids only grow; the list itself keeps the last 100). */ export const useChatSeen = create<{ seen: number }>(() => ({ seen: 0 })); export function ChatPanel({ room, me, title = true }: { room: RoomView; me: PublicUser; title?: boolean }) { const chat = useRoom((s) => s.chat); const [text, setText] = useState(''); const { ref: listRef, pin } = useStickToBottom(); const inputRef = useRef(null); const send = useChatOutbox((t) => socket.send({ t: 'chat', text: t })); const typing = useTypingSignal('room', room.chatEnabled); const lastId = chat.at(-1)?.id ?? 0; useEffect(() => { useChatSeen.setState({ seen: lastId }); }, [lastId]); // Enter anywhere (outside inputs) focuses chat — PC shortcut (docs/07 §3.5). useEffect(() => { const onKey = (e: KeyboardEvent) => { const t = e.target as HTMLElement; if (e.key === 'Enter' && !['INPUT', 'TEXTAREA', 'SELECT', 'BUTTON'].includes(t.tagName) && t.getAttribute('role') !== 'grid') { e.preventDefault(); inputRef.current?.focus(); } }; window.addEventListener('keydown', onKey); return () => window.removeEventListener('keydown', onKey); }, []); return (
{title &&

채팅

}
{chat.length === 0 &&

아직 메시지가 없어요. Enter를 누르면 바로 입력할 수 있어요.

} {chat.map((m, i) => m.from ? ( {m.text} ) : (
알림 {m.text}
), )}
{EMOTES.map((id) => ( ))}
{room.chatEnabled ? (
{ e.preventDefault(); if (text.trim()) send(text.trim().slice(0, CHAT_MAX)); setText(''); typing.stop(); pin(); }} > { setText(e.target.value); typing.onText(e.target.value); }} onBlur={typing.stop} onKeyDown={(e) => e.key === 'Escape' && (e.currentTarget as HTMLInputElement).blur()} maxLength={CHAT_MAX} placeholder="메시지 입력 (Enter)" aria-label="메시지" enterKeyHint="send" />
) : (

이 방은 채팅을 쓰지 않아요. 반응 버튼은 쓸 수 있어요.

)}
); } const chatKey = (m: { from?: { id: string } | null; at: number } | undefined) => m && { from: m.from?.id ?? null, at: m.at }; /** Floating emote bubbles (both layouts). */ export function EmoteBubbles({ room }: { room: RoomView }) { const emotes = useRoom((s) => s.emotes); const nick = (id: string) => room.seats.find((s) => s?.user.id === id)?.user.nickname ?? room.spectators.find((u) => u.id === id)?.nickname ?? ''; return (
{emotes.map((e) => (
{nick(e.from)}: {EMOTE_LABEL[e.emote as keyof typeof EMOTE_LABEL] ?? ''}
))}
); }