fix: avoid persisting threads and messages on local storage (#5249)

This commit is contained in:
Louis 2025-06-12 09:10:00 +07:00 committed by GitHub
parent 3accef8c92
commit 079f206044
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 306 additions and 328 deletions

View File

@ -1,7 +1,5 @@
import { create } from 'zustand'
import { ThreadMessage } from '@janhq/core'
import { createJSONStorage, persist } from 'zustand/middleware'
import { localStorageKey } from '@/constants/localStorage'
import {
createMessage,
deleteMessage as deleteMessageExt,
@ -16,9 +14,7 @@ type MessageState = {
deleteMessage: (threadId: string, messageId: string) => void
}
export const useMessages = create<MessageState>()(
persist(
(set, get) => ({
export const useMessages = create<MessageState>()((set, get) => ({
messages: {},
getMessages: (threadId) => {
return get().messages[threadId] || []
@ -65,10 +61,4 @@ export const useMessages = create<MessageState>()(
},
}))
},
}),
{
name: localStorageKey.messages,
storage: createJSONStorage(() => localStorage),
}
)
)
}))

View File

@ -1,6 +1,4 @@
import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import { localStorageKey } from '@/constants/localStorage'
import { ulid } from 'ulidx'
import { createThread, deleteThread, updateThread } from '@/services/threads'
import { Fzf } from 'fzf'
@ -30,9 +28,7 @@ type ThreadState = {
searchIndex: Fzf<Thread[]> | null
}
export const useThreads = create<ThreadState>()(
persist(
(set, get) => ({
export const useThreads = create<ThreadState>()((set, get) => ({
threads: {},
searchIndex: null,
setThreads: (threads) => {
@ -150,9 +146,7 @@ export const useThreads = create<ThreadState>()(
})
},
getFavoriteThreads: () => {
return Object.values(get().threads).filter(
(thread) => thread.isFavorite
)
return Object.values(get().threads).filter((thread) => thread.isFavorite)
},
getThreadById: (threadId: string) => {
return get().threads[threadId]
@ -275,7 +269,7 @@ export const useThreads = create<ThreadState>()(
// Update all other threads to increment their order by 1
const updatedThreads = { ...state.threads }
Object.keys(updatedThreads).forEach(id => {
Object.keys(updatedThreads).forEach((id) => {
if (id !== threadId) {
const otherThread = updatedThreads[id]
updatedThreads[id] = {
@ -301,10 +295,4 @@ export const useThreads = create<ThreadState>()(
}
})
},
}),
{
name: localStorageKey.threads,
storage: createJSONStorage(() => localStorage),
}
)
)
}))