Skip to content

Commit 6ee9258

Browse files
authored
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
1 parent 0d797ce commit 6ee9258

File tree

7 files changed

+69
-24
lines changed

7 files changed

+69
-24
lines changed

browser/components/MarkdownSplitEditor.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ class MarkdownSplitEditor extends React.Component {
156156
linesHighlighted,
157157
RTL
158158
} = this.props
159-
const storage = findStorage(storageKey)
159+
let storage
160+
try {
161+
storage = findStorage(storageKey)
162+
} catch (e) {
163+
return <div />
164+
}
160165
let editorFontSize = parseInt(config.editor.fontSize, 10)
161166
if (!(editorFontSize > 0 && editorFontSize < 101)) editorFontSize = 14
162167
let editorIndentSize = parseInt(config.editor.indentSize, 10)

browser/main/Detail/FolderSelect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ class FolderSelect extends React.Component {
294294
{optionList}
295295
</div>
296296
</div>
297-
) : (
297+
) : currentOption ? (
298298
<div styleName='idle' style={{ color: currentOption.folder.color }}>
299299
<div styleName='idle-label'>
300300
<i className='fa fa-folder' />
@@ -303,7 +303,7 @@ class FolderSelect extends React.Component {
303303
</span>
304304
</div>
305305
</div>
306-
)}
306+
) : null}
307307
</div>
308308
)
309309
}

browser/main/Detail/MarkdownNoteDetail.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,16 @@ class MarkdownNoteDetail extends React.Component {
479479
})
480480
})
481481
})
482-
const currentOption = options.filter(
482+
483+
const currentOption = _.find(
484+
options,
483485
option =>
484486
option.storage.key === storageKey && option.folder.key === folderKey
485-
)[0]
487+
)
488+
489+
// currentOption may be undefined
490+
const storageName = _.get(currentOption, 'storage.name') || ''
491+
const folderName = _.get(currentOption, 'folder.name') || ''
486492

487493
const trashTopBar = (
488494
<div styleName='info'>
@@ -495,8 +501,8 @@ class MarkdownNoteDetail extends React.Component {
495501
/>
496502
<InfoButton onClick={e => this.handleInfoButtonClick(e)} />
497503
<InfoPanelTrashed
498-
storageName={currentOption.storage.name}
499-
folderName={currentOption.folder.name}
504+
storageName={storageName}
505+
folderName={folderName}
500506
updatedAt={formatDate(note.updatedAt)}
501507
createdAt={formatDate(note.createdAt)}
502508
exportAsHtml={this.exportAsHtml}
@@ -579,8 +585,8 @@ class MarkdownNoteDetail extends React.Component {
579585
<InfoButton onClick={e => this.handleInfoButtonClick(e)} />
580586

581587
<InfoPanel
582-
storageName={currentOption.storage.name}
583-
folderName={currentOption.folder.name}
588+
storageName={storageName}
589+
folderName={folderName}
584590
noteLink={`[${note.title}](:note:${
585591
queryString.parse(location.search).key
586592
})`}

browser/main/Detail/SnippetNoteDetail.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -885,10 +885,16 @@ class SnippetNoteDetail extends React.Component {
885885
})
886886
})
887887
})
888-
const currentOption = options.filter(
888+
889+
const currentOption = _.find(
890+
options,
889891
option =>
890892
option.storage.key === storageKey && option.folder.key === folderKey
891-
)[0]
893+
)
894+
895+
// currentOption may be undefined
896+
const storageName = _.get(currentOption, 'storage.name') || ''
897+
const folderName = _.get(currentOption, 'folder.name') || ''
892898

893899
const trashTopBar = (
894900
<div styleName='info'>
@@ -901,8 +907,8 @@ class SnippetNoteDetail extends React.Component {
901907
/>
902908
<InfoButton onClick={e => this.handleInfoButtonClick(e)} />
903909
<InfoPanelTrashed
904-
storageName={currentOption.storage.name}
905-
folderName={currentOption.folder.name}
910+
storageName={storageName}
911+
folderName={folderName}
906912
updatedAt={formatDate(note.updatedAt)}
907913
createdAt={formatDate(note.createdAt)}
908914
exportAsMd={this.showWarning}
@@ -951,8 +957,8 @@ class SnippetNoteDetail extends React.Component {
951957
<InfoButton onClick={e => this.handleInfoButtonClick(e)} />
952958

953959
<InfoPanel
954-
storageName={currentOption.storage.name}
955-
folderName={currentOption.folder.name}
960+
storageName={storageName}
961+
folderName={folderName}
956962
noteLink={`[${note.title}](:note:${
957963
queryString.parse(location.search).key
958964
})`}

browser/main/NoteList/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,10 +1107,10 @@ class NoteList extends React.Component {
11071107

11081108
getNoteFolder(note) {
11091109
// note.folder = folder key
1110-
return _.find(
1111-
this.getNoteStorage(note).folders,
1112-
({ key }) => key === note.folder
1113-
)
1110+
const storage = this.getNoteStorage(note)
1111+
return storage
1112+
? _.find(storage.folders, ({ key }) => key === note.folder)
1113+
: []
11141114
}
11151115

11161116
getViewType() {
@@ -1145,9 +1145,14 @@ class NoteList extends React.Component {
11451145
? this.getNotes().sort(sortFunc)
11461146
: this.sortByPin(this.getNotes().sort(sortFunc))
11471147
this.notes = notes = sortedNotes.filter(note => {
1148-
// this is for the trash box
1149-
if (note.isTrashed !== true || location.pathname === '/trashed')
1148+
if (
1149+
// has matching storage
1150+
!!this.getNoteStorage(note) &&
1151+
// this is for the trash box
1152+
(note.isTrashed !== true || location.pathname === '/trashed')
1153+
) {
11501154
return true
1155+
}
11511156
})
11521157
if (sortDir === 'DESCENDING') this.notes.reverse()
11531158

@@ -1193,6 +1198,8 @@ class NoteList extends React.Component {
11931198
sortBy === 'CREATED_AT' ? note.createdAt : note.updatedAt
11941199
).fromNow('D')
11951200

1201+
const storage = this.getNoteStorage(note)
1202+
11961203
if (isDefault) {
11971204
return (
11981205
<NoteItem
@@ -1205,7 +1212,7 @@ class NoteList extends React.Component {
12051212
handleDragStart={this.handleDragStart.bind(this)}
12061213
pathname={location.pathname}
12071214
folderName={this.getNoteFolder(note).name}
1208-
storageName={this.getNoteStorage(note).name}
1215+
storageName={storage.name}
12091216
viewType={viewType}
12101217
showTagsAlphabetically={config.ui.showTagsAlphabetically}
12111218
coloredTags={config.coloredTags}
@@ -1223,7 +1230,7 @@ class NoteList extends React.Component {
12231230
handleDragStart={this.handleDragStart.bind(this)}
12241231
pathname={location.pathname}
12251232
folderName={this.getNoteFolder(note).name}
1226-
storageName={this.getNoteStorage(note).name}
1233+
storageName={storage.name}
12271234
viewType={viewType}
12281235
/>
12291236
)

browser/main/lib/dataApi/attachmentManagement.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,15 @@ function getAttachmentsPathAndStatus(markdownContent, storageKey, noteKey) {
835835
if (storageKey == null || noteKey == null || markdownContent == null) {
836836
return null
837837
}
838-
const targetStorage = findStorage.findStorage(storageKey)
838+
let targetStorage = null
839+
try {
840+
targetStorage = findStorage.findStorage(storageKey)
841+
} catch (error) {
842+
console.warn(`No stroage found for: ${storageKey}`)
843+
}
844+
if (!targetStorage) {
845+
return null
846+
}
839847
const attachmentFolder = path.join(
840848
targetStorage.path,
841849
DESTINATION_FOLDER,

tests/dataApi/attachmentManagement.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,19 @@ it('should test that getAttachmentsPathAndStatus return null if noteKey, storage
912912
expect(result).toBeNull()
913913
})
914914

915+
it('should test that getAttachmentsPathAndStatus return null if no storage found', function() {
916+
const noteKey = 'test'
917+
const storageKey = 'not_exist'
918+
const markdownContent = ''
919+
920+
const result = systemUnderTest.getAttachmentsPathAndStatus(
921+
markdownContent,
922+
storageKey,
923+
noteKey
924+
)
925+
expect(result).toBeNull()
926+
})
927+
915928
it('should test that getAttachmentsPathAndStatus return the correct path and status for attachments', async function() {
916929
const dummyStorage = { path: 'dummyStoragePath' }
917930
const noteKey = 'noteKey'

0 commit comments

Comments
 (0)