From 9a14ca2cb1539677a1e67056eb41e542f1b5b224 Mon Sep 17 00:00:00 2001 From: Jinmiao Luo <39730824+jinmiaoluo@users.noreply.github.com> Date: Fri, 28 Apr 2023 17:10:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20revert=20=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过 splice 函数来实现 revert sessions 数组的操作 --- app/store/chat.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/store/chat.ts b/app/store/chat.ts index ed9bbd670..07bfae676 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -212,7 +212,7 @@ export const useChatStore = create()( // 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()( { 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, })); },