From d0c910e7ddce28a923aa400694f800013e1a5cbd Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Tue, 2 Jan 2024 16:36:04 +0100 Subject: [PATCH 1/3] Use report.lastMessageText as the all-case fallback for getLastMessageTextForReport --- src/libs/OptionsListUtils.js | 10 +++++----- src/libs/PersonalDetailsUtils.ts | 32 ++++++++++++++++++++++++++++++++ src/libs/SidebarUtils.ts | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index 0d5162399fcb..1e8429810671 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -379,9 +379,10 @@ function getAllReportErrors(report, reportActions) { /** * Get the last message text from the report directly or from other sources for special cases. * @param {Object} report + * @param {Object[]} personalDetails - list of personal details of the report participants * @returns {String} */ -function getLastMessageTextForReport(report) { +function getLastMessageTextForReport(report, personalDetails) { const lastReportAction = _.find(allSortedReportActions[report.reportID], (reportAction) => ReportActionUtils.shouldReportActionBeVisibleAsLastAction(reportAction)); let lastMessageTextFromReport = ''; const lastActionName = lodashGet(lastReportAction, 'actionName', ''); @@ -418,10 +419,9 @@ function getLastMessageTextForReport(report) { lastMessageTextFromReport = lodashGet(lastReportAction, 'message[0].text', ''); } else if (ReportActionUtils.isCreatedTaskReportAction(lastReportAction)) { lastMessageTextFromReport = TaskUtils.getTaskCreatedMessage(lastReportAction); - } else { - lastMessageTextFromReport = report ? report.lastMessageText || '' : ''; } - return lastMessageTextFromReport; + + return lastMessageTextFromReport || PersonalDetailsUtils.replaceLoginsWithDisplayNames(lodashGet(report, 'lastMessageText', ''), personalDetails); } /** @@ -509,7 +509,7 @@ function createOption(accountIDs, personalDetails, report, reportActions = {}, { hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat; subtitle = ReportUtils.getChatRoomSubtitle(report); - const lastMessageTextFromReport = getLastMessageTextForReport(report); + const lastMessageTextFromReport = getLastMessageTextForReport(report, personalDetailList); const lastActorDetails = personalDetailMap[report.lastActorAccountID] || null; const lastActorDisplayName = hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID !== currentUserAccountID ? lastActorDetails.firstName || lastActorDetails.displayName : ''; diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index a5a033797c7b..00007b5e04d2 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -6,13 +6,28 @@ import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as UserUtils from './UserUtils'; +let currentUserAccountID: number | undefined; +Onyx.connect({ + key: ONYXKEYS.SESSION, + callback: (value) => { + // When signed out, val is undefined + if (!value) { + return; + } + + currentUserAccountID = value.accountID; + }, +}); + let personalDetails: Array = []; let allPersonalDetails: OnyxEntry = {}; +let currentUserPersonalDetails: OnyxEntry; Onyx.connect({ key: ONYXKEYS.PERSONAL_DETAILS_LIST, callback: (val) => { personalDetails = Object.values(val ?? {}); allPersonalDetails = val; + currentUserPersonalDetails = val?.[currentUserAccountID ?? -1] ?? null; }, }); @@ -27,6 +42,22 @@ function getDisplayNameOrDefault(displayName?: string, defaultValue = ''): strin return displayName || defaultValue || Localize.translateLocal('common.hidden'); } +function replaceLoginsWithDisplayNames(text: string, details: PersonalDetails[], includeCurrentAccount = true): string { + const result = details.reduce((replacedText, detail) => { + if (!detail.login) { + return replacedText; + } + + return replacedText.replaceAll(detail.login, getDisplayNameOrDefault(detail?.displayName)); + }, text); + + if (!includeCurrentAccount || !currentUserPersonalDetails) { + return result; + } + + return result.replaceAll(currentUserPersonalDetails.login ?? '', getDisplayNameOrDefault(currentUserPersonalDetails?.displayName)); +} + /** * Given a list of account IDs (as number) it will return an array of personal details objects. * @param accountIDs - Array of accountIDs @@ -212,4 +243,5 @@ export { getFormattedStreet, getStreetLines, getEffectiveDisplayName, + replaceLoginsWithDisplayNames, }; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index c4fad1a86906..cf90f87ab5e3 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -319,7 +319,7 @@ function getOptionData( // We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade. const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((participantPersonalDetailList || []).slice(0, 10), hasMultipleParticipants); - const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report); + const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report, displayNamesWithTooltips); // If the last actor's details are not currently saved in Onyx Collection, // then try to get that from the last report action if that action is valid From 1d1bf146385d833f6a49e18800d47f99dec70c32 Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Thu, 4 Jan 2024 19:59:12 +0100 Subject: [PATCH 2/3] Update usage of getDisplayNameOrDefault --- src/libs/PersonalDetailsUtils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index fbec0044c367..5a2f525dec43 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -7,7 +7,7 @@ import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as UserUtils from './UserUtils'; -let currentUserAccountID: number | undefined; +let sessionUserAccountID: number | undefined; Onyx.connect({ key: ONYXKEYS.SESSION, callback: (value) => { @@ -16,7 +16,7 @@ Onyx.connect({ return; } - currentUserAccountID = value.accountID; + sessionUserAccountID = value.accountID; }, }); @@ -28,7 +28,7 @@ Onyx.connect({ callback: (val) => { personalDetails = Object.values(val ?? {}); allPersonalDetails = val; - currentUserPersonalDetails = val?.[currentUserAccountID ?? -1] ?? null; + currentUserPersonalDetails = val?.[sessionUserAccountID ?? -1] ?? null; }, }); @@ -44,14 +44,14 @@ function replaceLoginsWithDisplayNames(text: string, details: PersonalDetails[], return replacedText; } - return replacedText.replaceAll(detail.login, getDisplayNameOrDefault(detail?.displayName)); + return replacedText.replaceAll(detail.login, getDisplayNameOrDefault(detail)); }, text); if (!includeCurrentAccount || !currentUserPersonalDetails) { return result; } - return result.replaceAll(currentUserPersonalDetails.login ?? '', getDisplayNameOrDefault(currentUserPersonalDetails?.displayName)); + return result.replaceAll(currentUserPersonalDetails.login ?? '', getDisplayNameOrDefault(currentUserPersonalDetails)); } /** From 8e1d451290cf853e06f64997fc62e1f393e2aaaa Mon Sep 17 00:00:00 2001 From: Pavlo Tsimura Date: Thu, 11 Jan 2024 18:03:35 +0100 Subject: [PATCH 3/3] Use report.lastMessageText as the all-case fallback --- src/libs/OptionsListUtils.js | 7 +++---- src/libs/PersonalDetailsUtils.ts | 32 -------------------------------- src/libs/SidebarUtils.ts | 2 +- 3 files changed, 4 insertions(+), 37 deletions(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index f86ac5457c49..0be73ce2735e 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -397,10 +397,9 @@ function getAllReportErrors(report, reportActions) { /** * Get the last message text from the report directly or from other sources for special cases. * @param {Object} report - * @param {Object[]} personalDetails - list of personal details of the report participants * @returns {String} */ -function getLastMessageTextForReport(report, personalDetails) { +function getLastMessageTextForReport(report) { const lastReportAction = _.find(allSortedReportActions[report.reportID], (reportAction) => ReportActionUtils.shouldReportActionBeVisibleAsLastAction(reportAction)); let lastMessageTextFromReport = ''; const lastActionName = lodashGet(lastReportAction, 'actionName', ''); @@ -439,7 +438,7 @@ function getLastMessageTextForReport(report, personalDetails) { lastMessageTextFromReport = TaskUtils.getTaskCreatedMessage(lastReportAction); } - return lastMessageTextFromReport || PersonalDetailsUtils.replaceLoginsWithDisplayNames(lodashGet(report, 'lastMessageText', ''), personalDetails); + return lastMessageTextFromReport || lodashGet(report, 'lastMessageText', ''); } /** @@ -527,7 +526,7 @@ function createOption(accountIDs, personalDetails, report, reportActions = {}, { hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat; subtitle = ReportUtils.getChatRoomSubtitle(report); - const lastMessageTextFromReport = getLastMessageTextForReport(report, personalDetailList); + const lastMessageTextFromReport = getLastMessageTextForReport(report); const lastActorDetails = personalDetailMap[report.lastActorAccountID] || null; const lastActorDisplayName = hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID !== currentUserAccountID diff --git a/src/libs/PersonalDetailsUtils.ts b/src/libs/PersonalDetailsUtils.ts index 5a2f525dec43..3346094adeec 100644 --- a/src/libs/PersonalDetailsUtils.ts +++ b/src/libs/PersonalDetailsUtils.ts @@ -7,28 +7,13 @@ import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as UserUtils from './UserUtils'; -let sessionUserAccountID: number | undefined; -Onyx.connect({ - key: ONYXKEYS.SESSION, - callback: (value) => { - // When signed out, val is undefined - if (!value) { - return; - } - - sessionUserAccountID = value.accountID; - }, -}); - let personalDetails: Array = []; let allPersonalDetails: OnyxEntry = {}; -let currentUserPersonalDetails: OnyxEntry; Onyx.connect({ key: ONYXKEYS.PERSONAL_DETAILS_LIST, callback: (val) => { personalDetails = Object.values(val ?? {}); allPersonalDetails = val; - currentUserPersonalDetails = val?.[sessionUserAccountID ?? -1] ?? null; }, }); @@ -38,22 +23,6 @@ function getDisplayNameOrDefault(passedPersonalDetails?: Partial((replacedText, detail) => { - if (!detail.login) { - return replacedText; - } - - return replacedText.replaceAll(detail.login, getDisplayNameOrDefault(detail)); - }, text); - - if (!includeCurrentAccount || !currentUserPersonalDetails) { - return result; - } - - return result.replaceAll(currentUserPersonalDetails.login ?? '', getDisplayNameOrDefault(currentUserPersonalDetails)); -} - /** * Given a list of account IDs (as number) it will return an array of personal details objects. * @param accountIDs - Array of accountIDs @@ -239,5 +208,4 @@ export { getFormattedStreet, getStreetLines, getEffectiveDisplayName, - replaceLoginsWithDisplayNames, }; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index 8631c7ceeb12..6e46ec320066 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -323,7 +323,7 @@ function getOptionData( // We only create tooltips for the first 10 users or so since some reports have hundreds of users, causing performance to degrade. const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips((participantPersonalDetailList || []).slice(0, 10), hasMultipleParticipants); - const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report, displayNamesWithTooltips); + const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report); // If the last actor's details are not currently saved in Onyx Collection, // then try to get that from the last report action if that action is valid