fix(ChatWindow): 处理消息内容为null或undefined的情况
添加对消息内容为null或undefined的检查,防止解析时出错
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user