feat: 多会话基础逻辑梳理

This commit is contained in:
ChenZhaoYu
2023-02-14 15:07:50 +08:00
parent 33c02cfe10
commit de34af8747
11 changed files with 213 additions and 107 deletions

View File

@@ -1,4 +1,6 @@
import { ls } from '@/utils/storage'
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'appSetting'
export interface AppState {
siderCollapsed: boolean
@@ -8,11 +10,11 @@ export function defaultSetting() {
return { siderCollapsed: false }
}
export function getAppSetting() {
const localSetting: AppState = ls.get('appSetting')
export function getLocalSetting() {
const localSetting: AppState | undefined = ss.get(LOCAL_NAME)
return localSetting ?? defaultSetting()
}
export function setAppSetting(setting: AppState) {
ls.set('appSetting', setting)
export function setLocalSetting(setting: AppState) {
ss.set(LOCAL_NAME, setting)
}

View File

@@ -1,13 +1,13 @@
import { defineStore } from 'pinia'
import type { AppState } from './helper'
import { getAppSetting, setAppSetting } from './helper'
import { getLocalSetting, setLocalSetting } from './helper'
export const useAppStore = defineStore('app-store', {
state: (): AppState => getAppSetting(),
state: (): AppState => getLocalSetting(),
actions: {
setSiderCollapsed(collapsed: boolean) {
this.siderCollapsed = collapsed
setAppSetting(this.$state)
setLocalSetting(this.$state)
},
toggleSiderCollapse() {
this.setSiderCollapsed(!this.siderCollapsed)

View File

@@ -0,0 +1,21 @@
import { ss } from '@/utils/storage'
const LOCAL_NAME = 'historyChat'
export interface HistoryState {
historyChat: Chat.HistoryChat[]
active: number | null
}
export function defaultSetting() {
return { historyChat: [], active: null }
}
export function getLocalHistory() {
const localSetting: HistoryState | undefined = ss.get(LOCAL_NAME)
return localSetting ?? defaultSetting()
}
export function setLocalHistory(data: HistoryState) {
ss.set(LOCAL_NAME, data)
}

View File

@@ -1,12 +1,55 @@
import { defineStore } from 'pinia'
interface HistoryState {
list: any[]
}
import type { HistoryState } from './helper'
import { getLocalHistory, setLocalHistory } from './helper'
export const useHistoryStore = defineStore('history-store', {
state: (): HistoryState => ({
list: [],
}),
actions: {},
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
},
},
actions: {
addChat(data: Chat.Chat) {
if (this.active !== null) {
this.historyChat[this.active].data.push(data)
this.active = this.historyChat.length - 1
setLocalHistory(this.$state)
}
},
clearChat() {
if (this.active !== null) {
this.historyChat[this.active].data = []
setLocalHistory(this.$state)
}
},
chooseHistory(index: number) {
this.active = index
setLocalHistory(this.$state)
},
addHistory(data: Chat.HistoryChat) {
this.historyChat.push(data)
this.active = this.historyChat.length - 1
setLocalHistory(this.$state)
},
editHistory(index: number, isEdit: boolean) {
this.historyChat[index].isEdit = isEdit
setLocalHistory(this.$state)
},
removeHistory(index: number) {
this.historyChat.splice(index, 1)
setLocalHistory(this.$state)
},
},
})