Skip to content

Commit

Permalink
Fix comments and update HeaderView for GroupChat
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenjaHorbach committed Apr 29, 2024
1 parent c0be6a1 commit 6a8908a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/pages/ReportDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -231,8 +231,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD
report,
isMoneyRequestReport,
isChatRoom,
canJoinOrLeave,
canJoin,
canLeave,
activeChatMembers.length,
session,
leaveChat,
Expand Down
41 changes: 35 additions & 6 deletions src/pages/home/HeaderView.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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({
Expand All @@ -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();
}),
});
}

Expand Down Expand Up @@ -372,6 +388,19 @@ function HeaderView({
cancelText={translate('common.cancel')}
danger
/>
<ConfirmModal
danger
title={translate('groupChat.lastMemberTitle')}
isVisible={isLastMemberLeavingGroupModalVisible}
onConfirm={() => {
setIsLastMemberLeavingGroupModalVisible(false);
Report.leaveGroupChat(report.reportID);
}}
onCancel={() => setIsLastMemberLeavingGroupModalVisible(false)}
prompt={translate('groupChat.lastMemberWarning')}
confirmText={translate('common.leave')}
cancelText={translate('common.cancel')}
/>
</>
)}
</View>
Expand Down

0 comments on commit 6a8908a

Please sign in to comment.