"use client"; import { useEffect, useRef, useState } from "react"; import { getSocket } from "../lib/socket-client"; import { useGame } from "../lib/store"; export default function ChatBox({ placeholder = "Type your guess..." }: { placeholder?: string }) { const chat = useGame((s) => s.chat); const myId = useGame((s) => s.myId); const room = useGame((s) => s.room); const [text, setText] = useState(""); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [chat.length]); const send = (e: React.FormEvent) => { e.preventDefault(); const t = text.trim(); if (!t) return; getSocket().emit("chat:send", { text: t }); setText(""); }; return (
{chat.map((m) => { const me = m.fromId && m.fromId === myId; if (m.kind === "system") { return
{m.text}
; } if (m.kind === "correct") { return
{m.fromName} {m.text}
; } if (m.kind === "close") { return
{m.text}
; } return
{m.fromName}:{m.text}
; })}
setText(e.target.value)} maxLength={200} className="input-text" style={{borderRadius:9999}} placeholder={placeholder}/>
); }