Skip to content

Commit

Permalink
Merge pull request Expensify#47300 from callstack-internal/edu/isOneO…
Browse files Browse the repository at this point in the history
…nOneChat_improvement

perf: improve isOneOnOneChat and getParticipantsAccountIDsForDisplay
  • Loading branch information
Beamanator authored Aug 26, 2024
2 parents d4d1c0c + 4c0d892 commit 728c51a
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1590,19 +1590,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 @@ -2033,7 +2027,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 @@ -2054,29 +2049,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

0 comments on commit 728c51a

Please sign in to comment.