Skip to content
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
16 changes: 1 addition & 15 deletions src/libs/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Navigation from '@libs/Navigation/Navigation';
import {getIsOffline} from '@libs/NetworkState';
import Parser from '@libs/Parser';
import type {OptionData as PersonalDetailOptionData} from '@libs/PersonalDetailOptionsListUtils/types';
import {getLoginByAccountID, getPersonalDetailByEmail, getPersonalDetailsByIDs, getPersonalDetailsListByIDs, temporaryGetDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import {getLoginByAccountID, getPersonalDetailByEmail, getPersonalDetailsListByIDs, temporaryGetDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import {addSMSDomainIfPhoneNumber, parsePhoneNumber} from '@libs/PhoneNumber';
import {
canSendInvoiceFromWorkspace,
Expand Down Expand Up @@ -54,7 +54,6 @@ import {
getLastVisibleMessage,
getMarkedReimbursedMessage,
getMccGroupCategoryMessage,
getMentionedAccountIDsFromAction,
getMessageOfOldDotReportAction,
getOneTransactionThreadReportID,
getOriginalMessage,
Expand All @@ -66,7 +65,6 @@ import {
getRenamedCardFeedMessage,
getReportAction,
getReportActionActorAccountID,
getReportActionHtml,
getReportActionMessageText,
getRequireCompanyCardsEnabledMessage,
getRoomAvatarUpdatedMessage,
Expand Down Expand Up @@ -580,12 +578,6 @@ function isSearchStringMatchUserDetails(personalDetail: PersonalDetails, searchV
return isSearchStringMatch(searchValue.trim(), memberDetails.toLowerCase());
}

function hasHiddenDisplayNames(accountIDs: number[], translate: LocalizedTranslate) {
return getPersonalDetailsByIDs({accountIDs, currentUserAccountID: 0}).some(
(personalDetail) => !temporaryGetDisplayNameOrDefault({passedPersonalDetails: personalDetail, shouldFallbackToHidden: false, translate}),
);
}

function getLatestVisibleMoneyRequestAction(
reportID: string,
canUserPerformWrite: boolean | undefined,
Expand Down Expand Up @@ -1019,12 +1011,6 @@ function getLastMessageTextForReport({
return lastMessageTextFromReport || (getReportLastMessage(reportID, isReportArchived, undefined).lastMessageText ?? '');
}

// When the last report action has unknown mentions (@Hidden), we want to consistently show @Hidden in LHN and report screen
// so we reconstruct the last message text of the report from the last report action.
if (!lastMessageTextFromReport && lastReportAction && hasHiddenDisplayNames(getMentionedAccountIDsFromAction(lastReportAction), translate)) {
lastMessageTextFromReport = Parser.htmlToText(getReportActionHtml(lastReportAction));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was added by this PR: #59551

It's to fix an issue where the last message of the LHN shows the mentioned email, but the report action message shows Hidden.

This is because for the report action message, we render the message.html, which is a mention tag with an accountID attribute. And because after we do "Clear cache & restart", the personal details for that accountID is not available yet.

But for the LHN last message, we render the report.lastMessageText, which is plain text. But that's not the case anymore. Now, we first get the message from the last action html by parsing the html.

// Fallback: use the action's own message text if not handled above.
if (!lastMessageTextFromReport && lastReportAction) {
lastMessageTextFromReport = lastVisibleMessage?.lastMessageText ?? '';
}

getLastVisibleMessage

let messageText = getReportActionMessageText(lastVisibleAction) ?? '';

return reportAction?.message?.reduce((acc, curr) => `${acc}${getTextFromHtml(curr?.html || curr?.text)}`, '') ?? '';

function getTextFromHtml(html?: string): string {
return html ? Parser.htmlToText(html) : '';
}

The problem is that the accountIDToNameMap keeps the removed personal details. So, when we do Clear cache & restart, all personal details are removed, but accountIDToNameMap keeps it. That causes the inconsistency.

App/src/libs/Parser.ts

Lines 61 to 64 in e6cc804

htmlToText(htmlString: string, extras?: Extras): string {
return super.htmlToText(htmlString, {
reportIDToName: extras?.reportIDToName ?? reportIDToNameMap,
accountIDToName: extras?.accountIDToName ?? accountIDToNameMap,

const accountIDToNameMap: Record<string, string> = {};

App/src/libs/Parser.ts

Lines 32 to 43 in e6cc804

Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (personalDetailsList) => {
for (const personalDetails of Object.values(personalDetailsList ?? {})) {
if (!personalDetails) {
continue;
}
accountIDToNameMap[personalDetails.accountID] = personalDetails.login ?? personalDetails.displayName ?? '';
}
},
});

So, I cleared it following the same pattern we already had for reportIDToNameMap.

App/src/libs/Parser.ts

Lines 11 to 17 in e6cc804

let reportIDToNameMap: Record<string, string> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
// Clear the map so removed reports don’t linger
reportIDToNameMap = {};

}

// If the last report action is a pending moderation action, get the last message text from the last visible report action
if (reportID && !lastMessageTextFromReport && isPendingRemove(lastOriginalReportAction)) {
lastMessageTextFromReport = getReportActionMessageText(lastReportAction);
Expand Down
5 changes: 3 additions & 2 deletions src/libs/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import Onyx from 'react-native-onyx';

import Log from './Log';

const accountIDToNameMap: Record<string, string> = {};

let reportIDToNameMap: Record<string, string> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
Expand All @@ -29,9 +27,12 @@ Onyx.connect({
},
});

let accountIDToNameMap: Record<string, string> = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (personalDetailsList) => {
// Clear the map so removed personal detail don’t linger
accountIDToNameMap = {};
for (const personalDetails of Object.values(personalDetailsList ?? {})) {
if (!personalDetails) {
continue;
Expand Down
1 change: 0 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4856,7 +4856,6 @@ export {
isReportActionAttachment,
isReportPreviewAction,
isReversedTransaction,
getMentionedAccountIDsFromAction,
isSentMoneyReportAction,
isSplitBillAction,
isTaskAction,
Expand Down
79 changes: 64 additions & 15 deletions tests/unit/OptionsListUtilsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,13 @@ describe('OptionsListUtils', () => {
);
});

beforeEach(async () => {
await act(async () => {
await Onyx.clear();
});
jest.clearAllMocks();
});

describe('getSearchOptions()', () => {
it('should return all options when no search value is provided', () => {
// Given a set of options
Expand Down Expand Up @@ -2034,6 +2041,11 @@ describe('OptionsListUtils', () => {
});

describe('getShareDestinationsOptions()', () => {
beforeEach(async () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}10`, REPORTS['10'] ?? {});
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}10`, reportNameValuePairs);
});

it('should exclude archived rooms and hidden threads from share destinations', () => {
// Given a set of filtered current Reports (as we do in the component) before getting share destination options
const filteredReports = Object.values(OPTIONS.reports).reduce<OptionList['reports']>((filtered, option) => {
Expand Down Expand Up @@ -3546,6 +3558,7 @@ describe('OptionsListUtils', () => {
it('createFilteredOptionList() localization', async () => {
renderLocaleContextProvider();
// Given a set of reports and personal details
await Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, PERSONAL_DETAILS);
// When we call createFilteredOptionList and extract the reports
const reports = createFilteredOptionList(PERSONAL_DETAILS, REPORTS, undefined, EMPTY_PRIVATE_IS_ARCHIVED_MAP, allPolicies, {isSearching: true}).reports;

Expand Down Expand Up @@ -5517,6 +5530,56 @@ describe('OptionsListUtils', () => {
});
expect(result).toBe(expectedVisibleText);
});
it('should return "@Hidden" when last action is an ADD_COMMENT mentioning a user not in personal details', async () => {
// Given a chat report whose last action is an ADD_COMMENT that mentions a user who does not exist in personal details
const mentionedAccountID = 999999;
const report: Report = {
...createRandomReport(0, undefined),
type: CONST.REPORT.TYPE.CHAT,
lastActionType: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
};
const addCommentAction: ReportAction = {
...createRandomReportAction(1),
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
created: DateUtils.getDBTime(),
message: [
{
type: 'COMMENT',
html: `<mention-user accountID="${mentionedAccountID}"></mention-user>`,
text: '',
isEdited: false,
isDeletedParentAction: false,
whisperedTo: [],
},
],
originalMessage: {
html: `<mention-user accountID="${mentionedAccountID}"></mention-user>`,
mentionedAccountIDs: [mentionedAccountID],
},
shouldShow: true,
pendingAction: null,
};
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report);
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, {
[addCommentAction.reportActionID]: addCommentAction,
});
await waitForBatchedUpdates();

// When we get the last message text while the mentioned user is absent from personal details
const lastMessage = getLastMessageTextForReport({
personalDetails: undefined,
translate: translateLocal,
report,
lastActorDetails: null,
policy: undefined,
isReportArchived: false,
lastAction: addCommentAction,
currentUserLogin: CURRENT_USER_EMAIL,
});

// Then the mention should fall back to the hidden placeholder
expect(lastMessage).toBe(`@${translateLocal('common.hidden')}`);
});
it('should return "No activity yet" for MoneyRequestReport with zero transactions', async () => {
const report: Report = {
...createRandomReport(0, undefined),
Expand Down Expand Up @@ -5616,13 +5679,6 @@ describe('OptionsListUtils', () => {
});

describe('DEW (Dynamic External Workflow)', () => {
beforeEach(async () => {
await act(async () => {
await Onyx.clear();
});
jest.clearAllMocks();
});

it('should show queued message for SUBMITTED action with DEW policy when offline and pending submit', async () => {
const reportID = 'dewReport1';
const report: Report = {
Expand Down Expand Up @@ -6385,13 +6441,6 @@ describe('OptionsListUtils', () => {
});

describe('getReportOption', () => {
beforeEach(async () => {
await act(async () => {
await Onyx.clear();
});
jest.clearAllMocks();
});

it('should return option with correct workspace name when policy is provided', async () => {
const reportID = '101';
const testPolicyID = 'policy123';
Expand Down Expand Up @@ -7606,7 +7655,7 @@ describe('OptionsListUtils', () => {
},
};

beforeAll(async () => {
beforeEach(async () => {
const report1: Report = {
reportID: formatReportID1,
reportName: 'Archived Format Chat',
Expand Down
Loading