From 8e0b08540337d3cddc6b06fb46cc7461ee5b8de2 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Fri, 26 Jul 2019 23:03:17 +0800 Subject: [PATCH 1/3] optimize: when storage or folder is removed, Detail components should render without error, fix #2876 --- browser/main/Detail/MarkdownNoteDetail.js | 14 +++++++++----- browser/main/Detail/SnippetNoteDetail.js | 15 ++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/browser/main/Detail/MarkdownNoteDetail.js b/browser/main/Detail/MarkdownNoteDetail.js index 45024751e..32b2a3bf9 100755 --- a/browser/main/Detail/MarkdownNoteDetail.js +++ b/browser/main/Detail/MarkdownNoteDetail.js @@ -424,7 +424,11 @@ class MarkdownNoteDetail extends React.Component { }) }) }) - const currentOption = options.filter((option) => option.storage.key === storageKey && option.folder.key === folderKey)[0] + const currentOption = _.find(options, (option) => option.storage.key === storageKey && option.folder.key === folderKey) + + // currentOption may be undefined + const storageName = _.get(currentOption, 'storage.name') || '' + const folderName = _.get(currentOption, 'folder.name') || '' const trashTopBar =
@@ -436,8 +440,8 @@ class MarkdownNoteDetail extends React.Component { onClick={(e) => this.handleInfoButtonClick(e)} /> option.storage.key === storageKey && option.folder.key === folderKey)[0] + + const currentOption = _.find(options, (option) => option.storage.key === storageKey && option.folder.key === folderKey) + + // currentOption may be undefined + const storageName = _.get(currentOption, 'storage.name') || '' + const folderName = _.get(currentOption, 'folder.name') || '' const trashTopBar =
@@ -794,8 +799,8 @@ class SnippetNoteDetail extends React.Component { onClick={(e) => this.handleInfoButtonClick(e)} /> Date: Sat, 28 Mar 2020 15:20:24 +0800 Subject: [PATCH 2/3] optimize: Handle some scenarios where storage is not found, should not break the renderer --- browser/components/MarkdownSplitEditor.js | 7 ++++++- browser/main/Detail/FolderSelect.js | 4 ++-- browser/main/lib/dataApi/attachmentManagement.js | 10 +++++++++- tests/dataApi/attachmentManagement.test.js | 13 +++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/browser/components/MarkdownSplitEditor.js b/browser/components/MarkdownSplitEditor.js index de06e1317..6db73bed3 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 9a4b547ec..9b2398ad7 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/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index f3b11997e..f59a7ef3c 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 e49556cac..2759445c1 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' From 15d59ae2a054e21ba0954304fe99c10c1c098ccf Mon Sep 17 00:00:00 2001 From: hikerpig Date: Sun, 29 Mar 2020 17:55:19 +0800 Subject: [PATCH 3/3] optimize: NoteList should work without error when storage is not found --- browser/main/NoteList/index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/browser/main/NoteList/index.js b/browser/main/NoteList/index.js index 92ba5d09b..2722bc3ba 100644 --- a/browser/main/NoteList/index.js +++ b/browser/main/NoteList/index.js @@ -1107,10 +1107,10 @@ class NoteList extends React.Component { getNoteFolder(note) { // note.folder = folder key - return _.find( - this.getNoteStorage(note).folders, - ({ key }) => 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 ( )