diff --git a/app/store/chat.ts b/app/store/chat.ts index ca8625a5f..cc91515aa 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -89,6 +89,7 @@ interface ChatStore { newSession: (mask?: Mask) => void; deleteSession: (index?: number) => void; currentSession: () => ChatSession; + deletedSession: ChatSession | undefined; onNewMessage: (message: Message) => void; onUserInput: (content: string) => Promise; summarizeSession: () => void; @@ -142,7 +143,7 @@ export const useChatStore = create()( }; } - sessions.splice(index, 1); + const deletedSession = sessions.splice(index, 1)[0]; // Save the deleted session object if (nextIndex === index) { nextIndex -= 1; @@ -151,6 +152,7 @@ export const useChatStore = create()( return { currentSessionIndex: nextIndex, sessions, + deletedSession, }; }); }, @@ -198,7 +200,6 @@ export const useChatStore = create()( }, deleteSession(i?: number) { - const deletedSession = get().currentSession(); const index = i ?? get().currentSessionIndex; const isLastSession = get().sessions.length === 1; if (!isMobileScreen() || confirm(Locale.Home.DeleteChat)) { @@ -209,21 +210,26 @@ export const useChatStore = create()( { text: Locale.Home.Revert, onClick() { - set((state) => ({ - sessions: state.sessions - .slice(0, index) - .concat([deletedSession]) - .concat( - state.sessions.slice(index + Number(isLastSession)), - ), - })); + const deletedSession = get().deletedSession as ChatSession; + if (deletedSession) { + set((state) => ({ + sessions: state.sessions + .slice(0, index) + .concat([deletedSession]) + .concat( + state.sessions.slice(index + Number(isLastSession)), + ), + //Reset the deletedSession variable to undefined, it will not be revoked infinitely + deletedSession: undefined, + })); + } }, }, 5000, ); } }, - + deletedSession: undefined, currentSession() { let index = get().currentSessionIndex; const sessions = get().sessions;