Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve isOneOnOneChat and getParticipantsAccountIDsForDisplay #47300

Merged
38 changes: 19 additions & 19 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1597,19 +1597,13 @@ function isOneTransactionThread(reportID: string, parentReportID: string, thread
*
*/
function isOneOnOneChat(report: OnyxEntry<Report>): boolean {
const participantAccountIDs = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID);
return (
!isChatRoom(report) &&
!isExpenseRequest(report) &&
!isMoneyRequestReport(report) &&
!isPolicyExpenseChat(report) &&
!isTaskReport(report) &&
isDM(report) &&
!isIOUReport(report) &&
participantAccountIDs.length === 1
);
const participants = report?.participants ?? {};
const isCurrentUserParticipant = participants[currentUserAccountID ?? 0] ? 1 : 0;
const participantAmount = Object.keys(participants).length - isCurrentUserParticipant;
if (participantAmount !== 1) {
return false;
}
return !isChatRoom(report) && !isExpenseRequest(report) && !isMoneyRequestReport(report) && !isPolicyExpenseChat(report) && !isTaskReport(report) && isDM(report) && !isIOUReport(report);
}

/**
Expand Down Expand Up @@ -2040,7 +2034,8 @@ function getDisplayNameForParticipant(
}

function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldExcludeHidden = false, shouldExcludeDeleted = false): number[] {
let participantsEntries = Object.entries(report?.participants ?? {});
const reportParticipants = report?.participants ?? {};
let participantsEntries = Object.entries(reportParticipants);

// We should not show participants that have an optimistic entry with the same login in the personal details
const nonOptimisticLoginMap: Record<string, boolean | undefined> = {};
Expand All @@ -2061,29 +2056,34 @@ function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldEx
return true;
});

let participantsIds = participantsEntries.map(([accountID]) => Number(accountID));

// For 1:1 chat, we don't want to include the current user as a participant in order to not mark 1:1 chats as having multiple participants
// For system chat, we want to display Expensify as the only participant
const shouldExcludeCurrentUser = isOneOnOneChat(report) || isSystemChat(report);

if (shouldExcludeCurrentUser || shouldExcludeHidden || shouldExcludeDeleted) {
participantsEntries = participantsEntries.filter(([accountID, participant]) => {
if (shouldExcludeCurrentUser && Number(accountID) === currentUserAccountID) {
participantsIds = participantsIds.filter((accountID) => {
if (shouldExcludeCurrentUser && accountID === currentUserAccountID) {
return false;
}

if (shouldExcludeHidden && participant.hidden) {
if (shouldExcludeHidden && reportParticipants[accountID]?.hidden) {
return false;
}

if (shouldExcludeDeleted && report?.pendingChatMembers?.findLast((member) => member.accountID === accountID)?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
if (
shouldExcludeDeleted &&
report?.pendingChatMembers?.findLast((member) => Number(member.accountID) === accountID)?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE
) {
return false;
}

return true;
});
}

return participantsEntries.map(([accountID]) => Number(accountID)).filter((accountID) => isNumber(accountID));
return participantsIds.filter((accountID) => isNumber(accountID));
}

function getParticipantsList(report: Report, personalDetails: OnyxEntry<PersonalDetailsList>, isRoomMembersList = false): number[] {
Expand Down
Loading