Browse Source

fix: [Chat] Fixed an issue where sending a message would result in a type error when Chat is [] (#2411)

YyumeiZhang 1 year ago
parent
commit
1283a621e8
1 changed files with 12 additions and 13 deletions
  1. 12 13
      packages/semi-ui/chat/index.tsx

+ 12 - 13
packages/semi-ui/chat/index.tsx

@@ -191,7 +191,7 @@ class Chat extends BaseComponent<ChatProps, ChatState> {
         const { chats, hints } = nextProps;
         const newState = {} as any;
         if (chats !== prevState.chats) {
-            newState.chats = chats;
+            newState.chats = chats ?? [];
         }
         if (hints !== prevState.cacheHints) {
             newState.cacheHints = hints;
@@ -212,19 +212,18 @@ class Chat extends BaseComponent<ChatProps, ChatState> {
         const { wheelScroll } = this.state;
         let shouldScroll = false;
         if (newChats !== oldChats) {
-            const newLastChat = newChats[newChats.length - 1];
-            const oldLastChat = oldChats[oldChats.length - 1];
-            if (newChats.length > oldChats.length) {
-                if (newLastChat.id !== oldLastChat.id) {
+            if (Array.isArray(newChats) && Array.isArray(oldChats)) {
+                const newLastChat = newChats[newChats.length - 1];
+                const oldLastChat = oldChats[oldChats.length - 1];
+                if (newChats.length > oldChats.length) {
+                    if (oldChats.length === 0 || newLastChat.id !== oldLastChat.id) {
+                        shouldScroll = true;
+                    }
+                } else if (newChats.length === oldChats.length &&
+                    (newLastChat.status !== 'complete' || newLastChat.status !== oldLastChat.status)
+                ) {
                     shouldScroll = true;
                 }
-            } else if (newChats.length === oldChats.length &&
-        (
-            newLastChat.status !== 'complete' ||
-          newLastChat.status !== oldLastChat.status
-        )
-            ) {
-                shouldScroll = true;
             }
         }
         if (newHints !== cacheHints) {
@@ -283,7 +282,7 @@ class Chat extends BaseComponent<ChatProps, ChatState> {
         const lastChat = chats.length > 0 && chats[chats.length - 1];
         let disableSend = false;
         if (lastChat && showStopGenerate) {
-            const lastChatOnGoing = lastChat.status && [MESSAGE_STATUS.LOADING, MESSAGE_STATUS.INCOMPLETE].includes(lastChat.status);
+            const lastChatOnGoing = lastChat?.status && [MESSAGE_STATUS.LOADING, MESSAGE_STATUS.INCOMPLETE].includes(lastChat?.status);
             disableSend = lastChatOnGoing;
             showStopGenerate && (showStopGenerateFlag = lastChatOnGoing);
         }