diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index d306ecf338bf..4c11c4e73424 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -540,6 +540,18 @@ function getOptionData({ const actionMessage = getReportActionMessageText(lastAction); result.alternateText = actionMessage ? `${lastActorDisplayName}: ${actionMessage}` : ''; } else if (isInviteOrRemovedAction(lastAction)) { + let actorDetails; + if (lastAction.actorAccountID) { + actorDetails = personalDetails?.[lastAction?.actorAccountID]; + } + let actorDisplayName = lastAction?.person?.[0]?.text; + if (!actorDetails && actorDisplayName && lastAction.actorAccountID) { + actorDetails = { + displayName: actorDisplayName, + accountID: lastAction.actorAccountID, + }; + } + actorDisplayName = actorDetails ? getLastActorDisplayName(actorDetails, hasMultipleParticipants) : undefined; const lastActionOriginalMessage = lastAction?.actionName ? getOriginalMessage(lastAction) : null; const targetAccountIDs = lastActionOriginalMessage?.targetAccountIDs ?? []; const targetAccountIDsLength = targetAccountIDs.length !== 0 ? targetAccountIDs.length : report.lastMessageHtml?.match(/]*><\/mention-user>/g)?.length ?? 0; @@ -548,7 +560,7 @@ function getOptionData({ ? translate(preferredLocale, 'workspace.invite.invited') : translate(preferredLocale, 'workspace.invite.removed'); const users = translate(preferredLocale, targetAccountIDsLength > 1 ? 'workspace.invite.users' : 'workspace.invite.user'); - result.alternateText = formatReportLastMessageText(`${lastActorDisplayName} ${verb} ${targetAccountIDsLength} ${users}`); + result.alternateText = formatReportLastMessageText(`${actorDisplayName ?? lastActorDisplayName} ${verb} ${targetAccountIDsLength} ${users}`); const roomName = getReportName(allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${lastActionOriginalMessage?.reportID}`]); if (roomName) { const preposition = diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index eca4ccf6fba6..e0bfb1b88845 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -531,6 +531,73 @@ describe('SidebarUtils', () => { expect(optionData?.alternateText).toBe(`${policy.name} ${CONST.DOT_SEPARATOR} test message`); }); + it('returns the last action message as an alternate text if the action is INVITETOROOM type', async () => { + // When a report has last action of INVITETOROOM type + const policy: Policy = { + ...createRandomPolicy(1), + role: CONST.POLICY.ROLE.ADMIN, + pendingAction: null, + }; + const session = { + authToken: 'sensitive-auth-token', + encryptedAuthToken: 'sensitive-encrypted-token', + email: 'user@example.com', + accountID: 12345, + }; + const report: Report = { + ...createRandomReport(4), + chatType: 'policyRoom', + lastMessageHtml: 'invited 1 user', + lastMessageText: 'invited 1 user', + lastVisibleActionCreated: '2025-01-20 12:30:03.784', + participants: { + '12345': { + notificationPreference: 'daily', + role: 'admin', + }, + }, + policyID: '1', + }; + const lastAction: ReportAction = { + ...createRandomReportAction(2), + message: [ + { + type: 'COMMENT', + html: 'invited ', + text: 'invited', + isEdited: false, + whisperedTo: [], + isDeletedParentAction: false, + deleted: '', + }, + ], + originalMessage: { + lastModified: '2025-03-04 10:32:10.416', + targetAccountIDs: [19268914], + }, + actorAccountID: 12345, + actionName: CONST.REPORT.ACTIONS.TYPE.ROOM_CHANGE_LOG.INVITE_TO_ROOM, + }; + const reportActions: ReportActions = {[lastAction.reportActionID]: lastAction}; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, reportActions); + await Onyx.set(ONYXKEYS.SESSION, session); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}1`, policy); + const result = SidebarUtils.getOptionData({ + report, + reportActions, + reportNameValuePairs: {}, + hasViolations: false, + personalDetails: {}, + policy: undefined, + parentReportAction: undefined, + preferredLocale: CONST.LOCALES.EN, + oneTransactionThreadReport: undefined, + }); + + // Then the alternate text should be equal to the message of the last action prepended with the last actor display name. + expect(result?.alternateText).toBe(`invited 1 user`); + }); }); }); });