diff --git a/web/helpers/atoms/ChatMessage.atom.ts b/web/helpers/atoms/ChatMessage.atom.ts index b0ec6c4936..7cdeb69463 100644 --- a/web/helpers/atoms/ChatMessage.atom.ts +++ b/web/helpers/atoms/ChatMessage.atom.ts @@ -19,11 +19,20 @@ const CHAT_MESSAGE_NAME = 'chatMessages' /** * Stores all chat messages for all threads */ -export const chatMessages = atomWithStorage>( - CHAT_MESSAGE_NAME, - {}, - undefined, - { getOnInit: true } +export const chatMessagesStorage = atomWithStorage< + Record +>(CHAT_MESSAGE_NAME, {}, undefined, { getOnInit: true }) + +export const cachedMessages = atom>() +/** + * Retrieve chat messages for all threads + */ +export const chatMessages = atom( + (get) => get(cachedMessages) ?? get(chatMessagesStorage), + (_get, set, newValue: Record) => { + set(cachedMessages, newValue) + ;(() => set(chatMessagesStorage, newValue))() + } ) /**