diff --git a/web/src/components/MemoEditor/hooks/useMemoSave.ts b/web/src/components/MemoEditor/hooks/useMemoSave.ts index 9aa3b8ebc..165a50d68 100644 --- a/web/src/components/MemoEditor/hooks/useMemoSave.ts +++ b/web/src/components/MemoEditor/hooks/useMemoSave.ts @@ -108,6 +108,8 @@ export function useMemoSave({ if (!memoName && !parentMemoName) { markNewMemo(result.memoName); + } else if (memoName) { + markNewMemo((currentName) => (currentName === memoName ? null : currentName)); } onConfirm?.(result.memoName); } catch (error) { diff --git a/web/src/contexts/NewMemoContext.tsx b/web/src/contexts/NewMemoContext.tsx index 79c5445a9..431634be7 100644 --- a/web/src/contexts/NewMemoContext.tsx +++ b/web/src/contexts/NewMemoContext.tsx @@ -1,11 +1,11 @@ -import { createContext, type ReactNode, useContext, useMemo, useState } from "react"; +import { createContext, type Dispatch, type ReactNode, type SetStateAction, useContext, useMemo, useState } from "react"; interface NewMemoContextValue { // Name of the most recently created memo in this view, or null. Only one // memo is ever "new" at a time: creating another overwrites it, which clears // the previous one for free. newMemoName: string | null; - markNewMemo: (name: string) => void; + markNewMemo: Dispatch>; } // Default is a safe no-op so MemoEditor/MemoView rendered outside a provider diff --git a/web/tests/use-memo-save.test.tsx b/web/tests/use-memo-save.test.tsx index 8bd148ee9..cb227bce7 100644 --- a/web/tests/use-memo-save.test.tsx +++ b/web/tests/use-memo-save.test.tsx @@ -8,6 +8,7 @@ const mocks = vi.hoisted(() => ({ dispatch: vi.fn(), markNewMemo: vi.fn(), memoSave: vi.fn(), + newMemoName: null as string | null, })); vi.mock("@/components/MemoEditor/services", () => ({ @@ -31,7 +32,7 @@ vi.mock("@/components/MemoEditor/state", () => ({ })); vi.mock("@/contexts/NewMemoContext", () => ({ - useNewMemo: () => ({ markNewMemo: mocks.markNewMemo }), + useNewMemo: () => ({ newMemoName: mocks.newMemoName, markNewMemo: mocks.markNewMemo }), })); vi.mock("@/utils/i18n", () => ({ @@ -43,6 +44,10 @@ describe("useMemoSave", () => { mocks.dispatch.mockReset(); mocks.markNewMemo.mockReset(); mocks.memoSave.mockReset(); + mocks.newMemoName = null; + mocks.markNewMemo.mockImplementation((value: string | null | ((current: string | null) => string | null)) => { + mocks.newMemoName = typeof value === "function" ? value(mocks.newMemoName) : value; + }); }); it("invalidates scoped attachment libraries after a memo save", async () => { @@ -90,6 +95,64 @@ describe("useMemoSave", () => { expect(onConfirm).toHaveBeenCalledWith("memos/existing"); }); + it("clears the new memo hoist after editing that memo", async () => { + mocks.newMemoName = "memos/new"; + mocks.memoSave.mockResolvedValue({ hasChanges: true, memoName: "memos/new" }); + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const wrapper = ({ children }: PropsWithChildren) => {children}; + const { result } = renderHook(() => useMemoSave({ memoName: "memos/new", discardDraft: vi.fn() }), { wrapper }); + + await act(async () => result.current()); + + expect(mocks.newMemoName).toBeNull(); + }); + + it("preserves the new memo hoist when editing another memo", async () => { + mocks.newMemoName = "memos/new"; + mocks.memoSave.mockResolvedValue({ hasChanges: true, memoName: "memos/existing" }); + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const wrapper = ({ children }: PropsWithChildren) => {children}; + const { result } = renderHook(() => useMemoSave({ memoName: "memos/existing", discardDraft: vi.fn() }), { wrapper }); + + await act(async () => result.current()); + + expect(mocks.newMemoName).toBe("memos/new"); + }); + + it("preserves a newer marker created while the edit save is pending", async () => { + mocks.newMemoName = "memos/new"; + let finishSave!: (value: { hasChanges: boolean; memoName: string }) => void; + mocks.memoSave.mockReturnValue( + new Promise((resolve) => { + finishSave = resolve; + }), + ); + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const wrapper = ({ children }: PropsWithChildren) => {children}; + const { result } = renderHook(() => useMemoSave({ memoName: "memos/new", discardDraft: vi.fn() }), { wrapper }); + + await act(async () => { + const save = result.current(); + mocks.newMemoName = "memos/newer"; + finishSave({ hasChanges: true, memoName: "memos/new" }); + await save; + }); + + expect(mocks.newMemoName).toBe("memos/newer"); + }); + + it("preserves the new memo hoist when the edit has no changes", async () => { + mocks.newMemoName = "memos/new"; + mocks.memoSave.mockResolvedValue({ hasChanges: false, memoName: "memos/new" }); + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }); + const wrapper = ({ children }: PropsWithChildren) => {children}; + const { result } = renderHook(() => useMemoSave({ memoName: "memos/new", discardDraft: vi.fn() }), { wrapper }); + + await act(async () => result.current()); + + expect(mocks.markNewMemo).not.toHaveBeenCalled(); + }); + it("does not hold the in-place composer after saving a new memo", async () => { mocks.memoSave.mockResolvedValue({ hasChanges: true, memoName: "memos/new" }); const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });