From 1970d98465875c0f5493bfe566ae31f8431d9764 Mon Sep 17 00:00:00 2001 From: Github Date: Fri, 19 Jul 2024 10:05:56 +0200 Subject: [PATCH 1/3] fix:improve isReportMessageAttachment performance --- src/libs/isReportMessageAttachment.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/libs/isReportMessageAttachment.ts b/src/libs/isReportMessageAttachment.ts index 9ef4125ee8bf..1631f0c9c6c0 100644 --- a/src/libs/isReportMessageAttachment.ts +++ b/src/libs/isReportMessageAttachment.ts @@ -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'); + /** * 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]. @@ -13,10 +15,13 @@ 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 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); + const isAttachmentMessageText = message.text === CONST.ATTACHMENT_MESSAGE_TEXT; + const isVideoText = Str.isVideo(message.text); + const hasAttachmentHtml = attachmentRegex.test(message.html) || message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML; + + return (isAttachmentMessageText || isVideoText) && hasAttachmentHtml; } From c88b180ede77f2170463d5240917ab49a15fee92 Mon Sep 17 00:00:00 2001 From: Github Date: Fri, 19 Jul 2024 10:55:56 +0200 Subject: [PATCH 2/3] refactor after cr --- src/libs/isReportMessageAttachment.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libs/isReportMessageAttachment.ts b/src/libs/isReportMessageAttachment.ts index 1631f0c9c6c0..94d9331a7132 100644 --- a/src/libs/isReportMessageAttachment.ts +++ b/src/libs/isReportMessageAttachment.ts @@ -19,9 +19,17 @@ export default function isReportMessageAttachment(message: Message | undefined): return message.text === CONST.ATTACHMENT_MESSAGE_TEXT && message.translationKey === CONST.TRANSLATION_KEYS.ATTACHMENT; } - const isAttachmentMessageText = message.text === CONST.ATTACHMENT_MESSAGE_TEXT; - const isVideoText = Str.isVideo(message.text); const hasAttachmentHtml = attachmentRegex.test(message.html) || message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML; - return (isAttachmentMessageText || isVideoText) && hasAttachmentHtml; + if (!hasAttachmentHtml) { + return false; + } + + const isAttachmentMessageText = message.text === CONST.ATTACHMENT_MESSAGE_TEXT; + + if (isAttachmentMessageText) { + return true; + } + + return Str.isVideo(message.text); } From 41cd5db40aba6bed752584740ea3f9762f66f3b0 Mon Sep 17 00:00:00 2001 From: Github Date: Fri, 19 Jul 2024 12:16:40 +0200 Subject: [PATCH 3/3] add small improvement --- src/libs/isReportMessageAttachment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/isReportMessageAttachment.ts b/src/libs/isReportMessageAttachment.ts index 94d9331a7132..dcb5aa97ee67 100644 --- a/src/libs/isReportMessageAttachment.ts +++ b/src/libs/isReportMessageAttachment.ts @@ -19,7 +19,7 @@ export default function isReportMessageAttachment(message: Message | undefined): return message.text === CONST.ATTACHMENT_MESSAGE_TEXT && message.translationKey === CONST.TRANSLATION_KEYS.ATTACHMENT; } - const hasAttachmentHtml = attachmentRegex.test(message.html) || message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML; + const hasAttachmentHtml = message.html === CONST.ATTACHMENT_UPLOADING_MESSAGE_HTML || attachmentRegex.test(message.html); if (!hasAttachmentHtml) { return false;