Files
tolaria/src/utils/autoSave.ts
lucaronin 12fe4978ab fix: auto-save unsaved notes before trash/archive
Flush unsaved editor content to disk before any trash or archive
operation (both single-note and bulk) so body edits are never silently
dropped when only frontmatter is updated.

- Add flushEditorContent utility that checks pending content ref, then
  falls back to comparing tab content with last-saved state
- Add onBeforeAction callback to useEntryActions, called before
  handleTrashNote and handleArchiveNote
- Wire flushBeforeAction in App.tsx using refs for stable closures
- Add error handling in useBulkActions so one failed save doesn't
  block remaining notes
- Extract findOrCreateType helper to reduce useEntryActions complexity
- Export persistContent from useSaveNote for reuse

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-08 16:52:08 +01:00

30 lines
1.1 KiB
TypeScript

import { persistContent } from '../hooks/useSaveNote'
export interface FlushDeps {
savePendingForPath: (path: string) => Promise<boolean>
getTabContent: (path: string) => string | undefined
isUnsaved: (path: string) => boolean
getSavedContent: (path: string) => string | undefined
onSaved?: (path: string, content: string) => void
}
/**
* Flush unsaved editor content to disk for a given path before a destructive action.
*
* 1. Try flushing the pending content ref (handles the currently-editing note).
* 2. If nothing was pending, check if the tab has unsaved content (either newly
* created or modified in the editor) and persist it directly.
*/
export async function flushEditorContent(path: string, deps: FlushDeps): Promise<void> {
const flushed = await deps.savePendingForPath(path)
if (flushed) return
const tabContent = deps.getTabContent(path)
if (tabContent === undefined) return
if (deps.isUnsaved(path) || tabContent !== deps.getSavedContent(path)) {
await persistContent(path, tabContent)
deps.onSaved?.(path, tabContent)
}
}