chore: 优化部份实现

main
ChenZhaoYu 2 years ago
parent de34af8747
commit 701ef0e6e1

@ -4,15 +4,6 @@ export function useChat() {
const historyStore = useHistoryStore() const historyStore = useHistoryStore()
function addChat(message: string, args?: { reversal?: boolean; error?: boolean; options?: Chat.ChatOptions }) { 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({ historyStore.addChat({
dateTime: new Date().toLocaleString(), dateTime: new Date().toLocaleString(),
message, message,

@ -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" 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)" @click="handleSelect(index)"
> >
<span> <span :class="[{ 'text-[#4b9e5f]': historyStore.active === index }]">
<SvgIcon icon="ri:message-3-line" /> <SvgIcon icon="ri:message-3-line" />
</span> </span>
<div class="relative flex-1 overflow-hidden break-all text-ellipsis whitespace-nowrap"> <div class="relative flex-1 overflow-hidden break-all text-ellipsis whitespace-nowrap">
<NInput <NInput
v-if="item.isEdit" v-if="item.isEdit" v-model:value="item.title" size="tiny"
v-model:value="item.title"
size="tiny"
@keypress="handleEnter(index, false, $event)" @keypress="handleEnter(index, false, $event)"
/> />
<span v-else>{{ item.title }}</span> <span v-else>{{ item.title }}</span>

@ -6,22 +6,27 @@ export const useHistoryStore = defineStore('history-store', {
state: (): HistoryState => getLocalHistory(), state: (): HistoryState => getLocalHistory(),
getters: { getters: {
getCurrentChat(state): Chat.Chat[] { getCurrentChat(state): Chat.Chat[] {
if (state.historyChat.length === 0) if (state.historyChat.length) {
return [] if (state.active === null || state.active >= state.historyChat.length || state.active < 0)
state.active = 0
if (state.active === null) return state.historyChat[state.active].data ?? []
state.active = state.historyChat.length - 1 }
state.active = null
return state.historyChat[state.active].data return []
}, },
}, },
actions: { actions: {
addChat(data: Chat.Chat) { addChat(data: Chat.Chat) {
if (this.active !== null) { if (this.active === null) {
this.historyChat[this.active].data.push(data) this.historyChat.push({ title: data.message, isEdit: false, data: [data] })
this.active = this.historyChat.length - 1 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() { clearChat() {
@ -31,11 +36,6 @@ export const useHistoryStore = defineStore('history-store', {
} }
}, },
chooseHistory(index: number) {
this.active = index
setLocalHistory(this.$state)
},
addHistory(data: Chat.HistoryChat) { addHistory(data: Chat.HistoryChat) {
this.historyChat.push(data) this.historyChat.push(data)
this.active = this.historyChat.length - 1 this.active = this.historyChat.length - 1
@ -49,6 +49,19 @@ export const useHistoryStore = defineStore('history-store', {
removeHistory(index: number) { removeHistory(index: number) {
this.historyChat.splice(index, 1) 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) setLocalHistory(this.$state)
}, },
}, },

Loading…
Cancel
Save