fix: prevent crash when opening notes
Move setActiveTabPath after tab content is loaded to prevent Editor's useEffect from firing before the tab exists in the tabs array. Add try/catch around editor.replaceBlocks for defensive error handling. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -180,18 +180,27 @@ export const Editor = memo(function Editor({
|
||||
const tab = tabs.find(t => t.entry.path === activeTabPath)
|
||||
if (!tab) return
|
||||
|
||||
if (cache.has(activeTabPath)) {
|
||||
// Instant switch — use cached blocks
|
||||
editor.replaceBlocks(editor.document, cache.get(activeTabPath)!)
|
||||
} else {
|
||||
// First open — parse markdown
|
||||
const [, body] = splitFrontmatter(tab.content)
|
||||
const preprocessed = preProcessWikilinks(body)
|
||||
editor.tryParseMarkdownToBlocks(preprocessed).then(blocks => {
|
||||
const withWikilinks = injectWikilinks(blocks)
|
||||
editor.replaceBlocks(editor.document, withWikilinks)
|
||||
cache.set(activeTabPath, withWikilinks)
|
||||
})
|
||||
try {
|
||||
if (cache.has(activeTabPath)) {
|
||||
// Instant switch — use cached blocks
|
||||
editor.replaceBlocks(editor.document, cache.get(activeTabPath)!)
|
||||
} else {
|
||||
// First open — parse markdown
|
||||
const [, body] = splitFrontmatter(tab.content)
|
||||
const preprocessed = preProcessWikilinks(body)
|
||||
editor.tryParseMarkdownToBlocks(preprocessed).then(blocks => {
|
||||
const withWikilinks = injectWikilinks(blocks)
|
||||
try {
|
||||
editor.replaceBlocks(editor.document, withWikilinks)
|
||||
} catch (err) {
|
||||
console.error('Failed to replace blocks:', err)
|
||||
return
|
||||
}
|
||||
cache.set(activeTabPath, withWikilinks)
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Failed to swap editor content:', err)
|
||||
}
|
||||
}, [activeTabPath, tabs, editor])
|
||||
|
||||
|
||||
@@ -111,10 +111,7 @@ export function useNoteActions(
|
||||
return
|
||||
}
|
||||
|
||||
// Set active immediately for instant visual feedback
|
||||
setActiveTabPath(entry.path)
|
||||
|
||||
// Load content async
|
||||
// Load content async, then add tab and set active together
|
||||
try {
|
||||
const content = isTauri()
|
||||
? await invoke<string>('get_note_content', { path: entry.path })
|
||||
@@ -123,12 +120,14 @@ export function useNoteActions(
|
||||
if (prev.some((t) => t.entry.path === entry.path)) return prev
|
||||
return [...prev, { entry, content }]
|
||||
})
|
||||
setActiveTabPath(entry.path)
|
||||
} catch (err) {
|
||||
console.warn('Failed to load note content:', err)
|
||||
setTabs((prev) => {
|
||||
if (prev.some((t) => t.entry.path === entry.path)) return prev
|
||||
return [...prev, { entry, content: '' }]
|
||||
})
|
||||
setActiveTabPath(entry.path)
|
||||
}
|
||||
}, [])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user