116 lines
4.8 KiB
TypeScript
116 lines
4.8 KiB
TypeScript
/** 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<HTMLDivElement>();
|
|
const inputRef = useRef<HTMLInputElement>(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 (
|
|
<section className="chat-panel" aria-label="채팅">
|
|
{title && <h2 style={{ fontSize: '1.1rem' }}>채팅</h2>}
|
|
<div ref={listRef} className="chat-list" aria-live="polite">
|
|
{chat.length === 0 && <p className="muted small">아직 메시지가 없어요. Enter를 누르면 바로 입력할 수 있어요.</p>}
|
|
{chat.map((m, i) =>
|
|
m.from ? (
|
|
<ChatMessage key={m.id} from={m.from.id} name={m.from.nickname} mine={m.from.id === me.id} cont={sameRun(chatKey(chat[i - 1]), chatKey(m))}>
|
|
{m.text}
|
|
</ChatMessage>
|
|
) : (
|
|
<div key={m.id} className="chat-line sys">
|
|
<strong className="sr-only">알림</strong>
|
|
{m.text}
|
|
</div>
|
|
),
|
|
)}
|
|
</div>
|
|
<BotActivity room={room} only="typing" ch="room" />
|
|
<div className="emote-row">
|
|
{EMOTES.map((id) => (
|
|
<button key={id} type="button" onClick={() => socket.send({ t: 'emote', id })}>
|
|
{EMOTE_LABEL[id]}
|
|
</button>
|
|
))}
|
|
</div>
|
|
{room.chatEnabled ? (
|
|
<form
|
|
className="row"
|
|
style={{ flexWrap: 'nowrap' }}
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
if (text.trim()) send(text.trim().slice(0, CHAT_MAX));
|
|
setText('');
|
|
typing.stop();
|
|
pin();
|
|
}}
|
|
>
|
|
<input
|
|
ref={inputRef}
|
|
className="input"
|
|
style={{ flex: 1, minHeight: 44 }}
|
|
value={text}
|
|
onChange={(e) => {
|
|
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"
|
|
/>
|
|
<button className="btn btn-primary btn-sm">보내기</button>
|
|
</form>
|
|
) : (
|
|
<p className="muted small">이 방은 채팅을 쓰지 않아요. 반응 버튼은 쓸 수 있어요.</p>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<div style={{ position: 'fixed', left: '50%', transform: 'translateX(-50%)', bottom: 24, zIndex: 35, display: 'flex', flexDirection: 'column', gap: 6, alignItems: 'center', pointerEvents: 'none' }}>
|
|
{emotes.map((e) => (
|
|
<div key={e.id} className="toast">
|
|
{nick(e.from)}: {EMOTE_LABEL[e.emote as keyof typeof EMOTE_LABEL] ?? ''}
|
|
</div>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|