From ff3918ebfacd5d6286d26f83c24ef8e4a5940f91 Mon Sep 17 00:00:00 2001 From: Abdelrahman Khattab Date: Wed, 3 Apr 2024 08:34:08 +0200 Subject: [PATCH] Enable creating tasks with short-mentions using the create task command in the comment --- src/libs/ReportUtils.ts | 39 ++++++++++++++++---------- src/pages/home/report/ReportFooter.tsx | 13 +++++---- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 8296e38411be..b605e2604e48 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -3012,6 +3012,27 @@ function hasReportNameError(report: OnyxEntry): boolean { return !isEmptyObject(report?.errorFields?.reportName); } +/** + * Adds a domain to a short mention, converting it into a full mention with email or SMS domain. + * @param mention The user mention to be converted. + * @returns The converted mention as a full mention string or undefined if conversion is not applicable. + */ +function addDomainToShortMention(mention: string): string | undefined { + if (!Str.isValidEmail(mention) && currentUserPrivateDomain) { + const mentionWithEmailDomain = `${mention}@${currentUserPrivateDomain}`; + if (allPersonalDetailLogins.includes(mentionWithEmailDomain)) { + return mentionWithEmailDomain; + } + } + if (Str.isValidPhone(mention)) { + const mentionWithSmsDomain = PhoneNumber.addSMSDomainIfPhoneNumber(mention); + if (allPersonalDetailLogins.includes(mentionWithSmsDomain)) { + return mentionWithSmsDomain; + } + } + return undefined; +} + /** * For comments shorter than or equal to 10k chars, convert the comment from MD into HTML because that's how it is stored in the database * For longer comments, skip parsing, but still escape the text, and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!! @@ -3020,21 +3041,8 @@ function getParsedComment(text: string): string { const parser = new ExpensiMark(); const textWithMention = text.replace(CONST.REGEX.SHORT_MENTION, (match) => { const mention = match.substring(1); - - if (!Str.isValidEmail(mention) && currentUserPrivateDomain) { - const mentionWithEmailDomain = `${mention}@${currentUserPrivateDomain}`; - if (allPersonalDetailLogins.includes(mentionWithEmailDomain)) { - return `@${mentionWithEmailDomain}`; - } - } - if (Str.isValidPhone(mention)) { - const mentionWithSmsDomain = PhoneNumber.addSMSDomainIfPhoneNumber(mention); - if (allPersonalDetailLogins.includes(mentionWithSmsDomain)) { - return `@${mentionWithSmsDomain}`; - } - } - - return match; + const mentionWithDomain = addDomainToShortMention(mention); + return mentionWithDomain ? `@${mentionWithDomain}` : match; }); return text.length <= CONST.MAX_MARKUP_LENGTH ? parser.replace(textWithMention, {shouldEscapeText: !shouldAllowRawHTMLMessages()}) : lodashEscape(text); @@ -5802,6 +5810,7 @@ export { getDefaultWorkspaceAvatarTestID, getCommentLength, getParsedComment, + addDomainToShortMention, getMoneyRequestOptions, canCreateRequest, hasIOUWaitingOnCurrentUserBankAccount, diff --git a/src/pages/home/report/ReportFooter.tsx b/src/pages/home/report/ReportFooter.tsx index bd143f9ef196..04fbd0308390 100644 --- a/src/pages/home/report/ReportFooter.tsx +++ b/src/pages/home/report/ReportFooter.tsx @@ -89,10 +89,10 @@ function ReportFooter({ /** * Matching task rule by group * Group 1: Start task rule with [] - * Group 2: Optional email group between \s+....\s* start rule with @+valid email + * Group 2: Optional email group between \s+....\s* start rule with @+valid email or short mention * Group 3: Title is remaining characters */ - const taskRegex = /^\[\]\s+(?:@([^\s@]+@[\w.-]+\.[a-zA-Z]{2,}))?\s*([\s\S]*)/; + const taskRegex = /^\[\]\s+(?:@([^\s@]+(?:@\w+\.\w+)?))?\s*([\s\S]*)/; const match = text.match(taskRegex); if (!match) { @@ -102,10 +102,13 @@ function ReportFooter({ if (!title) { return false; } - const email = match[1] ? match[1].trim() : undefined; + + const mention = match[1] ? match[1].trim() : undefined; + const mentionWithDomain = ReportUtils.addDomainToShortMention(mention ?? '') ?? mention; + let assignee: OnyxTypes.PersonalDetails | EmptyObject = {}; - if (email) { - assignee = Object.values(allPersonalDetails).find((value) => value?.login === email) ?? {}; + if (mentionWithDomain) { + assignee = Object.values(allPersonalDetails).find((value) => value?.login === mentionWithDomain) ?? {}; } Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee.accountID, undefined, report.policyID); return true;