Skip to content

Commit

Permalink
Merge pull request #39475 from abzokhattab/enable-creating-tasks-with…
Browse files Browse the repository at this point in the history
…-short-mention-using-comments-commands

Enable creating tasks with short-mentions using  the create task commands
  • Loading branch information
MonilBhavsar authored Apr 17, 2024
2 parents 1f694e2 + 0cef88d commit 553e54f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
39 changes: 24 additions & 15 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3123,6 +3123,27 @@ function hasReportNameError(report: OnyxEntry<Report>): 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.isValidE164Phone(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!!
Expand All @@ -3131,21 +3152,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.isValidE164Phone(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);
Expand Down Expand Up @@ -6110,6 +6118,7 @@ export {
getDefaultWorkspaceAvatarTestID,
getCommentLength,
getParsedComment,
addDomainToShortMention,
getMoneyRequestOptions,
canCreateRequest,
hasIOUWaitingOnCurrentUserBankAccount,
Expand Down
13 changes: 8 additions & 5 deletions src/pages/home/report/ReportFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 553e54f

Please sign in to comment.