Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AvailableCommand } from "@agentclientprotocol/sdk";
import { electronStorage } from "@renderer/lib/electronStorage";
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";
Expand Down Expand Up @@ -52,16 +53,19 @@ export const useDraftStore = create<DraftStore>()(
_hasHydrated: false,

actions: {
setHasHydrated: (hydrated) => set({ _hasHydrated: hydrated }),
setHasHydrated: (hydrated) => {
set({ _hasHydrated: hydrated });
},

setDraft: (sessionId, draft) =>
setDraft: (sessionId, draft) => {
set((state) => {
if (draft === null) {
delete state.drafts[sessionId];
} else {
state.drafts[sessionId] = draft;
}
}),
});
},

getDraft: (sessionId) => get().drafts[sessionId] ?? null,

Expand Down Expand Up @@ -110,6 +114,7 @@ export const useDraftStore = create<DraftStore>()(
})),
{
name: "message-editor-drafts",
storage: electronStorage,
partialize: (state) => ({ drafts: state.drafts }),
onRehydrateStorage: () => (state) => {
state?.actions.setHasHydrated(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,27 @@ export function useDraftSync(
context?: DraftContext,
) {
const hasRestoredRef = useRef(false);
const lastSessionIdRef = useRef(sessionId);
const lastEditorRef = useRef(editor);
const editorRef = useRef(editor);
editorRef.current = editor;

const draftActions = useDraftStore((s) => s.actions);
const draft = useDraftStore((s) => s.drafts[sessionId] ?? null);
const hasHydrated = useDraftStore((s) => s._hasHydrated);

// Reset restoration flag when sessionId changes (e.g., navigating between tasks)
if (lastSessionIdRef.current !== sessionId) {
lastSessionIdRef.current = sessionId;
hasRestoredRef.current = false;
}

// Reset restoration flag when editor instance changes (e.g., when disabled state changes)
if (lastEditorRef.current !== editor && editor !== null) {
lastEditorRef.current = editor;
hasRestoredRef.current = false;
}

// Set context for this session
useLayoutEffect(() => {
draftActions.setContext(sessionId, {
Expand All @@ -121,7 +135,7 @@ export function useDraftSync(
};
}, [sessionId, context?.taskId, context?.repoPath, draftActions]);

// Restore draft on mount
// Restore draft on mount or when sessionId/editor changes
useLayoutEffect(() => {
if (!hasHydrated || !editor || hasRestoredRef.current) return;
if (!draft || isContentEmpty(draft)) return;
Expand All @@ -137,14 +151,18 @@ export function useDraftSync(

const saveDraft = useCallback(
(e: Editor) => {
// Don't save until store has hydrated from storage
// This prevents overwriting stored drafts with empty content before restoration
if (!hasHydrated) return;

const json = e.getJSON();
const content = tiptapJsonToEditorContent(json);
draftActions.setDraft(
sessionId,
isContentEmpty(content) ? null : content,
);
},
[sessionId, draftActions],
[sessionId, draftActions, hasHydrated],
);

const clearDraft = useCallback(() => {
Expand Down