From bcb45f1447ac464b61c3268c1afef8c4ab5aadd0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 14 Apr 2022 10:38:51 +0100 Subject: [PATCH] Refactor SecurityRoomSettingsTab and remove unused state (#8306) --- .../tabs/room/SecurityRoomSettingsTab.tsx | 91 +++++++------------ 1 file changed, 35 insertions(+), 56 deletions(-) diff --git a/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx b/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx index b9e2d94594..ba79cbd875 100644 --- a/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx +++ b/src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx @@ -1,5 +1,5 @@ /* -Copyright 2019-2021 The Matrix.org Foundation C.I.C. +Copyright 2019-2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,14 +15,13 @@ limitations under the License. */ import React from 'react'; -import { GuestAccess, HistoryVisibility, JoinRule, RestrictedAllowType } from "matrix-js-sdk/src/@types/partials"; +import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { EventType } from 'matrix-js-sdk/src/@types/event'; import { logger } from "matrix-js-sdk/src/logger"; import { _t } from "../../../../../languageHandler"; -import { MatrixClientPeg } from "../../../../../MatrixClientPeg"; import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch"; import Modal from "../../../../../Modal"; import QuestionDialog from "../../../dialogs/QuestionDialog"; @@ -39,6 +38,7 @@ import ErrorDialog from "../../../dialogs/ErrorDialog"; import SettingsFieldset from '../../SettingsFieldset'; import ExternalLink from '../../../elements/ExternalLink'; import PosthogTrackers from "../../../../../PosthogTrackers"; +import MatrixClientContext from "../../../../../contexts/MatrixClientContext"; interface IProps { roomId: string; @@ -46,7 +46,6 @@ interface IProps { } interface IState { - restrictedAllowRoomIds?: string[]; guestAccess: GuestAccess; history: HistoryVisibility; hasAliases: boolean; @@ -55,52 +54,33 @@ interface IState { } export default class SecurityRoomSettingsTab extends React.Component { - constructor(props) { - super(props); + static contextType = MatrixClientContext; + public context!: React.ContextType; + + constructor(props, context) { + super(props, context); + + const state = context.getRoom(this.props.roomId).currentState; this.state = { - guestAccess: GuestAccess.Forbidden, - history: HistoryVisibility.Shared, - hasAliases: false, - encrypted: false, + guestAccess: this.pullContentPropertyFromEvent( + state.getStateEvents(EventType.RoomGuestAccess, ""), + 'guest_access', + GuestAccess.Forbidden, + ), + history: this.pullContentPropertyFromEvent( + state.getStateEvents(EventType.RoomHistoryVisibility, ""), + 'history_visibility', + HistoryVisibility.Shared, + ), + hasAliases: false, // async loaded in componentDidMount + encrypted: context.isRoomEncrypted(this.props.roomId), showAdvancedSection: false, }; } - // TODO: [REACT-WARNING] Move this to constructor - UNSAFE_componentWillMount() { // eslint-disable-line - const cli = MatrixClientPeg.get(); - cli.on(RoomStateEvent.Events, this.onStateEvent); - - const room = cli.getRoom(this.props.roomId); - const state = room.currentState; - - const joinRuleEvent = state.getStateEvents(EventType.RoomJoinRules, ""); - const joinRule: JoinRule = this.pullContentPropertyFromEvent( - joinRuleEvent, - 'join_rule', - JoinRule.Invite, - ); - const restrictedAllowRoomIds = joinRule === JoinRule.Restricted - ? joinRuleEvent?.getContent().allow - ?.filter(a => a.type === RestrictedAllowType.RoomMembership) - ?.map(a => a.room_id) - : undefined; - - const guestAccess: GuestAccess = this.pullContentPropertyFromEvent( - state.getStateEvents(EventType.RoomGuestAccess, ""), - 'guest_access', - GuestAccess.Forbidden, - ); - const history: HistoryVisibility = this.pullContentPropertyFromEvent( - state.getStateEvents(EventType.RoomHistoryVisibility, ""), - 'history_visibility', - HistoryVisibility.Shared, - ); - - const encrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.roomId); - this.setState({ restrictedAllowRoomIds, guestAccess, history, encrypted }); - + componentDidMount() { + this.context.on(RoomStateEvent.Events, this.onStateEvent); this.hasAliases().then(hasAliases => this.setState({ hasAliases })); } @@ -109,7 +89,7 @@ export default class SecurityRoomSettingsTab extends React.Component { @@ -123,7 +103,7 @@ export default class SecurityRoomSettingsTab extends React.Component { - if (MatrixClientPeg.get().getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) { + if (this.context.getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) { const dialog = Modal.createTrackedDialog('Confirm Public Encrypted Room', '', QuestionDialog, { title: _t('Are you sure you want to add encryption to this public room?'), description:
@@ -178,7 +158,7 @@ export default class SecurityRoomSettingsTab extends React.Component { @@ -196,8 +176,7 @@ export default class SecurityRoomSettingsTab extends React.Component { logger.error(e); @@ -227,7 +206,7 @@ export default class SecurityRoomSettingsTab extends React.Component { logger.error(e); @@ -236,11 +215,11 @@ export default class SecurityRoomSettingsTab extends React.Component { - MatrixClientPeg.get().getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked); + this.context.getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked); }; private async hasAliases(): Promise { - const cli = MatrixClientPeg.get(); + const cli = this.context; if (await cli.doesServerSupportUnstableFeature("org.matrix.msc2432")) { const response = await cli.unstableGetLocalAliases(this.props.roomId); const localAliases = response.aliases; @@ -254,7 +233,7 @@ export default class SecurityRoomSettingsTab extends React.Component