import React, { useRef } from 'react'; import { Box, Paper, Typography, Avatar, useMediaQuery, useTheme } from '@mui/material'; import ReactMarkdown from 'react-markdown'; import rehypeRaw from 'rehype-raw'; import remarkGfm from 'remark-gfm'; import type { Message as MessageType } from '../types/types'; interface MessageProps { message: MessageType; isLast?: boolean; } const Message: React.FC = ({ message, isLast = false }) => { const isUser = message.sender === 'user'; const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down('sm')); const paperRef = useRef(null); // 计算气泡的borderRadius const borderRadius = isUser ? '18px 18px 4px 18px' : '18px 18px 18px 4px'; return ( {!isUser && ( 🤖 )}
{isUser ? ( // 用户消息仍然使用普通文本展示 message.content.split('\n').map((line: string, index: number) => ( {line} {index < message.content.split('\n').length - 1 &&
}
)) ) : ( // 大模型回复使用markdown格式展示 (

), h2: (props: any) => (

), h3: (props: any) => (

), // 自定义列表样式 ul: (props: any) => (
    ), ol: (props: any) => (
      ), li: (props: any) => (
    1. ), // 自定义代码块样式 // 简化代码块处理,移除对 node 对象内部属性的访问 code: (props: any) => ( ), pre: (props: any) => (
                        ),
                        // 自定义引用样式
                        blockquote: (props: any) => (
                          
      ), // 自定义链接样式 a: (props: any) => ( ) }} > {message.content} )}

{message.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
{isUser && ( 👤 )}
); }; export default Message;