diff --git a/apps/client/src/widgets/view_widgets/view_mode_storage.ts b/apps/client/src/widgets/view_widgets/view_mode_storage.ts index 7b11f53f4..b3bf6bda2 100644 --- a/apps/client/src/widgets/view_widgets/view_mode_storage.ts +++ b/apps/client/src/widgets/view_widgets/view_mode_storage.ts @@ -26,17 +26,18 @@ export default class ViewModeStorage { } async restore() { - const existingAttachments = await this.note.getAttachmentsByRole(ATTACHMENT_ROLE); + const existingAttachments = (await this.note.getAttachmentsByRole(ATTACHMENT_ROLE)) + .filter(a => a.title === this.attachmentName); if (existingAttachments.length === 0) { return undefined; } - const attachment = existingAttachments - .find(a => a.title === this.attachmentName); - if (!attachment) { - return undefined; + if (existingAttachments.length > 1) { + // Clean up duplicates. + await Promise.all(existingAttachments.slice(1).map(async a => await server.remove(`attachments/${a.attachmentId}`))); } + const attachment = existingAttachments[0]; const attachmentData = await server.get<{ content: string } | null>(`attachments/${attachment.attachmentId}/blob`); return JSON.parse(attachmentData?.content ?? "{}") as T; }