diff --git a/web/src/store/chatStore.ts b/web/src/store/chatStore.ts index 057449e..ec2acca 100644 --- a/web/src/store/chatStore.ts +++ b/web/src/store/chatStore.ts @@ -2,6 +2,42 @@ import { create } from "zustand"; import { getChats, getMessages, updateMessageStatus } from "../api/chats"; import type { Chat, DeliveryStatus, Message, MessageType } from "../chat/types"; +const DRAFTS_STORAGE_KEY = "bm_drafts_v1"; + +function loadDraftsFromStorage(): Record { + if (typeof window === "undefined") { + return {}; + } + try { + const raw = window.localStorage.getItem(DRAFTS_STORAGE_KEY); + if (!raw) { + return {}; + } + const parsed = JSON.parse(raw) as Record; + const result: Record = {}; + for (const [key, value] of Object.entries(parsed)) { + const chatId = Number(key); + if (Number.isFinite(chatId) && typeof value === "string") { + result[chatId] = value; + } + } + return result; + } catch { + return {}; + } +} + +function saveDraftsToStorage(drafts: Record): void { + if (typeof window === "undefined") { + return; + } + try { + window.localStorage.setItem(DRAFTS_STORAGE_KEY, JSON.stringify(drafts)); + } catch { + return; + } +} + interface ChatState { chats: Chat[]; activeChatId: number | null; @@ -52,7 +88,7 @@ export const useChatStore = create((set, get) => ({ chats: [], activeChatId: null, messagesByChat: {}, - draftsByChat: {}, + draftsByChat: loadDraftsFromStorage(), hasMoreByChat: {}, loadingMoreByChat: {}, typingByChat: {}, @@ -331,12 +367,14 @@ export const useChatStore = create((set, get) => ({ }) })), setDraft: (chatId, text) => - set((state) => ({ - draftsByChat: { + set((state) => { + const nextDrafts = { ...state.draftsByChat, [chatId]: text - } - })), + }; + saveDraftsToStorage(nextDrafts); + return { draftsByChat: nextDrafts }; + }), clearDraft: (chatId) => set((state) => { if (!(chatId in state.draftsByChat)) { @@ -344,6 +382,7 @@ export const useChatStore = create((set, get) => ({ } const next = { ...state.draftsByChat }; delete next[chatId]; + saveDraftsToStorage(next); return { draftsByChat: next }; }), setFocusedMessage: (chatId, messageId) =>