Skip to content

Commit

Permalink
fix no sync database after delete chatsession
Browse files Browse the repository at this point in the history
  • Loading branch information
cyx2000 committed Apr 25, 2024
1 parent 3543b84 commit 486a070
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
10 changes: 10 additions & 0 deletions web/src/store/modules/chat/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ export function getLocalState(): Chat.ChatState {
export function setLocalState(state: Chat.ChatState) {
ss.set(LOCAL_NAME, state)
}

export function check_chat(chat: Chat.ChatState['chat'], need_length = true) {
const keys = Object.keys(chat)
const data: [Array<string>, number?] = [keys]
if (need_length) {
const keys_length = keys.length
data.push(keys_length)
}
return data
}
33 changes: 15 additions & 18 deletions web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineStore } from 'pinia'
import { v4 as uuidv4 } from 'uuid'

import { getLocalState, setLocalState } from './helper'
import { check_chat, getLocalState, setLocalState } from './helper'
import { router } from '@/router'
import {
clearSessionChatMessages,
Expand Down Expand Up @@ -123,23 +123,20 @@ export const useChatStore = defineStore('chat-store', {

if (index > 0 && index <= this.history.length) {
const uuid = this.history[index - 1].uuid
this.active = uuid
this.reloadRoute(uuid)
this.setActive(uuid)
return
}

if (index === 0) {
if (this.history.length > 0) {
const uuid = this.history[0].uuid
this.active = uuid
this.reloadRoute(uuid)
this.setActive(uuid)
}
}

if (index > this.history.length) {
const uuid = this.history[this.history.length - 1].uuid
this.active = uuid
this.reloadRoute(uuid)
this.setActive(uuid)
}
},

Expand All @@ -150,9 +147,9 @@ export const useChatStore = defineStore('chat-store', {
},

getChatByUuidAndIndex(uuid: string, index: number) {
const keys = Object.keys(this.chat)
const [keys, keys_length] = check_chat(this.chat)
if (!uuid) {
if (keys.length)
if (keys_length)
return this.chat[uuid][index]
return null
}
Expand All @@ -164,7 +161,7 @@ export const useChatStore = defineStore('chat-store', {

async addChatByUuid(uuid: string, chat: Chat.Message) {
const new_chat_text = t('chat.new')
const keys = Object.keys(this.chat)
const [keys] = check_chat(this.chat, false)
if (!uuid) {
if (this.history.length === 0) {
const uuid = uuidv4()
Expand Down Expand Up @@ -206,9 +203,9 @@ export const useChatStore = defineStore('chat-store', {

async updateChatByUuid(uuid: string, index: number, chat: Chat.Message) {
// TODO: sync with server
const keys = Object.keys(this.chat)
const [keys, keys_length] = check_chat(this.chat)
if (!uuid) {
if (keys.length) {
if (keys_length) {
this.chat[keys[0]][index] = chat
this.recordState()
}
Expand All @@ -227,9 +224,9 @@ export const useChatStore = defineStore('chat-store', {
index: number,
chat: Partial<Chat.Message>,
) {
const keys = Object.keys(this.chat)
const [keys, keys_length] = check_chat(this.chat)
if (!uuid) {
if (keys.length) {
if (keys_length) {
this.chat[keys[0]][index] = { ...this.chat[keys[0]][index], ...chat }
this.recordState()
}
Expand All @@ -247,9 +244,9 @@ export const useChatStore = defineStore('chat-store', {
},

async deleteChatByUuid(uuid: string, index: number) {
const keys = Object.keys(this.chat)
const [keys, keys_length] = check_chat(this.chat)
if (!uuid) {
if (keys.length) {
if (keys_length) {
const chatData = this.chat[keys[0]]
const chat = chatData[index]
chatData.splice(index, 1)
Expand All @@ -273,9 +270,9 @@ export const useChatStore = defineStore('chat-store', {

clearChatByUuid(uuid: string) {
// does this every happen?
const keys = Object.keys(this.chat)
const [keys, keys_length] = check_chat(this.chat)
if (!uuid) {
if (keys.length) {
if (keys_length) {
this.chat[keys[0]] = []
this.recordState()
}
Expand Down

0 comments on commit 486a070

Please sign in to comment.