From 6ee92588b10cb9bbc5f9b43dd367ffd7439ca554 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Mon, 6 Apr 2020 17:02:52 +0800 Subject: [PATCH] When storage or folder is removed, Detail components should render without error (#3168) * optimize: when storage or folder is removed, Detail components should render without error, fix #2876 * optimize: Handle some scenarios where storage is not found, should not break the renderer * optimize: NoteList should work without error when storage is not found --- browser/components/MarkdownSplitEditor.js | 7 +++++- browser/main/Detail/FolderSelect.js | 4 ++-- browser/main/Detail/MarkdownNoteDetail.js | 18 ++++++++++----- browser/main/Detail/SnippetNoteDetail.js | 18 ++++++++++----- browser/main/NoteList/index.js | 23 ++++++++++++------- .../main/lib/dataApi/attachmentManagement.js | 10 +++++++- tests/dataApi/attachmentManagement.test.js | 13 +++++++++++ 7 files changed, 69 insertions(+), 24 deletions(-) diff --git a/browser/components/MarkdownSplitEditor.js b/browser/components/MarkdownSplitEditor.js index de06e131..6db73bed 100644 --- a/browser/components/MarkdownSplitEditor.js +++ b/browser/components/MarkdownSplitEditor.js @@ -156,7 +156,12 @@ class MarkdownSplitEditor extends React.Component { linesHighlighted, RTL } = this.props - const storage = findStorage(storageKey) + let storage + try { + storage = findStorage(storageKey) + } catch (e) { + return
+ } let editorFontSize = parseInt(config.editor.fontSize, 10) if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14 let editorIndentSize = parseInt(config.editor.indentSize, 10) diff --git a/browser/main/Detail/FolderSelect.js b/browser/main/Detail/FolderSelect.js index 9a4b547e..9b2398ad 100644 --- a/browser/main/Detail/FolderSelect.js +++ b/browser/main/Detail/FolderSelect.js @@ -294,7 +294,7 @@ class FolderSelect extends React.Component { {optionList}
- ) : ( + ) : currentOption ? (
@@ -303,7 +303,7 @@ class FolderSelect extends React.Component {
- )} + ) : null} ) } diff --git a/browser/main/Detail/MarkdownNoteDetail.js b/browser/main/Detail/MarkdownNoteDetail.js index d9f5f256..7770252b 100755 --- a/browser/main/Detail/MarkdownNoteDetail.js +++ b/browser/main/Detail/MarkdownNoteDetail.js @@ -479,10 +479,16 @@ class MarkdownNoteDetail extends React.Component { }) }) }) - const currentOption = options.filter( + + const currentOption = _.find( + options, option => option.storage.key === storageKey && option.folder.key === folderKey - )[0] + ) + + // currentOption may be undefined + const storageName = _.get(currentOption, 'storage.name') || '' + const folderName = _.get(currentOption, 'folder.name') || '' const trashTopBar = (
@@ -495,8 +501,8 @@ class MarkdownNoteDetail extends React.Component { /> this.handleInfoButtonClick(e)} /> this.handleInfoButtonClick(e)} /> option.storage.key === storageKey && option.folder.key === folderKey - )[0] + ) + + // currentOption may be undefined + const storageName = _.get(currentOption, 'storage.name') || '' + const folderName = _.get(currentOption, 'folder.name') || '' const trashTopBar = (
@@ -901,8 +907,8 @@ class SnippetNoteDetail extends React.Component { /> this.handleInfoButtonClick(e)} /> this.handleInfoButtonClick(e)} /> key === note.folder - ) + const storage = this.getNoteStorage(note) + return storage + ? _.find(storage.folders, ({ key }) => key === note.folder) + : [] } getViewType() { @@ -1145,9 +1145,14 @@ class NoteList extends React.Component { ? this.getNotes().sort(sortFunc) : this.sortByPin(this.getNotes().sort(sortFunc)) this.notes = notes = sortedNotes.filter(note => { - // this is for the trash box - if (note.isTrashed !== true || location.pathname === '/trashed') + if ( + // has matching storage + !!this.getNoteStorage(note) && + // this is for the trash box + (note.isTrashed !== true || location.pathname === '/trashed') + ) { return true + } }) if (sortDir === 'DESCENDING') this.notes.reverse() @@ -1193,6 +1198,8 @@ class NoteList extends React.Component { sortBy === 'CREATED_AT' ? note.createdAt : note.updatedAt ).fromNow('D') + const storage = this.getNoteStorage(note) + if (isDefault) { return ( ) diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index f3b11997..f59a7ef3 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -835,7 +835,15 @@ function getAttachmentsPathAndStatus(markdownContent, storageKey, noteKey) { if (storageKey == null || noteKey == null || markdownContent == null) { return null } - const targetStorage = findStorage.findStorage(storageKey) + let targetStorage = null + try { + targetStorage = findStorage.findStorage(storageKey) + } catch (error) { + console.warn(`No stroage found for: ${storageKey}`) + } + if (!targetStorage) { + return null + } const attachmentFolder = path.join( targetStorage.path, DESTINATION_FOLDER, diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index e49556ca..2759445c 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -912,6 +912,19 @@ it('should test that getAttachmentsPathAndStatus return null if noteKey, storage expect(result).toBeNull() }) +it('should test that getAttachmentsPathAndStatus return null if no storage found', function() { + const noteKey = 'test' + const storageKey = 'not_exist' + const markdownContent = '' + + const result = systemUnderTest.getAttachmentsPathAndStatus( + markdownContent, + storageKey, + noteKey + ) + expect(result).toBeNull() +}) + it('should test that getAttachmentsPathAndStatus return the correct path and status for attachments', async function() { const dummyStorage = { path: 'dummyStoragePath' } const noteKey = 'noteKey'