Skip to content

fix: upsert attachmemts only when attachment exist and handle FileList for react native #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

khushal87
Copy link
Member

The goal of the PR is to upsert attachments only when they exist. This is especially important because on React Native, we allow cancelling uploading by calling removeAttachments, but when the upload still happens, the upsert proceeds. We will only update you if the attachment is present.

The other fix is that the FileList is not supported on RN. Thanks to @MartinCupela for a prompt suggestion on this issue.

Copy link
Contributor

github-actions bot commented May 14, 2025

Size Change: +322 B (+0.09%)

Total Size: 379 kB

Filename Size Change
dist/cjs/index.browser.cjs 107 kB +107 B (+0.1%)
dist/cjs/index.node.cjs 151 kB +106 B (+0.07%)
dist/esm/index.js 120 kB +109 B (+0.09%)

compressed-size-action

@@ -448,7 +448,11 @@ export class AttachmentManager {
},
};

this.upsertAttachments([failedAttachment]);
const isAttachmentPresent =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khushal87 we could prevent duplication of these lines by adding a new method updateAttachmet to AttachmentManager. A proposal of a refactor:

private prepareAttachmentUpdate = (attachmentToUpdate: LocalAttachment) => {
    const stateAttachments = this.attachments;
    const attachments = [...this.attachments];
    const attachmentIndex = this.getAttachmentIndex(attachmentToUpdate.localMetadata.id);

    if (attachmentIndex === -1) return null;

    const merged = mergeWithDiff<LocalAttachment>(
      stateAttachments[attachmentIndex] ?? {},
      attachmentToUpdate,
    );
    const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
    
    if (updatesOnMerge) {
      const localAttachment = ensureIsLocalAttachment(merged.result);
      if (localAttachment) {
        attachments.splice(attachmentIndex, 1, localAttachment);
        return attachments;
      }
    }
    return null;
  };

  updateAttachment = (attachmentToUpdate: LocalAttachment) => {
    const updatedAttachments = this.prepareAttachmentUpdate(attachmentToUpdate);
    if (updatedAttachments) {
      this.state.partialNext({ attachments: updatedAttachments });
    }
  };

  upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
    if (!attachmentsToUpsert.length) return;
    
    let attachments = [...this.attachments];
    let hasUpdates = false;

    attachmentsToUpsert.forEach((attachment) => {
      const updatedAttachments = this.prepareAttachmentUpdate(attachment);
      if (updatedAttachments) {
        attachments = updatedAttachments;
        hasUpdates = true;
      } else {
        const localAttachment = ensureIsLocalAttachment(attachment);
        if (localAttachment) {
          attachments.push(localAttachment);
          hasUpdates = true;
        }
      }
    });

    if (hasUpdates) {
      this.state.partialNext({ attachments });
    }
  };

the in uploadAttachment you would only do this.updateAttachment(failedAttachment). WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this can do the trick as well. My bad I was testing upsertAttachments and thought this would not work but using the updateAttachments can be useful here and avoid duplication.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed the code and added a few relevant tests. Let me know if its fine to merge it. Thanks 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants