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:
lucaronin
2026-02-17 17:52:10 +01:00
parent 829cd2ad95
commit b239de3719
2 changed files with 24 additions and 16 deletions

View File

@@ -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])