fix(ChatWindow): 处理消息内容为null或undefined的情况

添加对消息内容为null或undefined的检查,防止解析时出错
This commit is contained in:
超级老白兔
2025-09-04 11:58:36 +08:00
parent 2d78c2a37a
commit 923f0e99b9

View File

@@ -48,6 +48,9 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
if (!prevMsg || prevMsg.id !== msg.id) return false;
try {
// 检查msg.content和prevMsg.content是否为null或undefined
if (!msg.content || !prevMsg.content) return false;
const currentContent =
typeof msg.content === "string"
? JSON.parse(msg.content)
@@ -107,7 +110,14 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
};
// 解析消息内容,判断消息类型并返回对应的渲染内容
const parseMessageContent = (content: string, msg: ChatRecord) => {
const parseMessageContent = (
content: string | null | undefined,
msg: ChatRecord,
) => {
// 处理null或undefined的内容
if (content === null || content === undefined) {
return <div className={styles.messageText}></div>;
}
// 检查是否为表情包
if (
typeof content === "string" &&
@@ -530,7 +540,7 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
};
const renderMessage = (msg: ChatRecord) => {
const isOwn = msg.isSend;
const isOwn = msg?.isSend;
return (
<div
key={msg.id}
@@ -549,7 +559,7 @@ const ChatWindow: React.FC<ChatWindowProps> = ({
)}
<div className={styles.messageBubble}>
{!isOwn && (
<div className={styles.messageSender}>{msg.senderName}</div>
<div className={styles.messageSender}>{msg?.senderName}</div>
)}
{parseMessageContent(msg.content, msg)}
</div>