diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 7e99c60cb618..f30137894d91 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1851,13 +1851,18 @@ function getDisplayNameForParticipant(accountID?: number, shouldUseShortForm = f return shouldUseShortForm ? shortName : longName; } -function getParticipantAccountIDs(reportID: string) { +function getParticipantAccountIDs(reportID: string, includeOnlyActiveMembers = false) { const report = getReport(reportID); if (!report || !report.participants) { return []; } - - const accountIDStrings = Object.keys(report.participants); + const accountIDStrings = Object.keys(report.participants).filter((accountID) => { + if (!includeOnlyActiveMembers) { + return true; + } + const pendingMember = report?.pendingChatMembers?.findLast((member) => member.accountID === accountID.toString()); + return !pendingMember || pendingMember.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; + }); return accountIDStrings.map((accountID) => Number(accountID)); } diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 3a1cd85b8040..40f3c93a0492 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -2736,12 +2736,17 @@ function inviteToRoom(reportID: string, inviteeEmailsToAccountIDs: InvitedEmails ...newPersonalDetailsOnyxData.optimisticData, ]; + const successPendingChatMembers = report?.pendingChatMembers + ? report?.pendingChatMembers?.filter( + (pendingMember) => !(inviteeAccountIDs.includes(Number(pendingMember.accountID)) && pendingMember.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), + ) + : null; const successData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, value: { - pendingChatMembers: report?.pendingChatMembers ?? null, + pendingChatMembers: successPendingChatMembers, }, }, ...newPersonalDetailsOnyxData.finallyData, diff --git a/src/pages/InviteReportParticipantsPage.tsx b/src/pages/InviteReportParticipantsPage.tsx index 74bb5bec96d7..0754f81576e9 100644 --- a/src/pages/InviteReportParticipantsPage.tsx +++ b/src/pages/InviteReportParticipantsPage.tsx @@ -52,7 +52,10 @@ function InviteReportParticipantsPage({betas, personalDetails, report, didScreen const [userToInvite, setUserToInvite] = useState(null); // Any existing participants and Expensify emails should not be eligible for invitation - const excludedUsers = useMemo(() => [...PersonalDetailsUtils.getLoginsByAccountIDs(ReportUtils.getParticipantAccountIDs(report?.reportID ?? '')), ...CONST.EXPENSIFY_EMAILS], [report]); + const excludedUsers = useMemo( + () => [...PersonalDetailsUtils.getLoginsByAccountIDs(ReportUtils.getParticipantAccountIDs(report?.reportID ?? '', true)), ...CONST.EXPENSIFY_EMAILS], + [report], + ); useEffect(() => { const inviteOptions = OptionsListUtils.getMemberInviteOptions(options.personalDetails, betas ?? [], searchTerm, excludedUsers, false, options.reports, true); diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 02f97397e4cd..8a3d7da0c884 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -212,7 +212,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD icon: Expensicons.Exit, isAnonymousAction: true, action: () => { - if (Object.keys(report?.participants ?? {}).length === 1 && isGroupChat) { + if (ReportUtils.getParticipantAccountIDs(report.reportID, true).length === 1 && isGroupChat) { setIsLastMemberLeavingGroupModalVisible(true); return; }