import { useEffect, useRef } from "react"; import { Bot, Sparkles, User } from "lucide-react"; import type { UiMessage } from "./types"; import { MarkdownContent } from "./MarkdownContent"; import { MessageTraceability } from "./MessageTraceability"; import { PlotlyChartBlock } from "./PlotlyChartBlock"; import { cx, compactQuestions, formatDateTime } from "./utils"; export function MessageList({ messages, scrollTo, onScrollDone, businessQuestions = [], onSelectQuestion, }: { messages: UiMessage[]; scrollTo?: "bottom" | "last-ai" | null; onScrollDone?: () => void; businessQuestions?: string[]; onSelectQuestion?: (question: string) => void; }) { const containerRef = useRef(null); const lastAiRef = useRef(null); useEffect(() => { if (!scrollTo) return; if (scrollTo === "bottom") { containerRef.current?.scrollIntoView({ block: "end", behavior: "smooth" }); } else if (scrollTo === "last-ai" && lastAiRef.current) { lastAiRef.current.scrollIntoView({ block: "start", behavior: "smooth" }); } onScrollDone?.(); }, [scrollTo]); if (messages.length === 0) { const questions = compactQuestions(businessQuestions); return (

Start the conversation

{questions.length > 0 ? "Pick one of the business questions below, or type your own question in the chat box." : "Ask a focused question once the analysis context is ready."}

{questions.length > 0 && (
{questions.map((question) => ( ))}
)}
); } const lastAiIndex = messages.reduce((last, msg, i) => (msg.role === "ai" ? i : last), -1); return (
{messages.map((message, index) => { const isUser = message.role === "user"; const isLastAi = index === lastAiIndex; return (
{!isUser && (
)}
{isUser ? "You" : "AI Agent"} {formatDateTime(message.created_at)}
{isUser ?

{message.content}

: } {message.status === "streaming" && message.statusText &&

{message.statusText}

} {message.status === "error" &&

{message.statusText ?? "Stream failed"}

} {!isUser && message.charts && message.charts.length > 0 && (
{message.charts.map((chart) => (
{chart.title &&

{chart.title}

}
))}
)} {!isUser && ( )}
{isUser && (
)}
); })}
); }