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

This commit is contained in:
Jiacheng Dong 2023-06-15 10:23:01 +08:00 committed by GitHub
parent 818cc545eb
commit e636d486f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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