mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix(web): restore list ordering after editing a new memo (#6451)
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<SetStateAction<string | null>>;
|
||||
}
|
||||
|
||||
// Default is a safe no-op so MemoEditor/MemoView rendered outside a provider
|
||||
|
||||
@@ -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) => <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
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) => <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
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) => <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
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) => <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
|
||||
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 } } });
|
||||
|
||||
Reference in New Issue
Block a user