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

Enable creating tasks with short-mentions using the create task commands #39475

Merged
Show file tree
Hide file tree
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
39 changes: 24 additions & 15 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3021,6 +3021,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) {
MonilBhavsar marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -3029,21 +3050,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 @@ -5819,6 +5827,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]*)/;
Copy link
Contributor

Choose a reason for hiding this comment

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

This regex change caused #49454 which we fixed in #49685


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
Loading