Skip to content
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

Improve isReportMessageAttachment performance #45748

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/libs/isReportMessageAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Str} from 'expensify-common';
import CONST from '@src/CONST';
import type {Message} from '@src/types/onyx/ReportAction';

const attachmentRegex = new RegExp(` ${CONST.ATTACHMENT_SOURCE_ATTRIBUTE}="(.*)"`, 'i');
Copy link
Contributor

@ZhenjaHorbach ZhenjaHorbach Jul 19, 2024

Choose a reason for hiding this comment

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

I have a small question
Is a space at the beginning of RegExp required?

After testing, I noticed that RegExp works the same

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh
Just noticed
The same RegExp was before

Copy link
Contributor

Choose a reason for hiding this comment

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

👍


/**
* Check whether a report action is Attachment or not.
* Ignore messages containing [Attachment] as the main content. Attachments are actions with only text as [Attachment].
Expand All @@ -13,10 +15,21 @@ export default function isReportMessageAttachment(message: Message | undefined):
return false;
}

if (message.translationKey && message.text === CONST.ATTACHMENT_MESSAGE_TEXT) {
return message?.translationKey === CONST.TRANSLATION_KEYS.ATTACHMENT;
if (message.translationKey) {
return message.text === CONST.ATTACHMENT_MESSAGE_TEXT && message.translationKey === CONST.TRANSLATION_KEYS.ATTACHMENT;
}

const hasAttachmentHtml = message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML || attachmentRegex.test(message.html);

if (!hasAttachmentHtml) {
return false;
}

const isAttachmentMessageText = message.text === CONST.ATTACHMENT_MESSAGE_TEXT;

if (isAttachmentMessageText) {
return true;
}

const regex = new RegExp(` ${CONST.ATTACHMENT_SOURCE_ATTRIBUTE}="(.*)"`, 'i');
return (message.text === CONST.ATTACHMENT_MESSAGE_TEXT || !!Str.isVideo(message.text)) && (!!message.html.match(regex) || message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML);
return Str.isVideo(message.text);
}
Loading