diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 3451e14e388b..7da99d3ca8e6 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -293,6 +293,7 @@ type OptimisticChatReport = Pick< | 'writeCapability' | 'avatarUrl' | 'invoiceReceiver' + | 'isHidden' > & { isOptimisticReport: true; }; diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index fab1374ebea6..329119d688c9 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -650,6 +650,26 @@ function setAssigneeChatReport(chatReport: OnyxTypes.Report) { Onyx.merge(ONYXKEYS.TASK, {assigneeChatReport: chatReport}); } +function setNewOptimisticAssignee(assigneeLogin: string, assigneeAccountID: number) { + const report: ReportUtils.OptimisticChatReport = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); + + // When assigning a task to a new user, by default we share the task in their DM + // However, the DM doesn't exist yet - and will be created optimistically once the task is created + // We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown + // So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created + report.isHidden = true; + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); + + const optimisticPersonalDetailsListAction: OnyxTypes.PersonalDetails = { + accountID: assigneeAccountID, + avatar: allPersonalDetails?.[assigneeAccountID]?.avatar ?? UserUtils.getDefaultAvatarURL(assigneeAccountID), + displayName: allPersonalDetails?.[assigneeAccountID]?.displayName ?? assigneeLogin, + login: assigneeLogin, + }; + Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction}); + return {assignee: optimisticPersonalDetailsListAction, assigneeReport: report}; +} + /** * Sets the assignee value for the task and checks for an existing chat with the assignee * If there is no existing chat, it creates an optimistic chat report @@ -670,26 +690,7 @@ function setAssigneeValue( } // If chat report is still not found we need to build new optimistic chat report if (!report) { - report = ReportUtils.buildOptimisticChatReport([assigneeAccountID]); - report.isOptimisticReport = true; - - // When assigning a task to a new user, by default we share the task in their DM - // However, the DM doesn't exist yet - and will be created optimistically once the task is created - // We don't want to show the new DM yet, because if you select an assignee and then change the assignee, the previous DM will still be shown - // So here, we create it optimistically to share it with the assignee, but we have to hide it until the task is created - if (report) { - report.isHidden = true; - } - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); - - // If this is an optimistic report, we likely don't have their personal details yet so we set it here optimistically as well - const optimisticPersonalDetailsListAction = { - accountID: assigneeAccountID, - avatar: allPersonalDetails?.[assigneeAccountID]?.avatar ?? UserUtils.getDefaultAvatarURL(assigneeAccountID), - displayName: allPersonalDetails?.[assigneeAccountID]?.displayName ?? assigneeEmail, - login: assigneeEmail, - }; - Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {[assigneeAccountID]: optimisticPersonalDetailsListAction}); + report = setNewOptimisticAssignee(assigneeEmail, assigneeAccountID).assigneeReport; } // The optimistic field may not exist in the existing report and it can be overridden by the optimistic field of previous report data when merging the assignee chat report @@ -1051,6 +1052,7 @@ export { getTaskAssigneeAccountID, clearTaskErrors, canModifyTask, + setNewOptimisticAssignee, }; export type {PolicyValue, Assignee, ShareDestination}; diff --git a/src/pages/home/report/ReportFooter.tsx b/src/pages/home/report/ReportFooter.tsx index 9a8ca2955127..b128b83f88fc 100644 --- a/src/pages/home/report/ReportFooter.tsx +++ b/src/pages/home/report/ReportFooter.tsx @@ -12,6 +12,7 @@ import useNetwork from '@hooks/useNetwork'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; import * as ReportUtils from '@libs/ReportUtils'; +import * as UserUtils from '@libs/UserUtils'; import variables from '@styles/variables'; import * as Report from '@userActions/Report'; import * as Task from '@userActions/Task'; @@ -110,10 +111,17 @@ function ReportFooter({ const mentionWithDomain = ReportUtils.addDomainToShortMention(mention ?? '') ?? mention; let assignee: OnyxTypes.PersonalDetails | EmptyObject = {}; + let assigneeChatReport; if (mentionWithDomain) { assignee = Object.values(allPersonalDetails).find((value) => value?.login === mentionWithDomain) ?? {}; + if (!Object.keys(assignee).length) { + const assigneeAccountID = UserUtils.generateAccountID(mentionWithDomain); + const optimisticDataForNewAssignee = Task.setNewOptimisticAssignee(mentionWithDomain, assigneeAccountID); + assignee = optimisticDataForNewAssignee.assignee; + assigneeChatReport = optimisticDataForNewAssignee.assigneeReport; + } } - Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee.accountID, undefined, report.policyID); + Task.createTaskAndNavigate(report.reportID, title, '', assignee?.login ?? '', assignee.accountID, assigneeChatReport, report.policyID); return true; }, [allPersonalDetails, report.policyID, report.reportID],