diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 7211e09c9f59..2e826e89c8a0 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -106,8 +106,8 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD const isPrivateNotesFetchTriggered = report?.isLoadingPrivateNotes !== undefined; const isSelfDM = useMemo(() => ReportUtils.isSelfDM(report), [report]); - const canJoinOrLeave = !isSelfDM && !isGroupChat && (isChatThread || isUserCreatedPolicyRoom || canLeaveRoom || canLeavePolicyExpenseChat); - const canJoin = report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const canLeave = + !isSelfDM && (isChatThread || isUserCreatedPolicyRoom || canLeaveRoom || canLeavePolicyExpenseChat) && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; useEffect(() => { // Do not fetch private notes if isLoadingPrivateNotes is already defined, or if the network is offline, or if the report is a self DM. @@ -200,7 +200,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD }); } - if (isGroupChat || (isChatRoom && canJoinOrLeave && !canJoin)) { + if (isGroupChat || (isChatRoom && canLeave)) { items.push({ key: CONST.REPORT_DETAILS_MENU_ITEM.LEAVE_ROOM, translationKey: 'common.leave', @@ -231,8 +231,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD report, isMoneyRequestReport, isChatRoom, - canJoinOrLeave, - canJoin, + canLeave, activeChatMembers.length, session, leaveChat, diff --git a/src/pages/home/HeaderView.tsx b/src/pages/home/HeaderView.tsx index 4295f1f0c46a..d529dfe6e1c8 100644 --- a/src/pages/home/HeaderView.tsx +++ b/src/pages/home/HeaderView.tsx @@ -1,4 +1,4 @@ -import React, {memo, useMemo} from 'react'; +import React, {memo, useCallback, useMemo, useState} from 'react'; import {View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import {withOnyx} from 'react-native-onyx'; @@ -82,7 +82,8 @@ function HeaderView({ onNavigationMenuButtonClicked, shouldUseNarrowLayout = false, }: HeaderViewProps) { - const [isDeleteTaskConfirmModalVisible, setIsDeleteTaskConfirmModalVisible] = React.useState(false); + const [isDeleteTaskConfirmModalVisible, setIsDeleteTaskConfirmModalVisible] = useState(false); + const [isLastMemberLeavingGroupModalVisible, setIsLastMemberLeavingGroupModalVisible] = useState(false); const {windowWidth} = useWindowDimensions(); const {translate} = useLocalize(); const theme = useTheme(); @@ -157,8 +158,17 @@ function HeaderView({ Report.updateNotificationPreference(reportID, report.notificationPreference, CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS, false, report.parentReportID, report.parentReportActionID), ); - const canJoinOrLeave = !isSelfDM && !isGroupChat && (isChatThread || isUserCreatedPolicyRoom || canLeaveRoom || canLeavePolicyExpenseChat); - const canJoin = canJoinOrLeave && !isWhisperAction && report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN; + const leaveChat = useCallback(() => { + if (isChatRoom) { + const isWorkspaceMemberLeavingWorkspaceRoom = !isChatThread && (report.visibility === CONST.REPORT.VISIBILITY.RESTRICTED || isPolicyExpenseChat) && isPolicyEmployee; + Report.leaveRoom(report.reportID, isWorkspaceMemberLeavingWorkspaceRoom); + return; + } + Report.leaveGroupChat(report.reportID); + }, [isChatRoom, isChatThread, isPolicyEmployee, isPolicyExpenseChat, report.reportID, report.visibility]); + + const canJoinOrLeave = !isSelfDM && (isChatThread || isUserCreatedPolicyRoom || canLeaveRoom || canLeavePolicyExpenseChat); + const canJoin = canJoinOrLeave && !isWhisperAction && report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN && !isGroupChat; const canLeave = canJoinOrLeave && ((isChatThread && !!report.notificationPreference?.length) || isUserCreatedPolicyRoom || canLeaveRoom || canLeavePolicyExpenseChat); if (canJoin) { threeDotMenuItems.push({ @@ -167,11 +177,17 @@ function HeaderView({ onSelected: join, }); } else if (canLeave) { - const isWorkspaceMemberLeavingWorkspaceRoom = !isChatThread && (report.visibility === CONST.REPORT.VISIBILITY.RESTRICTED || isPolicyExpenseChat) && isPolicyEmployee; threeDotMenuItems.push({ icon: Expensicons.ChatBubbles, text: translate('common.leave'), - onSelected: Session.checkIfActionIsAllowed(() => Report.leaveRoom(reportID, isWorkspaceMemberLeavingWorkspaceRoom)), + onSelected: Session.checkIfActionIsAllowed(() => { + if ((report?.participantAccountIDs ?? []).length === 1 && isGroupChat) { + setIsLastMemberLeavingGroupModalVisible(true); + return; + } + + leaveChat(); + }), }); } @@ -372,6 +388,19 @@ function HeaderView({ cancelText={translate('common.cancel')} danger /> + { + setIsLastMemberLeavingGroupModalVisible(false); + Report.leaveGroupChat(report.reportID); + }} + onCancel={() => setIsLastMemberLeavingGroupModalVisible(false)} + prompt={translate('groupChat.lastMemberWarning')} + confirmText={translate('common.leave')} + cancelText={translate('common.cancel')} + /> )}