fix: session delete undo

This commit is contained in:
wsw
2023-04-30 02:59:25 +08:00
parent b0cd8579f1
commit 31900a15a8

View File

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