Skip to content

Commit

Permalink
Use getParticipantsAccountIDsForDisplay. DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
s77rt committed Jun 12, 2024
1 parent 79e619a commit 3eeddae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 67 deletions.
6 changes: 1 addition & 5 deletions src/components/ReportWelcomeText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,13 @@ type ReportWelcomeTextProps = ReportWelcomeTextOnyxProps & {
function ReportWelcomeText({report, policy, personalDetails}: ReportWelcomeTextProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const [session] = useOnyx(ONYXKEYS.SESSION);
const isPolicyExpenseChat = ReportUtils.isPolicyExpenseChat(report);
const isChatRoom = ReportUtils.isChatRoom(report);
const isSelfDM = ReportUtils.isSelfDM(report);
const isInvoiceRoom = ReportUtils.isInvoiceRoom(report);
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const isSystemChat = ReportUtils.isSystemChat(report);
const isDefault = !(isChatRoom || isPolicyExpenseChat || isSelfDM || isInvoiceRoom || isSystemChat);
const participantAccountIDs = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== session?.accountID || (!isOneOnOneChat && !isSystemChat));
const participantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report);
const isMultipleParticipant = participantAccountIDs.length > 1;
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(OptionsListUtils.getPersonalDetailsForAccountIDs(participantAccountIDs, personalDetails), isMultipleParticipant);
const roomWelcomeMessage = ReportUtils.getRoomWelcomeMessage(report);
Expand Down
35 changes: 6 additions & 29 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -773,15 +773,10 @@ function createOption(
result.policyID = report.policyID;
result.isSelfDM = ReportUtils.isSelfDM(report);

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const visibleParticipantAccountIDs = Object.entries(report.participants ?? {})
.filter(([, participant]) => participant && !participant.hidden)
.map(([accountID]) => Number(accountID))
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true);

result.tooltipText = ReportUtils.getReportParticipantsTitle(visibleParticipantAccountIDs);
result.isOneOnOneChat = isOneOnOneChat;
result.isOneOnOneChat = ReportUtils.isOneOnOneChat(report);

hasMultipleParticipants = personalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat || ReportUtils.isGroupChat(report);
subtitle = ReportUtils.getChatRoomSubtitle(report);
Expand Down Expand Up @@ -838,13 +833,7 @@ function createOption(
*/
function getReportOption(participant: Participant): ReportUtils.OptionData {
const report = ReportUtils.getReport(participant.reportID);

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const visibleParticipantAccountIDs = Object.entries(report?.participants ?? {})
.filter(([, reportParticipant]) => reportParticipant && !reportParticipant.hidden)
.map(([accountID]) => Number(accountID))
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true);

const option = createOption(
visibleParticipantAccountIDs,
Expand Down Expand Up @@ -1553,11 +1542,8 @@ function createOptionList(personalDetails: OnyxEntry<PersonalDetailsList>, repor
return;
}

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const accountIDs = Object.keys(report.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const accountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report);

const isChatRoom = ReportUtils.isChatRoom(report);
if ((!accountIDs || accountIDs.length === 0) && !isChatRoom) {
Expand Down Expand Up @@ -1590,11 +1576,7 @@ function createOptionList(personalDetails: OnyxEntry<PersonalDetailsList>, repor
}

function createOptionFromReport(report: Report, personalDetails: OnyxEntry<PersonalDetailsList>) {
// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const accountIDs = Object.keys(report.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const accountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report);

return {
item: report,
Expand Down Expand Up @@ -1863,13 +1845,8 @@ function getOptions(
const isPolicyExpenseChat = option.isPolicyExpenseChat;
const isMoneyRequestReport = option.isMoneyRequestReport;
const isSelfDM = option.isSelfDM;
const isOneOnOneChat = option.isOneOnOneChat;
const isChatRoom = option.isChatRoom;

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const accountIDs = Object.keys(report.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const accountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report);

if (isPolicyExpenseChat && report.isOwnPolicyExpenseChat && !includeOwnedWorkspaceChats) {
return;
Expand Down
35 changes: 22 additions & 13 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,24 @@ function getParticipantAccountIDs(reportID: string, includeOnlyActiveMembers = f
return accountIDStrings.map((accountID) => Number(accountID));
}

function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldExcludeHidden = false): number[] {
// 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 shouldFilterOutCurrentUser = isOneOnOneChat(report) || isSystemChat(report);

let accountIDs = shouldExcludeHidden
? Object.entries(report?.participants ?? {})
.filter(([, participant]) => participant && !participant.hidden)
.map(([accountID]) => Number(accountID))
: Object.keys(report?.participants ?? {}).map(Number);

if (shouldFilterOutCurrentUser) {
accountIDs = accountIDs.filter((accountID) => accountID !== currentUserAccountID);
}

return accountIDs;
}

function buildParticipantsFromAccountIDs(accountIDs: number[]): Participants {
const finalParticipants: Participants = {};
return accountIDs.reduce((participants, accountID) => {
Expand Down Expand Up @@ -3479,11 +3497,7 @@ function getParentNavigationSubtitle(report: OnyxEntry<Report>): ParentNavigatio
function navigateToDetailsPage(report: OnyxEntry<Report>) {
const isSelfDMReport = isSelfDM(report);
const isOneOnOneChatReport = isOneOnOneChat(report);

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const participantAccountID = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChatReport);
const participantAccountID = getParticipantsAccountIDsForDisplay(report);

if (isSelfDMReport || isOneOnOneChatReport) {
Navigation.navigate(ROUTES.PROFILE.getRoute(participantAccountID[0]));
Expand All @@ -3500,11 +3514,7 @@ function navigateToDetailsPage(report: OnyxEntry<Report>) {
*/
function goBackToDetailsPage(report: OnyxEntry<Report>) {
const isOneOnOneChatReport = isOneOnOneChat(report);

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const participantAccountID = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChatReport);
const participantAccountID = getParticipantsAccountIDsForDisplay(report);

if (isOneOnOneChatReport) {
Navigation.navigate(ROUTES.PROFILE.getRoute(participantAccountID[0]));
Expand All @@ -3523,9 +3533,7 @@ function goBackFromPrivateNotes(report: OnyxEntry<Report>, session: OnyxEntry<Se
}
const currentUserPrivateNote = report.privateNotes?.[session.accountID]?.note ?? '';
if (isEmpty(currentUserPrivateNote)) {
const participantAccountIDs = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat(report));
const participantAccountIDs = getParticipantsAccountIDsForDisplay(report);

if (isOneOnOneChat(report)) {
Navigation.goBack(ROUTES.PROFILE.getRoute(participantAccountIDs[0]));
Expand Down Expand Up @@ -7044,6 +7052,7 @@ export {
getParentNavigationSubtitle,
getParsedComment,
getParticipantAccountIDs,
getParticipantsAccountIDsForDisplay,
getParticipants,
getPendingChatMembers,
getPersonalDetailsForAccountID,
Expand Down
13 changes: 3 additions & 10 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,8 @@ function getOptionData({
isDeletedParentAction: false,
};

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const participantAccountIDs = Object.keys(report.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const visibleParticipantAccountIDs = Object.entries(report.participants ?? {})
.filter(([, participant]) => participant && !participant.hidden)
.map(([accountID]) => Number(accountID))
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const participantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report);
const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true);

const participantPersonalDetailList = Object.values(OptionsListUtils.getPersonalDetailsForAccountIDs(participantAccountIDs, personalDetails)) as PersonalDetails[];
const personalDetail = participantPersonalDetailList[0] ?? {};
Expand Down Expand Up @@ -295,7 +288,7 @@ function getOptionData({
result.chatType = report.chatType;
result.isDeletedParentAction = report.isDeletedParentAction;
result.isSelfDM = ReportUtils.isSelfDM(report);
result.isOneOnOneChat = isOneOnOneChat;
result.isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
result.tooltipText = ReportUtils.getReportParticipantsTitle(visibleParticipantAccountIDs);

const hasMultipleParticipants = participantPersonalDetailList.length > 1 || result.isChatRoom || result.isPolicyExpenseChat || ReportUtils.isExpenseReport(report);
Expand Down
4 changes: 1 addition & 3 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,7 @@ function getShareDestination(reportID: string, reports: OnyxCollection<OnyxTypes

const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);

const participants = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== currentUserAccountID || !isOneOnOneChat);
const participants = ReportUtils.getParticipantsAccountIDsForDisplay(report);

const isMultipleParticipant = participants.length > 1;
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(OptionsListUtils.getPersonalDetailsForAccountIDs(participants, personalDetails), isMultipleParticipant);
Expand Down
8 changes: 1 addition & 7 deletions src/pages/home/HeaderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,8 @@ function HeaderView({
const styles = useThemeStyles();
const isSelfDM = ReportUtils.isSelfDM(report);
const isGroupChat = ReportUtils.isGroupChat(report) || ReportUtils.isDeprecatedGroupDM(report);
const isOneOnOneChat = ReportUtils.isOneOnOneChat(report);
const isSystemChat = ReportUtils.isSystemChat(report);

// For 1:1 chat, we don't want to include currentUser as participants in order to not mark 1:1 chats as having multiple participants
const participants = Object.keys(report?.participants ?? {})
.map(Number)
.filter((accountID) => accountID !== session?.accountID || (!isOneOnOneChat && !isSystemChat))
.slice(0, 5);
const participants = ReportUtils.getParticipantsAccountIDsForDisplay(report).slice(0, 5);
const isMultipleParticipant = participants.length > 1;

const participantPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs(participants, personalDetails);
Expand Down

0 comments on commit 3eeddae

Please sign in to comment.