优化 revert 操作的实现

通过 splice 函数来实现 revert sessions 数组的操作
This commit is contained in:
Jinmiao Luo
2023-04-28 17:10:13 +08:00
parent c67f3ace6c
commit 9a14ca2cb1

View File

@@ -212,7 +212,7 @@ export const useChatStore = create<ChatStore>()(
// On the desktop, the deleted session index may not be the current session index
const seletedSessionIndex = get().currentSessionIndex;
const isLastSession = get().sessions.length === 1;
const isLastSession = get().sessions.length === 1 ? 1 : 0;
if (!isMobileScreen() || confirm(Locale.Home.DeleteChat)) {
get().removeSession(index);
@@ -221,13 +221,12 @@ 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: get()
.sessions.slice(0, index)
.concat([deletedSession])
.concat(
get().sessions.slice(index + Number(isLastSession)),
),
sessions: sessions,
currentSessionIndex: seletedSessionIndex,
}));
},