From 8a6328ef07dc332dcf00bbeade4d5441ac349ee8 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 14 Mar 2024 06:42:09 +0500 Subject: [PATCH] web: reopen notes after permanently deleting an attachment --- apps/web/src/stores/attachment-store.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/apps/web/src/stores/attachment-store.ts b/apps/web/src/stores/attachment-store.ts index b9489161b..67758a12c 100644 --- a/apps/web/src/stores/attachment-store.ts +++ b/apps/web/src/stores/attachment-store.ts @@ -118,17 +118,22 @@ class AttachmentStore extends BaseStore { permanentDelete = async (attachment: Attachment) => { try { this._changeWorkingStatus(attachment.hash, "delete"); + const linkedNotes = ( + await db.relations + .to({ id: attachment.id, type: "attachment" }, "note") + .get() + ).map((l) => l.toId); if (await db.attachments.remove(attachment.hash, false)) { await this.get().refresh(); - const session = useEditorStore.getState().getActiveSession(); //.id; - if ( - session && - (await db.relations - .to({ id: attachment.id, type: "attachment" }, "note") - .has(session.id)) - ) { - useEditorStore.getState().closeSessions(session.id); + const { sessions, openSession } = useEditorStore.getState(); + for (const session of sessions) { + if ( + linkedNotes.includes(session.id) || + ("note" in session && linkedNotes.includes(session.note.id)) + ) + continue; + await openSession(session.id, { force: true, silent: true }); } } } catch (e) {