Files
tolaria/src/hooks/useSaveNote.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
945 B
TypeScript

import { useCallback } from 'react'
import { invoke } from '@tauri-apps/api/core'
import { isTauri, mockInvoke, updateMockContent } from '../mock-tauri'
export async function persistContent(path: string, content: string): Promise<void> {
if (isTauri()) {
await invoke('save_note_content', { path, content })
} else {
await mockInvoke('save_note_content', { path, content })
}
}
/**
* Hook that provides an explicit save function for note content.
* Called on Cmd+S — no debounce, no auto-save.
*
* @param updateContent - callback to also update in-memory state after save
*/
export function useSaveNote(updateContent: (path: string, content: string) => void) {
const saveNote = useCallback(async (path: string, content: string) => {
await persistContent(path, content)
if (!isTauri()) {
updateMockContent(path, content)
}
updateContent(path, content)
}, [updateContent])
return { saveNote }
}