Browse Source

fix: updating the array using push in zustand does not actually trigger component updates

Jiacheng Dong 1 year ago
parent
commit
e636d486f5
1 changed files with 9 additions and 4 deletions
  1. 9 4
      app/store/chat.ts

+ 9 - 4
app/store/chat.ts

@@ -227,6 +227,7 @@ export const useChatStore = create<ChatStore>()(
 
       onNewMessage(message) {
         get().updateCurrentSession((session) => {
+          session.messages = session.messages.concat();
           session.lastUpdate = Date.now();
         });
         get().updateStat(message);
@@ -273,8 +274,9 @@ export const useChatStore = create<ChatStore>()(
 
         // save user's and bot's message
         get().updateCurrentSession((session) => {
-          session.messages.push(userMessage);
-          session.messages.push(botMessage);
+          // session.messages.push(userMessage);
+          // session.messages.push(botMessage);
+          session.messages = session.messages.concat([userMessage, botMessage]);
         });
 
         // make request
@@ -287,7 +289,10 @@ export const useChatStore = create<ChatStore>()(
             if (message) {
               botMessage.content = message;
             }
-            set(() => ({}));
+            // set(() => ({}));
+            get().updateCurrentSession((session) => {
+              session.messages = session.messages.concat();
+            });
           },
           onFinish(message) {
             botMessage.streaming = false;
@@ -299,7 +304,7 @@ export const useChatStore = create<ChatStore>()(
               sessionIndex,
               botMessage.id ?? messageIndex,
             );
-            set(() => ({}));
+            // set(() => ({}));
           },
           onError(error) {
             const isAborted = error.message.includes("aborted");