import { For, Show, createEffect } from 'solid-js' import { useChatContext } from './chat' import { ChatMessage } from './chat-message' import type { JSX } from 'solid-js' import type { UIMessage } from '../types' /** @deprecated Use `createChatUI()` Messages instead. Deprecated in 0.8.0. Removed in 1.0.0. */ export interface ChatMessagesProps { /** Custom render function for each message */ children?: (message: UIMessage, index: number) => JSX.Element /** CSS class name */ class?: string /** Element to show when there are no messages */ emptyState?: JSX.Element /** Element to show while loading the first message */ loadingState?: JSX.Element /** Custom error renderer */ errorState?: (props: { error: Error reload: () => Promise }) => JSX.Element /** Auto-scroll to bottom on new messages */ autoScroll?: boolean } /** * @deprecated Use `createChatUI()` Messages instead. Deprecated in 0.8.0. Removed in 1.0.0. * * Messages container - renders all messages in the conversation * * @example * ```tsx * * {(message) => } * * ``` */ export function ChatMessages(props: ChatMessagesProps) { const { messages, isLoading, error, reload } = useChatContext() let containerRef: HTMLDivElement | undefined // Auto-scroll to bottom on new messages createEffect(() => { // Track messages to trigger effect on change messages() if ((props.autoScroll ?? true) && containerRef) { containerRef.scrollTop = containerRef.scrollHeight } }) return ( { const err = error() return err ? <>{props.errorState?.({ error: err, reload })} : null })()} > 0 || !props.loadingState} fallback={<>{props.loadingState}} > 0 || !props.emptyState} fallback={<>{props.emptyState}} >
{ containerRef = el }} class={props.class} data-chat-messages data-message-count={messages().length} > {(message, index) => props.children ? (
{props.children(message, index())}
) : ( ) }
) }