import React, { useEffect, useRef } from "react"; import type { ChatMessage } from "../../../shared/types"; import type { ChatThread } from "./types"; import { useI18n } from "../i18n"; function formatTime(value: number) { return new Date(value).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); } export default function ChatThread({ thread, messages, meId, onSend, readOnly, nameById, }: { thread: ChatThread; messages: ChatMessage[]; meId: string | null; onSend?: (body: string) => void; readOnly?: boolean; nameById: Record; }) { const { t } = useI18n(); const listRef = useRef(null); useEffect(() => { if (listRef.current) { listRef.current.scrollTop = listRef.current.scrollHeight; } }, [messages]); const showSender = thread.kind === "group" || thread.kind === "global"; return (
{messages.length === 0 && (

{t("chat.noMessages")}

{t("chat.startConversation")}
)} {messages.map((message) => { const isMe = message.fromId === meId; return (
{showSender && !isMe && ( {nameById[message.fromId] ?? t("common.player")} )}

{message.body}

{formatTime(message.createdAt)}
); })}
{!readOnly && ( { if (!body.trim()) return; onSend?.(body); }} /> )}
); } function ChatComposer({ onSend, placeholder, sendLabel, }: { onSend: (body: string) => void; placeholder: string; sendLabel: string; }) { const [value, setValue] = React.useState(""); return (
setValue(event.target.value)} placeholder={placeholder} onKeyDown={(event) => { if (event.key === "Enter") { event.preventDefault(); if (!value.trim()) return; onSend(value); setValue(""); } }} />
); }