Skip to content

Commit

Permalink
Merge pull request #34872 from koko57/refactor/34411-lhn-previews-ref…
Browse files Browse the repository at this point in the history
…actor

Refactor/34411 lhn previews refactor
  • Loading branch information
mountiny authored Jan 31, 2024
2 parents 568c785 + cba0608 commit a1ac92d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 73 deletions.
61 changes: 34 additions & 27 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,43 @@ function getAllReportErrors(report: OnyxEntry<Report>, reportActions: OnyxEntry<
return allReportErrors;
}

/**
* Get the last actor display name from last actor details.
*/
function getLastActorDisplayName(lastActorDetails: Partial<PersonalDetails> | 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<Report>): string {
function getLastMessageTextForReport(report: OnyxEntry<Report>, lastActorDetails: Partial<PersonalDetails> | null, policy?: OnyxEntry<Policy>): 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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}),
);
});
Expand Down Expand Up @@ -1987,6 +1993,7 @@ export {
getParticipantsOption,
isSearchStringMatch,
shouldOptionShowTooltip,
getLastActorDisplayName,
getLastMessageTextForReport,
getEnabledCategoriesCount,
hasEnabledOptions,
Expand Down
50 changes: 4 additions & 46 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<PersonalDetails> | 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
Expand All @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/SidebarTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('Sidebar', () => {
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
};

const action = {
...LHNTestUtils.getFakeReportAction('[email protected]', 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');
Expand All @@ -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(() => {
Expand Down

0 comments on commit a1ac92d

Please sign in to comment.