fix: exception when selected note is deleted permanently (#1010)
This commit is contained in:
@@ -8,7 +8,7 @@ import { FunctionComponent } from 'preact'
|
|||||||
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
|
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
|
||||||
import { Icon } from '@/Components/Icon'
|
import { Icon } from '@/Components/Icon'
|
||||||
import { useCloseOnBlur } from '@/Hooks/useCloseOnBlur'
|
import { useCloseOnBlur } from '@/Hooks/useCloseOnBlur'
|
||||||
import { ChallengeReason, CollectionSort, ContentType, SNFile } from '@standardnotes/snjs'
|
import { ChallengeReason, CollectionSort, ContentType, SNFile, SNNote } from '@standardnotes/snjs'
|
||||||
import { confirmDialog } from '@/Services/AlertService'
|
import { confirmDialog } from '@/Services/AlertService'
|
||||||
import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit'
|
import { addToast, dismissToast, ToastType } from '@standardnotes/stylekit'
|
||||||
import { StreamingFileReader } from '@standardnotes/filepicker'
|
import { StreamingFileReader } from '@standardnotes/filepicker'
|
||||||
@@ -46,7 +46,7 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
|
|||||||
const premiumModal = usePremiumModal()
|
const premiumModal = usePremiumModal()
|
||||||
const filePreviewModal = useFilePreviewModal()
|
const filePreviewModal = useFilePreviewModal()
|
||||||
|
|
||||||
const note = Object.values(appState.notes.selectedNotes)[0]
|
const note: SNNote | undefined = Object.values(appState.notes.selectedNotes)[0]
|
||||||
|
|
||||||
const [open, setOpen] = useState(false)
|
const [open, setOpen] = useState(false)
|
||||||
const [position, setPosition] = useState({
|
const [position, setPosition] = useState({
|
||||||
@@ -69,7 +69,9 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
|
|||||||
|
|
||||||
const unregisterFileStream = application.streamItems(ContentType.File, () => {
|
const unregisterFileStream = application.streamItems(ContentType.File, () => {
|
||||||
setAllFiles(application.items.getDisplayableItems<SNFile>(ContentType.File))
|
setAllFiles(application.items.getDisplayableItems<SNFile>(ContentType.File))
|
||||||
setAttachedFiles(application.items.getFilesForNote(note))
|
if (note) {
|
||||||
|
setAttachedFiles(application.items.getFilesForNote(note))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -132,12 +134,27 @@ export const AttachedFilesButton: FunctionComponent<Props> = observer(
|
|||||||
|
|
||||||
const attachFileToNote = useCallback(
|
const attachFileToNote = useCallback(
|
||||||
async (file: SNFile) => {
|
async (file: SNFile) => {
|
||||||
|
if (!note) {
|
||||||
|
addToast({
|
||||||
|
type: ToastType.Error,
|
||||||
|
message: 'Could not attach file because selected note was deleted',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
await application.items.associateFileWithNote(file, note)
|
await application.items.associateFileWithNote(file, note)
|
||||||
},
|
},
|
||||||
[application.items, note],
|
[application.items, note],
|
||||||
)
|
)
|
||||||
|
|
||||||
const detachFileFromNote = async (file: SNFile) => {
|
const detachFileFromNote = async (file: SNFile) => {
|
||||||
|
if (!note) {
|
||||||
|
addToast({
|
||||||
|
type: ToastType.Error,
|
||||||
|
message: 'Could not attach file because selected note was deleted',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
await application.items.disassociateFileWithNote(file, note)
|
await application.items.disassociateFileWithNote(file, note)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user