2026-03-16 18:31:33 +01:00
|
|
|
import { useCallback } from 'react'
|
2026-03-17 16:52:23 +01:00
|
|
|
import { invoke } from '@tauri-apps/api/core'
|
|
|
|
|
import { isTauri } from '../mock-tauri'
|
2026-02-17 12:10:21 +01:00
|
|
|
import type { VaultEntry } from '../types'
|
|
|
|
|
import type { FrontmatterValue } from '../components/Inspector'
|
2026-03-18 02:37:29 +01:00
|
|
|
import { useTabManagement, syncNoteTitle } from './useTabManagement'
|
2026-03-11 19:12:05 +01:00
|
|
|
import { resolveEntry } from '../utils/wikilink'
|
2026-03-16 18:31:33 +01:00
|
|
|
import { useNoteCreation } from './useNoteCreation'
|
|
|
|
|
import {
|
|
|
|
|
useNoteRename,
|
|
|
|
|
performRename, loadNoteContent, renameToastMessage, reloadTabsAfterRename,
|
|
|
|
|
} from './useNoteRename'
|
2026-03-16 23:34:45 +01:00
|
|
|
import { runFrontmatterAndApply } from './frontmatterOps'
|
2026-02-21 19:22:44 +01:00
|
|
|
|
fix: sync in-memory entries after property/rename changes
Two propagation gaps fixed:
1. Property changes (type, status, color, etc.) now immediately sync to
the entries state via frontmatterToEntryPatch(), so sidebar, note list,
and relations panel reflect updates without restart.
2. After renaming a note, all other open tabs reload their content from
disk, picking up updated wikilinks immediately.
Design decisions:
- Used key-mapping approach (frontmatterToEntryPatch) instead of a new
parse_md_file Tauri command — simpler, works in both Tauri and mock
modes, covers all VaultEntry fields.
- Converted useNoteActions to config object to avoid excess arguments.
- Extracted findWikilinkTarget, reloadTabsAfterRename, renameToastMessage
as standalone functions to reduce hook complexity (cc 10→9).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 17:48:39 +01:00
|
|
|
export interface NoteActionsConfig {
|
2026-03-08 22:15:08 +01:00
|
|
|
addEntry: (entry: VaultEntry) => void
|
feat: add optimistic error recovery for note creation (#69)
Note creation was already optimistic (UI updates before disk write) but
errors were silently swallowed. Now if the disk write fails, the
optimistic entry is reverted (tab closed, entry removed) and the user
sees an error toast. Each note creation is independent, so one failure
in rapid Cmd+N presses doesn't affect the others.
- Add removeEntry to useVaultLoader for reverting optimistic adds
- Refactor persistNewNote to return a Promise for error handling
- Extract navigateWikilink, persistOptimistic, createAndPersist,
and runFrontmatterAndApply helpers to reduce hook complexity
- Add tests for error recovery: single note, type, success path,
and rapid creation with partial failure
- Code health: 8.93→9.01 (CC fixed: 10→7, primitive obsession improved)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 16:11:59 +01:00
|
|
|
removeEntry: (path: string) => void
|
fix: sync in-memory entries after property/rename changes
Two propagation gaps fixed:
1. Property changes (type, status, color, etc.) now immediately sync to
the entries state via frontmatterToEntryPatch(), so sidebar, note list,
and relations panel reflect updates without restart.
2. After renaming a note, all other open tabs reload their content from
disk, picking up updated wikilinks immediately.
Design decisions:
- Used key-mapping approach (frontmatterToEntryPatch) instead of a new
parse_md_file Tauri command — simpler, works in both Tauri and mock
modes, covers all VaultEntry fields.
- Converted useNoteActions to config object to avoid excess arguments.
- Extracted findWikilinkTarget, reloadTabsAfterRename, renameToastMessage
as standalone functions to reduce hook complexity (cc 10→9).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 17:48:39 +01:00
|
|
|
entries: VaultEntry[]
|
|
|
|
|
setToastMessage: (msg: string | null) => void
|
|
|
|
|
updateEntry: (path: string, patch: Partial<VaultEntry>) => void
|
2026-03-08 20:00:20 +01:00
|
|
|
vaultPath: string
|
2026-02-27 14:11:31 +01:00
|
|
|
addPendingSave?: (path: string) => void
|
|
|
|
|
removePendingSave?: (path: string) => void
|
2026-02-27 18:17:47 +01:00
|
|
|
trackUnsaved?: (path: string) => void
|
|
|
|
|
clearUnsaved?: (path: string) => void
|
|
|
|
|
unsavedPaths?: Set<string>
|
|
|
|
|
markContentPending?: (path: string, content: string) => void
|
2026-03-02 03:07:02 +01:00
|
|
|
onNewNotePersisted?: () => void
|
2026-03-08 22:15:08 +01:00
|
|
|
replaceEntry?: (oldPath: string, patch: Partial<VaultEntry> & { path: string }) => void
|
2026-03-08 21:37:27 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 18:44:44 +01:00
|
|
|
function isTitleKey(key: string): boolean {
|
|
|
|
|
return key.toLowerCase().replace(/\s+/g, '_') === 'title'
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 18:31:33 +01:00
|
|
|
interface TitleRenameDeps {
|
|
|
|
|
vaultPath: string
|
|
|
|
|
tabsRef: React.MutableRefObject<{ entry: VaultEntry; content: string }[]>
|
|
|
|
|
replaceEntry?: (oldPath: string, patch: Partial<VaultEntry> & { path: string }) => void
|
|
|
|
|
setTabs: React.Dispatch<React.SetStateAction<{ entry: VaultEntry; content: string }[]>>
|
|
|
|
|
activeTabPathRef: React.MutableRefObject<string | null>
|
|
|
|
|
handleSwitchTab: (path: string) => void
|
|
|
|
|
setToastMessage: (msg: string | null) => void
|
|
|
|
|
updateTabContent: (path: string, content: string) => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function renameAfterTitleChange(path: string, newTitle: string, deps: TitleRenameDeps): Promise<void> {
|
|
|
|
|
const oldTitle = deps.tabsRef.current.find(t => t.entry.path === path)?.entry.title
|
|
|
|
|
const result = await performRename(path, newTitle, deps.vaultPath, oldTitle)
|
|
|
|
|
if (result.new_path !== path) {
|
|
|
|
|
const newFilename = result.new_path.split('/').pop() ?? ''
|
|
|
|
|
deps.replaceEntry?.(path, { path: result.new_path, filename: newFilename, title: newTitle } as Partial<VaultEntry> & { path: string })
|
|
|
|
|
const newContent = await loadNoteContent(result.new_path)
|
|
|
|
|
deps.setTabs(prev => prev.map(t => t.entry.path === path
|
|
|
|
|
? { entry: { ...t.entry, path: result.new_path, filename: newFilename, title: newTitle }, content: newContent }
|
|
|
|
|
: t))
|
|
|
|
|
if (deps.activeTabPathRef.current === path) deps.handleSwitchTab(result.new_path)
|
|
|
|
|
const otherTabPaths = deps.tabsRef.current.filter(t => t.entry.path !== path && t.entry.path !== result.new_path).map(t => t.entry.path)
|
|
|
|
|
await reloadTabsAfterRename(otherTabPaths, deps.updateTabContent)
|
|
|
|
|
}
|
|
|
|
|
deps.setToastMessage(renameToastMessage(result.updated_files))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldRenameOnTitleUpdate(key: string, value: FrontmatterValue): value is string {
|
|
|
|
|
return isTitleKey(key) && typeof value === 'string' && value !== ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function navigateWikilink(entries: VaultEntry[], target: string, selectNote: (e: VaultEntry) => void): void {
|
2026-03-16 23:34:45 +01:00
|
|
|
const found = resolveEntry(entries, target)
|
2026-03-16 18:31:33 +01:00
|
|
|
if (found) selectNote(found)
|
|
|
|
|
else console.warn(`Navigation target not found: ${target}`)
|
|
|
|
|
}
|
|
|
|
|
|
fix: sync in-memory entries after property/rename changes
Two propagation gaps fixed:
1. Property changes (type, status, color, etc.) now immediately sync to
the entries state via frontmatterToEntryPatch(), so sidebar, note list,
and relations panel reflect updates without restart.
2. After renaming a note, all other open tabs reload their content from
disk, picking up updated wikilinks immediately.
Design decisions:
- Used key-mapping approach (frontmatterToEntryPatch) instead of a new
parse_md_file Tauri command — simpler, works in both Tauri and mock
modes, covers all VaultEntry fields.
- Converted useNoteActions to config object to avoid excess arguments.
- Extracted findWikilinkTarget, reloadTabsAfterRename, renameToastMessage
as standalone functions to reduce hook complexity (cc 10→9).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 17:48:39 +01:00
|
|
|
export function useNoteActions(config: NoteActionsConfig) {
|
2026-03-16 18:31:33 +01:00
|
|
|
const { entries, setToastMessage, updateEntry } = config
|
2026-03-08 22:15:08 +01:00
|
|
|
const tabMgmt = useTabManagement()
|
2026-02-27 18:17:47 +01:00
|
|
|
const { setTabs, handleSelectNote, openTabWithContent, handleCloseTab, handleCloseTabRef, activeTabPathRef, handleSwitchTab } = tabMgmt
|
2026-02-17 12:10:21 +01:00
|
|
|
|
2026-02-22 10:11:52 +01:00
|
|
|
const updateTabContent = useCallback((path: string, newContent: string) => {
|
2026-02-22 10:55:45 +01:00
|
|
|
setTabs((prev) => prev.map((t) => t.entry.path === path ? { ...t, content: newContent } : t))
|
2026-03-08 22:15:08 +01:00
|
|
|
}, [setTabs])
|
2026-02-17 12:10:21 +01:00
|
|
|
|
2026-03-17 16:52:23 +01:00
|
|
|
// After opening a note, reload its VaultEntry so title reflects any sync.
|
|
|
|
|
const handleSelectNoteWithSync = useCallback(async (entry: VaultEntry) => {
|
2026-03-18 02:37:29 +01:00
|
|
|
// Always sync title with filename — even for already-open tabs.
|
|
|
|
|
// handleSelectNote skips sync for open tabs (early return), so we call it here first.
|
|
|
|
|
const wasModified = await syncNoteTitle(entry.path)
|
2026-03-17 16:52:23 +01:00
|
|
|
await handleSelectNote(entry)
|
|
|
|
|
// Reload entry from disk to pick up title changes from sync_note_title
|
|
|
|
|
if (isTauri()) {
|
|
|
|
|
try {
|
|
|
|
|
const fresh = await invoke<VaultEntry>('reload_vault_entry', { path: entry.path })
|
|
|
|
|
if (fresh.title !== entry.title) updateEntry(entry.path, { title: fresh.title })
|
2026-03-18 02:37:29 +01:00
|
|
|
// If sync modified the file and tab was already open, refresh tab content
|
|
|
|
|
if (wasModified) {
|
|
|
|
|
const content = await loadNoteContent(entry.path)
|
|
|
|
|
setTabs(prev => prev.map(t => t.entry.path === entry.path
|
|
|
|
|
? { entry: { ...t.entry, title: fresh.title }, content }
|
|
|
|
|
: t))
|
|
|
|
|
}
|
2026-03-17 16:52:23 +01:00
|
|
|
} catch { /* non-fatal: entry display may be stale */ }
|
|
|
|
|
}
|
2026-03-18 02:37:29 +01:00
|
|
|
}, [handleSelectNote, updateEntry, setTabs])
|
2026-03-17 16:52:23 +01:00
|
|
|
|
|
|
|
|
const creation = useNoteCreation(config, { openTabWithContent, handleSelectNote: handleSelectNoteWithSync, handleCloseTab, handleCloseTabRef })
|
2026-03-16 18:31:33 +01:00
|
|
|
const rename = useNoteRename(
|
|
|
|
|
{ entries, setToastMessage },
|
|
|
|
|
{ tabs: tabMgmt.tabs, setTabs, activeTabPathRef, handleSwitchTab, updateTabContent },
|
|
|
|
|
)
|
|
|
|
|
|
feat: add optimistic error recovery for note creation (#69)
Note creation was already optimistic (UI updates before disk write) but
errors were silently swallowed. Now if the disk write fails, the
optimistic entry is reverted (tab closed, entry removed) and the user
sees an error toast. Each note creation is independent, so one failure
in rapid Cmd+N presses doesn't affect the others.
- Add removeEntry to useVaultLoader for reverting optimistic adds
- Refactor persistNewNote to return a Promise for error handling
- Extract navigateWikilink, persistOptimistic, createAndPersist,
and runFrontmatterAndApply helpers to reduce hook complexity
- Add tests for error recovery: single note, type, success path,
and rapid creation with partial failure
- Code health: 8.93→9.01 (CC fixed: 10→7, primitive obsession improved)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 16:11:59 +01:00
|
|
|
const handleNavigateWikilink = useCallback(
|
2026-03-17 16:52:23 +01:00
|
|
|
(target: string) => navigateWikilink(entries, target, handleSelectNoteWithSync),
|
|
|
|
|
[entries, handleSelectNoteWithSync],
|
feat: add optimistic error recovery for note creation (#69)
Note creation was already optimistic (UI updates before disk write) but
errors were silently swallowed. Now if the disk write fails, the
optimistic entry is reverted (tab closed, entry removed) and the user
sees an error toast. Each note creation is independent, so one failure
in rapid Cmd+N presses doesn't affect the others.
- Add removeEntry to useVaultLoader for reverting optimistic adds
- Refactor persistNewNote to return a Promise for error handling
- Extract navigateWikilink, persistOptimistic, createAndPersist,
and runFrontmatterAndApply helpers to reduce hook complexity
- Add tests for error recovery: single note, type, success path,
and rapid creation with partial failure
- Code health: 8.93→9.01 (CC fixed: 10→7, primitive obsession improved)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 16:11:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const runFrontmatterOp = useCallback(
|
|
|
|
|
(op: 'update' | 'delete', path: string, key: string, value?: FrontmatterValue) =>
|
2026-03-16 18:31:33 +01:00
|
|
|
runFrontmatterAndApply(op, path, key, value, { updateTab: updateTabContent, updateEntry, toast: setToastMessage }),
|
|
|
|
|
[updateTabContent, updateEntry, setToastMessage],
|
feat: add optimistic error recovery for note creation (#69)
Note creation was already optimistic (UI updates before disk write) but
errors were silently swallowed. Now if the disk write fails, the
optimistic entry is reverted (tab closed, entry removed) and the user
sees an error toast. Each note creation is independent, so one failure
in rapid Cmd+N presses doesn't affect the others.
- Add removeEntry to useVaultLoader for reverting optimistic adds
- Refactor persistNewNote to return a Promise for error handling
- Extract navigateWikilink, persistOptimistic, createAndPersist,
and runFrontmatterAndApply helpers to reduce hook complexity
- Add tests for error recovery: single note, type, success path,
and rapid creation with partial failure
- Code health: 8.93→9.01 (CC fixed: 10→7, primitive obsession improved)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 16:11:59 +01:00
|
|
|
)
|
2026-02-17 12:10:21 +01:00
|
|
|
|
|
|
|
|
return {
|
2026-02-22 10:55:45 +01:00
|
|
|
...tabMgmt,
|
2026-03-17 16:52:23 +01:00
|
|
|
handleSelectNote: handleSelectNoteWithSync,
|
2026-03-16 18:31:33 +01:00
|
|
|
handleCloseTab: creation.handleCloseTabWithCleanup,
|
2026-02-17 12:10:21 +01:00
|
|
|
handleNavigateWikilink,
|
2026-03-16 18:31:33 +01:00
|
|
|
handleCreateNote: creation.handleCreateNote,
|
|
|
|
|
handleCreateNoteImmediate: creation.handleCreateNoteImmediate,
|
|
|
|
|
handleCreateNoteForRelationship: creation.handleCreateNoteForRelationship,
|
|
|
|
|
handleOpenDailyNote: creation.handleOpenDailyNote,
|
|
|
|
|
handleCreateType: creation.handleCreateType,
|
|
|
|
|
createTypeEntrySilent: creation.createTypeEntrySilent,
|
2026-03-08 21:37:27 +01:00
|
|
|
handleUpdateFrontmatter: useCallback(async (path: string, key: string, value: FrontmatterValue) => {
|
|
|
|
|
await runFrontmatterOp('update', path, key, value)
|
2026-03-16 18:31:33 +01:00
|
|
|
if (shouldRenameOnTitleUpdate(key, value)) {
|
2026-03-11 18:44:44 +01:00
|
|
|
try {
|
2026-03-16 18:31:33 +01:00
|
|
|
await renameAfterTitleChange(path, value, {
|
|
|
|
|
vaultPath: config.vaultPath, tabsRef: rename.tabsRef, replaceEntry: config.replaceEntry,
|
|
|
|
|
setTabs, activeTabPathRef, handleSwitchTab, setToastMessage, updateTabContent,
|
|
|
|
|
})
|
2026-03-11 18:44:44 +01:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to rename note after title change:', err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-16 18:31:33 +01:00
|
|
|
}, [runFrontmatterOp, config.vaultPath, config.replaceEntry, rename.tabsRef, setTabs, activeTabPathRef, handleSwitchTab, setToastMessage, updateTabContent]),
|
2026-02-22 10:55:45 +01:00
|
|
|
handleDeleteProperty: useCallback((path: string, key: string) => runFrontmatterOp('delete', path, key), [runFrontmatterOp]),
|
|
|
|
|
handleAddProperty: useCallback((path: string, key: string, value: FrontmatterValue) => runFrontmatterOp('update', path, key, value), [runFrontmatterOp]),
|
2026-03-16 18:31:33 +01:00
|
|
|
handleRenameNote: rename.handleRenameNote,
|
2026-02-17 12:10:21 +01:00
|
|
|
}
|
|
|
|
|
}
|