From d74e2c08c17ebb8e2069f9bb968f0aa271659f39 Mon Sep 17 00:00:00 2001 From: benya Date: Sun, 8 Mar 2026 02:53:32 +0300 Subject: [PATCH] feat(drafts): persist chat drafts in localStorage - load drafts from localStorage on startup - write drafts to localStorage on update/clear - keep per-chat draft restore across page reload --- web/src/store/chatStore.ts | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) 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) =>