From 701ef0e6e1d642dc58a96de4e4fa56f98b980d55 Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Tue, 14 Feb 2023 15:37:18 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=E9=83=A8=E4=BB=BD?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/business/Chat/hooks/useChat.ts | 9 ---- .../business/Chat/layout/sider/List.vue | 6 +-- src/store/modules/history/index.ts | 43 ++++++++++++------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/components/business/Chat/hooks/useChat.ts b/src/components/business/Chat/hooks/useChat.ts index f6ebd50..270fe5e 100644 --- a/src/components/business/Chat/hooks/useChat.ts +++ b/src/components/business/Chat/hooks/useChat.ts @@ -4,15 +4,6 @@ export function useChat() { const historyStore = useHistoryStore() function addChat(message: string, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }) { - if (historyStore.historyChat.length === 0) { - historyStore.addHistory({ - title: message, - isEdit: false, - data: [], - }) - historyStore.chooseHistory(historyStore.historyChat.length - 1) - } - historyStore.addChat({ dateTime: new Date().toLocaleString(), message, diff --git a/src/components/business/Chat/layout/sider/List.vue b/src/components/business/Chat/layout/sider/List.vue index 5e55079..1a4e797 100644 --- a/src/components/business/Chat/layout/sider/List.vue +++ b/src/components/business/Chat/layout/sider/List.vue @@ -34,14 +34,12 @@ function handleEnter(index: number, isEdit: boolean, event: KeyboardEvent) { class="relative flex items-center gap-3 px-3 py-3 break-all rounded-md cursor-pointer bg-neutral-50 pr-14 hover:bg-neutral-100 group" @click="handleSelect(index)" > - +
{{ item.title }} diff --git a/src/store/modules/history/index.ts b/src/store/modules/history/index.ts index 5b6df0f..24ea21b 100644 --- a/src/store/modules/history/index.ts +++ b/src/store/modules/history/index.ts @@ -6,22 +6,27 @@ export const useHistoryStore = defineStore('history-store', { state: (): HistoryState => getLocalHistory(), getters: { getCurrentChat(state): Chat.Chat[] { - if (state.historyChat.length === 0) - return [] - - if (state.active === null) - state.active = state.historyChat.length - 1 - - return state.historyChat[state.active].data + if (state.historyChat.length) { + if (state.active === null || state.active >= state.historyChat.length || state.active < 0) + state.active = 0 + return state.historyChat[state.active].data ?? [] + } + state.active = null + return [] }, }, actions: { addChat(data: Chat.Chat) { - if (this.active !== null) { - this.historyChat[this.active].data.push(data) + if (this.active === null) { + this.historyChat.push({ title: data.message, isEdit: false, data: [data] }) this.active = this.historyChat.length - 1 - setLocalHistory(this.$state) } + else { + if (this.historyChat[this.active].title === '') + this.historyChat[this.active].title = data.message + this.historyChat[this.active].data.push(data) + } + setLocalHistory(this.$state) }, clearChat() { @@ -31,11 +36,6 @@ export const useHistoryStore = defineStore('history-store', { } }, - chooseHistory(index: number) { - this.active = index - setLocalHistory(this.$state) - }, - addHistory(data: Chat.HistoryChat) { this.historyChat.push(data) this.active = this.historyChat.length - 1 @@ -49,6 +49,19 @@ export const useHistoryStore = defineStore('history-store', { removeHistory(index: number) { this.historyChat.splice(index, 1) + if (this.active === index) { + if (this.historyChat.length === 0) + this.active = null + else if (this.active === this.historyChat.length) + this.active-- + else + this.active = 0 + } + setLocalHistory(this.$state) + }, + + chooseHistory(index: number) { + this.active = index setLocalHistory(this.$state) }, },