更新 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,
onClick() {
const sessions = get().sessions;
// if the deleted session is the only session in sessions array,
// we should delete the empty session first before inserting the deleted session
sessions.splice(index, isLastSession, deletedSession);
set(() => ({
sessions: sessions,
currentSessionIndex: seletedSessionIndex,
}));
set((state) => {
const newSessions = [...state.sessions];
// if the deleted session is the only session in sessions array,
// we should delete the empty session first before inserting the deleted session
newSessions.splice(index, isLastSession, deletedSession);
return {
sessions: newSessions,
currentSessionIndex: seletedSessionIndex,
};
});
},
},
5000,