diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 812ebb051624..00e9aea7ad81 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -489,15 +489,43 @@ function getAllReportErrors(report: OnyxEntry, reportActions: OnyxEntry< return allReportErrors; } +/** + * Get the last actor display name from last actor details. + */ +function getLastActorDisplayName(lastActorDetails: Partial | null, hasMultipleParticipants: boolean) { + return hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID !== currentUserAccountID + ? lastActorDetails.firstName ?? PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails) + : ''; +} + /** * Get the last message text from the report directly or from other sources for special cases. */ -function getLastMessageTextForReport(report: OnyxEntry): string { +function getLastMessageTextForReport(report: OnyxEntry, lastActorDetails: Partial | null, policy?: OnyxEntry): string { const lastReportAction = allSortedReportActions[report?.reportID ?? '']?.find((reportAction) => ReportActionUtils.shouldReportActionBeVisibleAsLastAction(reportAction)) ?? null; + // some types of actions are filtered out for lastReportAction, in some cases we need to check the actual last action + const lastOriginalReportAction = lastReportActions[report?.reportID ?? ''] ?? null; let lastMessageTextFromReport = ''; const lastActionName = lastReportAction?.actionName ?? ''; - if (ReportActionUtils.isMoneyRequestAction(lastReportAction)) { + if (ReportUtils.isArchivedRoom(report)) { + const archiveReason = + (lastOriginalReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED && lastOriginalReportAction?.originalMessage?.reason) || CONST.REPORT.ARCHIVE_REASON.DEFAULT; + switch (archiveReason) { + case CONST.REPORT.ARCHIVE_REASON.ACCOUNT_CLOSED: + case CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY: + case CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED: { + lastMessageTextFromReport = Localize.translate(preferredLocale, `reportArchiveReasons.${archiveReason}`, { + displayName: PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails), + policyName: ReportUtils.getPolicyName(report, false, policy), + }); + break; + } + default: { + lastMessageTextFromReport = Localize.translate(preferredLocale, `reportArchiveReasons.default`); + } + } + } else if (ReportActionUtils.isMoneyRequestAction(lastReportAction)) { const properSchemaForMoneyRequestMessage = ReportUtils.getReportPreviewMessage(report, lastReportAction, true, false, null, true); lastMessageTextFromReport = ReportUtils.formatReportLastMessageText(properSchemaForMoneyRequestMessage); } else if (ReportActionUtils.isReportPreviewAction(lastReportAction)) { @@ -622,27 +650,10 @@ function createOption( hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat; subtitle = ReportUtils.getChatRoomSubtitle(report); - const lastMessageTextFromReport = getLastMessageTextForReport(report); const lastActorDetails = personalDetailMap[report.lastActorAccountID ?? 0] ?? null; - const lastActorDisplayName = - hasMultipleParticipants && lastActorDetails && lastActorDetails.accountID !== currentUserAccountID - ? // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - lastActorDetails.firstName || PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails) - : ''; - + const lastActorDisplayName = getLastActorDisplayName(lastActorDetails, hasMultipleParticipants); + const lastMessageTextFromReport = getLastMessageTextForReport(report, lastActorDetails); let lastMessageText = lastMessageTextFromReport; - const lastReportAction = lastReportActions[report.reportID ?? '']; - if (result.isArchivedRoom && lastReportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED) { - const archiveReason = lastReportAction.originalMessage?.reason || CONST.REPORT.ARCHIVE_REASON.DEFAULT; - if (archiveReason === CONST.REPORT.ARCHIVE_REASON.DEFAULT || archiveReason === CONST.REPORT.ARCHIVE_REASON.ACCOUNT_MERGED) { - lastMessageText = Localize.translate(preferredLocale, 'reportArchiveReasons.default'); - } else { - lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.${archiveReason}`, { - displayName: PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails), - policyName: ReportUtils.getPolicyName(report), - }); - } - } const lastAction = visibleReportActionItems[report.reportID]; const shouldDisplayLastActorName = lastAction && lastAction.actionName !== CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && lastAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU; @@ -1461,16 +1472,11 @@ function getOptions( if (accountIDs.length <= 1 && !isPolicyExpenseChat && !isChatRoom) { reportMapForAccountIDs[accountIDs[0]] = report; } - const isSearchingSomeonesPolicyExpenseChat = !report.isOwnPolicyExpenseChat && searchValue !== ''; - - // Checks to see if the current user is the admin of the policy, if so the policy - // name preview will be shown. - const isPolicyChatAdmin = ReportUtils.isPolicyExpenseChatAdmin(report, policies); allReportOptions.push( createOption(accountIDs, personalDetails, report, reportActions, { showChatPreviewLine, - forcePolicyNamePreview: isPolicyExpenseChat ? isSearchingSomeonesPolicyExpenseChat || isPolicyChatAdmin : forcePolicyNamePreview, + forcePolicyNamePreview, }), ); }); @@ -1987,6 +1993,7 @@ export { getParticipantsOption, isSearchStringMatch, shouldOptionShowTooltip, + getLastActorDisplayName, getLastMessageTextForReport, getEnabledCategoriesCount, hasEnabledOptions, diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index c61c6d940a48..958570e6c936 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -16,7 +16,6 @@ import * as CollectionUtils from './CollectionUtils'; import * as LocalePhoneNumber from './LocalePhoneNumber'; import * as Localize from './Localize'; import * as OptionsListUtils from './OptionsListUtils'; -import * as PersonalDetailsUtils from './PersonalDetailsUtils'; import * as ReportActionsUtils from './ReportActionsUtils'; import * as ReportUtils from './ReportUtils'; import * as TaskUtils from './TaskUtils'; @@ -49,21 +48,6 @@ Onyx.connect({ }, }); -// Session can remain stale because the only way for the current user to change is to -// sign out and sign in, which would clear out all the Onyx -// data anyway and cause SidebarLinks to rerender. -let currentUserAccountID: number | undefined; -Onyx.connect({ - key: ONYXKEYS.SESSION, - callback: (session) => { - if (!session) { - return; - } - - currentUserAccountID = session.accountID; - }, -}); - let resolveSidebarIsReadyPromise: (args?: unknown[]) => void; let sidebarIsReadyPromise = new Promise((resolve) => { @@ -239,13 +223,6 @@ function getOrderedReportIDs( return LHNReports; } -type ActorDetails = { - displayName?: string; - firstName?: string; - lastName?: string; - accountID?: number; -}; - /** * Gets all the data necessary for rendering an OptionRowLHN component */ @@ -350,12 +327,12 @@ 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); // 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 // to get data from. - let lastActorDetails: ActorDetails | null = report.lastActorAccountID && personalDetails?.[report.lastActorAccountID] ? personalDetails[report.lastActorAccountID] : null; + let lastActorDetails: Partial | null = report.lastActorAccountID && personalDetails?.[report.lastActorAccountID] ? personalDetails[report.lastActorAccountID] : null; + if (!lastActorDetails && visibleReportActionItems[report.reportID]) { const lastActorDisplayName = visibleReportActionItems[report.reportID]?.person?.[0]?.text; lastActorDetails = lastActorDisplayName @@ -366,31 +343,12 @@ function getOptionData({ : null; } - const shouldShowDisplayName = hasMultipleParticipants && lastActorDetails?.accountID && Number(lastActorDetails.accountID) !== currentUserAccountID; - const lastActorName = lastActorDetails?.firstName ?? lastActorDetails?.displayName; - const lastActorDisplayName = shouldShowDisplayName ? lastActorName : ''; + const lastActorDisplayName = OptionsListUtils.getLastActorDisplayName(lastActorDetails, hasMultipleParticipants); + const lastMessageTextFromReport = OptionsListUtils.getLastMessageTextForReport(report, lastActorDetails, policy); let lastMessageText = lastMessageTextFromReport; const reportAction = lastReportActions?.[report.reportID]; - if (result.isArchivedRoom) { - const archiveReason = (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED && reportAction?.originalMessage?.reason) || CONST.REPORT.ARCHIVE_REASON.DEFAULT; - - switch (archiveReason) { - case CONST.REPORT.ARCHIVE_REASON.ACCOUNT_CLOSED: - case CONST.REPORT.ARCHIVE_REASON.REMOVED_FROM_POLICY: - case CONST.REPORT.ARCHIVE_REASON.POLICY_DELETED: { - lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.${archiveReason}`, { - policyName: ReportUtils.getPolicyName(report, false, policy), - displayName: PersonalDetailsUtils.getDisplayNameOrDefault(lastActorDetails), - }); - break; - } - default: { - lastMessageText = Localize.translate(preferredLocale, `reportArchiveReasons.default`); - } - } - } const isThreadMessage = ReportUtils.isThread(report) && reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && reportAction?.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; diff --git a/tests/unit/SidebarTest.js b/tests/unit/SidebarTest.js index 8f9d7492a1f5..dba1365626ba 100644 --- a/tests/unit/SidebarTest.js +++ b/tests/unit/SidebarTest.js @@ -56,6 +56,14 @@ describe('Sidebar', () => { stateNum: CONST.REPORT.STATE_NUM.APPROVED, }; + const action = { + ...LHNTestUtils.getFakeReportAction('email1@test.com', 3, true), + actionName: 'CLOSED', + originalMessage: { + reason: CONST.REPORT.ARCHIVE_REASON.DEFAULT, + }, + }; + // Given the user is in all betas const betas = [CONST.BETAS.DEFAULT_ROOMS]; LHNTestUtils.getDefaultRenderedSidebarLinks('0'); @@ -69,6 +77,7 @@ describe('Sidebar', () => { [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, [`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`]: report, + [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`]: {[action.reportActionId]: action}, }), ) .then(() => {