import { AgentMessage } from "@vertesia/common"; import React, { useEffect } from "react"; import StackedMessages from "./StackedMessages"; export interface MessagesContainerProps { messages: AgentMessage[]; isCompleted: boolean; bottomRef: React.RefObject; /** Additional className for the outer container */ className?: string; /** Additional className for the messages wrapper */ messagesClassName?: string; /** Additional className for the empty state */ emptyClassName?: string; } export default function MessagesContainer({ messages, bottomRef, className, messagesClassName, emptyClassName, }: MessagesContainerProps) { const containerRef = React.useRef(null); // Auto-scroll to bottom when messages change useEffect(() => { if (bottomRef.current) { bottomRef.current.scrollIntoView({ behavior: "smooth" }); } }, [messages, bottomRef]); // Sort all messages chronologically const sortedMessages = React.useMemo( () => [...messages].sort((a, b) => { const timeA = typeof a.timestamp === "number" ? a.timestamp : new Date(a.timestamp).getTime(); const timeB = typeof b.timestamp === "number" ? b.timestamp : new Date(b.timestamp).getTime(); return timeA - timeB; }), [messages] ); return (
{sortedMessages.length === 0 ? (
Waiting for agent response...
) : (
)}
); }