更新 revert 操作的实现逻辑

通过 ... 的语法创建一个新的 ChatSession[] 副本,在副本上执行 splice 操作,避免直接修改状态
This commit is contained in:
jinmiaoluo
2023-04-29 13:37:57 +08:00
committed by Jinmiao Luo
parent c7965f0e6a
commit 0910332f9a

View File

@@ -221,14 +221,16 @@ export const useChatStore = create<ChatStore>()(
{ {
text: Locale.Home.Revert, text: Locale.Home.Revert,
onClick() { onClick() {
const sessions = get().sessions; set((state) => {
// if the deleted session is the only session in sessions array, const newSessions = [...state.sessions];
// we should delete the empty session first before inserting the deleted session // if the deleted session is the only session in sessions array,
sessions.splice(index, isLastSession, deletedSession); // we should delete the empty session first before inserting the deleted session
set(() => ({ newSessions.splice(index, isLastSession, deletedSession);
sessions: sessions, return {
currentSessionIndex: seletedSessionIndex, sessions: newSessions,
})); currentSessionIndex: seletedSessionIndex,
};
});
}, },
}, },
5000, 5000,