/* * @Author: ZhaoYing * @Date: 2025-12-10 16:46:17 * @Last Modified by: ZhaoYing * @Last Modified time: 2026-01-12 20:41:27 */ import { type FC, useRef, useEffect } from 'react' import clsx from 'clsx' import Markdown from '@/components/Markdown' import type { ChatContentProps } from './types' /** * 聊天内容显示组件 * 负责渲染聊天消息列表,支持不同角色的消息样式和自动滚动 */ const ChatContent: FC = ({ classNames, contentClassNames, data = [], streamLoading = false, empty, labelPosition = 'bottom', labelFormat, errorDesc }) => { // 滚动容器引用,用于控制自动滚动到底部 const scrollContainerRef = useRef<(HTMLDivElement | null)>(null) // 当数据变化时,自动滚动到底部显示最新消息 useEffect(() => { setTimeout(() => { if (scrollContainerRef.current) { scrollContainerRef.current.scrollTop = scrollContainerRef.current.scrollHeight; } }, 0); }, [data]) return (
{data.length === 0 ? empty // 显示空状态 : data.map((item, index) => (
{/* 流式加载时且内容为空则不显示 */} {streamLoading && item.content === '' ? null : <> {/* 顶部标签(如时间戳、用户名等) */} {labelPosition === 'top' &&
{labelFormat(item)}
} {/* 消息气泡框 */}
{/* 使用Markdown组件渲染消息内容 */}
{/* 底部标签(如时间戳、用户名等) */} {labelPosition === 'bottom' &&
{labelFormat(item)}
} }
)) }
) } export default ChatContent