UBERF-7987 Fix drafts in chats (#6436)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2024-08-29 14:17:40 +07:00 committed by GitHub
parent 1bd66c64da
commit 376ffb0b28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -14,7 +14,7 @@
--> -->
<script lang="ts"> <script lang="ts">
import { Attachment } from '@hcengineering/attachment' import { Attachment } from '@hcengineering/attachment'
import core, { Account, Class, Doc, IdMap, Markup, Ref, Space, generateId, toIdMap } from '@hcengineering/core' import { Account, Class, Doc, IdMap, Markup, Ref, Space, generateId, toIdMap } from '@hcengineering/core'
import { Asset, IntlString, setPlatformStatus, unknownError } from '@hcengineering/platform' import { Asset, IntlString, setPlatformStatus, unknownError } from '@hcengineering/platform'
import { import {
DraftController, DraftController,
@ -63,8 +63,8 @@
const client = getClient() const client = getClient()
const query = createQuery() const query = createQuery()
const draftKey = `${objectId}_attachments` $: draftKey = `${objectId}_attachments`
const draftController = new DraftController<Record<Ref<Attachment>, Attachment>>(draftKey) $: draftController = new DraftController<Record<Ref<Attachment>, Attachment>>(draftKey)
let draftAttachments: Record<Ref<Attachment>, Attachment> | undefined = undefined let draftAttachments: Record<Ref<Attachment>, Attachment> | undefined = undefined
let originalAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>() let originalAttachments: Set<Ref<Attachment>> = new Set<Ref<Attachment>>()
@ -143,8 +143,8 @@
try { try {
const uuid = await uploadFile(file) const uuid = await uploadFile(file)
const metadata = await getFileMetadata(file, uuid) const metadata = await getFileMetadata(file, uuid)
const _id: Ref<Attachment> = generateId() const _id: Ref<Attachment> = generateId()
attachments.set(_id, { attachments.set(_id, {
_id, _id,
_class: attachment.class.Attachment, _class: attachment.class.Attachment,
@ -163,8 +163,9 @@
}) })
newAttachments.add(_id) newAttachments.add(_id)
attachments = attachments attachments = attachments
dispatch('update', { message: content, attachments: attachments.size }) saved = false
saveDraft() saveDraft()
dispatch('update', { message: content, attachments: attachments.size })
} catch (err: any) { } catch (err: any) {
void setPlatformStatus(unknownError(err)) void setPlatformStatus(unknownError(err))
} }
@ -173,6 +174,7 @@
async function saveAttachment (doc: Attachment): Promise<void> { async function saveAttachment (doc: Attachment): Promise<void> {
if (!existingAttachments.includes(doc._id)) { if (!existingAttachments.includes(doc._id)) {
await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id) await client.addCollection(attachment.class.Attachment, space, objectId, _class, 'attachments', doc, doc._id)
newAttachments.delete(doc._id)
} }
} }
@ -207,12 +209,9 @@
async function removeAttachment (attachment: Attachment): Promise<void> { async function removeAttachment (attachment: Attachment): Promise<void> {
removedAttachments.add(attachment) removedAttachments.add(attachment)
attachments.delete(attachment._id) attachments.delete(attachment._id)
if (shouldSaveDraft) {
await createAttachments()
}
attachments = attachments attachments = attachments
dispatch('update', { message: content, attachments: attachments.size })
saveDraft() saveDraft()
dispatch('update', { message: content, attachments: attachments.size })
} }
async function deleteAttachment (attachment: Attachment): Promise<void> { async function deleteAttachment (attachment: Attachment): Promise<void> {
@ -239,9 +238,6 @@
} }
}) })
} }
if (!saved && shouldSaveDraft) {
void createAttachments()
}
}) })
export function removeDraft (removeFiles: boolean): void { export function removeDraft (removeFiles: boolean): void {
@ -256,7 +252,10 @@
} }
} }
export function createAttachments (): Promise<void> { export async function createAttachments (): Promise<void> {
if (saved) {
return
}
saved = true saved = true
const promises: Promise<any>[] = [] const promises: Promise<any>[] = []
newAttachments.forEach((p) => { newAttachments.forEach((p) => {
@ -268,7 +267,10 @@
removedAttachments.forEach((p) => { removedAttachments.forEach((p) => {
promises.push(deleteAttachment(p)) promises.push(deleteAttachment(p))
}) })
return Promise.all(promises).then() await Promise.all(promises)
newAttachments.clear()
removedAttachments.clear()
saveDraft()
} }
async function onMessage (event: CustomEvent): Promise<void> { async function onMessage (event: CustomEvent): Promise<void> {