Add canToggleEncryption validation to RoomView/RightButtons

This commit is contained in:
Diego Mello 2024-05-14 16:14:23 -03:00
parent 6702893944
commit 119666a0ac
1 changed files with 37 additions and 7 deletions

View File

@ -12,7 +12,7 @@ import { ILivechatDepartment } from '../../definitions/ILivechatDepartment';
import { ILivechatTag } from '../../definitions/ILivechatTag'; import { ILivechatTag } from '../../definitions/ILivechatTag';
import i18n from '../../i18n'; import i18n from '../../i18n';
import database from '../../lib/database'; import database from '../../lib/database';
import { showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers'; import { hasPermission, showConfirmationAlert, showErrorAlert } from '../../lib/methods/helpers';
import { closeLivechat as closeLivechatService } from '../../lib/methods/helpers/closeLivechat'; import { closeLivechat as closeLivechatService } from '../../lib/methods/helpers/closeLivechat';
import { events, logEvent } from '../../lib/methods/helpers/log'; import { events, logEvent } from '../../lib/methods/helpers/log';
import { Services } from '../../lib/services'; import { Services } from '../../lib/services';
@ -50,6 +50,7 @@ interface IRightButtonsProps extends Pick<ISubscription, 't'> {
issuesWithNotifications: boolean; issuesWithNotifications: boolean;
notificationsDisabled?: boolean; notificationsDisabled?: boolean;
hasE2EEWarning: boolean; hasE2EEWarning: boolean;
toggleRoomE2EEncryptionPermission?: string[];
} }
interface IRigthButtonsState { interface IRigthButtonsState {
@ -57,6 +58,7 @@ interface IRigthButtonsState {
tunread: string[]; tunread: string[];
tunreadUser: string[]; tunreadUser: string[];
tunreadGroup: string[]; tunreadGroup: string[];
canToggleEncryption: boolean;
} }
class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsState> { class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsState> {
@ -70,7 +72,8 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
isFollowingThread: true, isFollowingThread: true,
tunread: [], tunread: [],
tunreadUser: [], tunreadUser: [],
tunreadGroup: [] tunreadGroup: [],
canToggleEncryption: false
}; };
} }
@ -94,10 +97,11 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
console.log("Can't find subscription to observe."); console.log("Can't find subscription to observe.");
} }
} }
this.setCanToggleEncryption();
} }
shouldComponentUpdate(nextProps: IRightButtonsProps, nextState: IRigthButtonsState) { shouldComponentUpdate(nextProps: IRightButtonsProps, nextState: IRigthButtonsState) {
const { isFollowingThread, tunread, tunreadUser, tunreadGroup } = this.state; const { isFollowingThread, tunread, tunreadUser, tunreadGroup, canToggleEncryption } = this.state;
const { const {
teamId, teamId,
status, status,
@ -106,7 +110,8 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
theme, theme,
hasE2EEWarning, hasE2EEWarning,
issuesWithNotifications, issuesWithNotifications,
notificationsDisabled notificationsDisabled,
toggleRoomE2EEncryptionPermission
} = this.props; } = this.props;
if (nextProps.teamId !== teamId) { if (nextProps.teamId !== teamId) {
return true; return true;
@ -120,6 +125,9 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
if (nextProps.theme !== theme) { if (nextProps.theme !== theme) {
return true; return true;
} }
if (nextState.canToggleEncryption !== canToggleEncryption) {
return true;
}
if (nextState.isFollowingThread !== isFollowingThread) { if (nextState.isFollowingThread !== isFollowingThread) {
return true; return true;
} }
@ -144,9 +152,19 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
if (!dequal(nextState.tunreadGroup, tunreadGroup)) { if (!dequal(nextState.tunreadGroup, tunreadGroup)) {
return true; return true;
} }
if (!dequal(nextProps.toggleRoomE2EEncryptionPermission, toggleRoomE2EEncryptionPermission)) {
return true;
}
return false; return false;
} }
componentDidUpdate(prevProps: Readonly<IRightButtonsProps>): void {
const { toggleRoomE2EEncryptionPermission } = this.props;
if (prevProps.toggleRoomE2EEncryptionPermission !== toggleRoomE2EEncryptionPermission) {
this.setCanToggleEncryption();
}
}
componentWillUnmount() { componentWillUnmount() {
if (this.threadSubscription && this.threadSubscription.unsubscribe) { if (this.threadSubscription && this.threadSubscription.unsubscribe) {
this.threadSubscription.unsubscribe(); this.threadSubscription.unsubscribe();
@ -317,6 +335,15 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
showActionSheet({ options }); showActionSheet({ options });
}; };
setCanToggleEncryption = async () => {
const { rid } = this.props;
const { toggleRoomE2EEncryptionPermission } = this.props;
const permissions = await hasPermission([toggleRoomE2EEncryptionPermission], rid);
const canToggleEncryption = permissions[0];
this.setState({ canToggleEncryption });
};
navigateToNotificationOrPushTroubleshoot = () => { navigateToNotificationOrPushTroubleshoot = () => {
const { room } = this; const { room } = this;
const { rid, navigation, isMasterDetail, issuesWithNotifications } = this.props; const { rid, navigation, isMasterDetail, issuesWithNotifications } = this.props;
@ -374,7 +401,7 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
}; };
render() { render() {
const { isFollowingThread, tunread, tunreadUser, tunreadGroup } = this.state; const { isFollowingThread, tunread, tunreadUser, tunreadGroup, canToggleEncryption } = this.state;
const { t, tmid, threadsEnabled, rid, colors, issuesWithNotifications, notificationsDisabled, hasE2EEWarning } = this.props; const { t, tmid, threadsEnabled, rid, colors, issuesWithNotifications, notificationsDisabled, hasE2EEWarning } = this.props;
if (!rid) { if (!rid) {
@ -404,7 +431,9 @@ class RightButtonsContainer extends Component<IRightButtonsProps, IRigthButtonsS
} }
return ( return (
<HeaderButton.Container> <HeaderButton.Container>
{hasE2EEWarning ? <HeaderButton.Item iconName='encrypted' onPress={() => toggleRoomE2EE(rid)} /> : null} {hasE2EEWarning ? (
<HeaderButton.Item iconName='encrypted' onPress={() => toggleRoomE2EE(rid)} disabled={!canToggleEncryption} />
) : null}
{issuesWithNotifications || notificationsDisabled ? ( {issuesWithNotifications || notificationsDisabled ? (
<HeaderButton.Item <HeaderButton.Item
color={issuesWithNotifications ? colors!.fontDanger : ''} color={issuesWithNotifications ? colors!.fontDanger : ''}
@ -435,7 +464,8 @@ const mapStateToProps = (state: IApplicationState) => ({
threadsEnabled: state.settings.Threads_enabled as boolean, threadsEnabled: state.settings.Threads_enabled as boolean,
isMasterDetail: state.app.isMasterDetail, isMasterDetail: state.app.isMasterDetail,
livechatRequestComment: state.settings.Livechat_request_comment_when_closing_conversation as boolean, livechatRequestComment: state.settings.Livechat_request_comment_when_closing_conversation as boolean,
issuesWithNotifications: state.troubleshootingNotification.issuesWithNotifications issuesWithNotifications: state.troubleshootingNotification.issuesWithNotifications,
toggleRoomE2EEncryptionPermission: state.permissions['toggle-room-e2e-encryption']
}); });
export default connect(mapStateToProps)(withTheme(RightButtonsContainer)); export default connect(mapStateToProps)(withTheme(RightButtonsContainer));