diff --git a/src/App.tsx b/src/App.tsx index fc9dc1a5..14be45a5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -541,8 +541,7 @@ function App() { removeEntry: vault.removeEntry, entries: vault.entries, flushBeforeNoteSwitch: flushEditorStateBeforeAction, - flushBeforeFrontmatterChange: flushEditorStateBeforeAction, - flushBeforePathRename: flushEditorStateBeforeAction, + flushBeforeNoteMutation: flushEditorStateBeforeAction, reloadVault: vault.reloadVault, setToastMessage, updateEntry: vault.updateEntry, diff --git a/src/hooks/useNoteActions.persistence.test.ts b/src/hooks/useNoteActions.persistence.test.ts index 4743efd2..1f481ed9 100644 --- a/src/hooks/useNoteActions.persistence.test.ts +++ b/src/hooks/useNoteActions.persistence.test.ts @@ -70,16 +70,16 @@ describe('useNoteActions frontmatter persistence', () => { }, }, ])('flushes pending raw content before a frontmatter $label', async ({ run }) => { - const flushBeforeFrontmatterChange = vi.fn().mockResolvedValue(undefined) + const flushBeforeNoteMutation = vi.fn().mockResolvedValue(undefined) const { result } = renderHook(() => useNoteActions({ ...makeConfig(vi.fn()), - flushBeforeFrontmatterChange, + flushBeforeNoteMutation, })) await act(async () => { await run(result) }) - expect(flushBeforeFrontmatterChange).toHaveBeenCalledWith('/vault/note.md') + expect(flushBeforeNoteMutation).toHaveBeenCalledWith('/vault/note.md') }) }) diff --git a/src/hooks/useNoteActions.rename-guard.test.ts b/src/hooks/useNoteActions.rename-guard.test.ts index dceb5acd..4b2479d6 100644 --- a/src/hooks/useNoteActions.rename-guard.test.ts +++ b/src/hooks/useNoteActions.rename-guard.test.ts @@ -77,7 +77,7 @@ describe('useNoteActions title rename guard', () => { it('flushes pending editor work before renaming through the title property', async () => { const entry = makeEntry() const events: string[] = [] - const flushBeforePathRename = vi.fn(async (path: string) => { + const flushBeforeNoteMutation = vi.fn(async (path: string) => { events.push(`flush:${path}`) }) @@ -90,7 +90,7 @@ describe('useNoteActions title rename guard', () => { return '' }) - const { result } = renderHook(() => useNoteActions(makeConfig(entry, { flushBeforePathRename }))) + const { result } = renderHook(() => useNoteActions(makeConfig(entry, { flushBeforeNoteMutation }))) await act(async () => { result.current.handleSelectNote(entry) @@ -100,17 +100,18 @@ describe('useNoteActions title rename guard', () => { await result.current.handleUpdateFrontmatter(entry.path, 'title', 'New Name') }) - expect(flushBeforePathRename).toHaveBeenCalledWith(entry.path) + expect(flushBeforeNoteMutation).toHaveBeenCalledTimes(1) + expect(flushBeforeNoteMutation).toHaveBeenCalledWith(entry.path) expect(events).toEqual([`flush:${entry.path}`, 'rename']) }) it('stops the title rename flow when pending editor work fails to flush', async () => { const entry = makeEntry() - const flushBeforePathRename = vi.fn().mockRejectedValue(new Error('disk full')) + const flushBeforeNoteMutation = vi.fn().mockRejectedValue(new Error('disk full')) const updateEntry = vi.fn() const { result } = renderHook(() => useNoteActions(makeConfig(entry, { - flushBeforePathRename, + flushBeforeNoteMutation, updateEntry, }))) diff --git a/src/hooks/useNoteActions.ts b/src/hooks/useNoteActions.ts index fa0833fa..41d8cd8c 100644 --- a/src/hooks/useNoteActions.ts +++ b/src/hooks/useNoteActions.ts @@ -15,8 +15,7 @@ export interface NoteActionsConfig { removeEntry: (path: string) => void entries: VaultEntry[] flushBeforeNoteSwitch?: (path: string) => Promise - flushBeforeFrontmatterChange?: (path: string) => Promise - flushBeforePathRename?: (path: string) => Promise + flushBeforeNoteMutation?: (path: string) => Promise reloadVault?: () => Promise setToastMessage: (msg: string | null) => void updateEntry: (path: string, patch: Partial) => void @@ -127,30 +126,14 @@ interface MaybeRenameAfterFrontmatterUpdateParams { deps: TitleRenameDeps } -async function flushBeforeTitleRename( +async function flushBeforeNoteMutation( path: string, - key: string, - value: FrontmatterValue, - flushBeforePathRename?: (path: string) => Promise, + flushBeforeMutation?: (path: string) => Promise, ): Promise { - if (!shouldRenameOnTitleUpdate(key, value) || !flushBeforePathRename) return true + if (!flushBeforeMutation) return true try { - await flushBeforePathRename(path) - return true - } catch { - return false - } -} - -async function flushBeforeFrontmatterMutation( - path: string, - flushBeforeFrontmatterChange?: (path: string) => Promise, -): Promise { - if (!flushBeforeFrontmatterChange) return true - - try { - await flushBeforeFrontmatterChange(path) + await flushBeforeMutation(path) return true } catch { return false @@ -196,12 +179,9 @@ async function updateFrontmatterAndMaybeRename({ runFrontmatterOp, value, }: UpdateFrontmatterAndMaybeRenameParams): Promise { - const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange) + const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation) if (!canFlush) return - const canRename = await flushBeforeTitleRename(path, key, value, config.flushBeforePathRename) - if (!canRename) return - const newContent = await runFrontmatterOp('update', path, key, value, options) if (!applyFrontmatterCallbacks({ config, path, newContent })) return @@ -289,7 +269,7 @@ function useFrontmatterActionHandlers({ }, [activeTabPathRef, config, handleSwitchTab, renameTabsRef, runFrontmatterOp, setTabs, setToastMessage, updateTabContent]) const handleDeleteProperty = useCallback(async (path: string, key: string, options?: FrontmatterOpOptions) => { - const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange) + const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation) if (!canFlush) return const newContent = await runFrontmatterOp('delete', path, key, undefined, options) @@ -298,7 +278,7 @@ function useFrontmatterActionHandlers({ }, [config, runFrontmatterOp]) const handleAddProperty = useCallback(async (path: string, key: string, value: FrontmatterValue) => { - const canFlush = await flushBeforeFrontmatterMutation(path, config.flushBeforeFrontmatterChange) + const canFlush = await flushBeforeNoteMutation(path, config.flushBeforeNoteMutation) if (!canFlush) return const newContent = await runFrontmatterOp('update', path, key, value)