Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit abb86ce

Browse files
t3chguyKerry Archibald
authored andcommitted
Refactor SecurityRoomSettingsTab and remove unused state (#8306)
1 parent 05a1f67 commit abb86ce

File tree

1 file changed

+35
-56
lines changed

1 file changed

+35
-56
lines changed

src/components/views/settings/tabs/room/SecurityRoomSettingsTab.tsx

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
2+
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -15,14 +15,13 @@ limitations under the License.
1515
*/
1616

1717
import React from 'react';
18-
import { GuestAccess, HistoryVisibility, JoinRule, RestrictedAllowType } from "matrix-js-sdk/src/@types/partials";
18+
import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials";
1919
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
2020
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
2121
import { EventType } from 'matrix-js-sdk/src/@types/event';
2222
import { logger } from "matrix-js-sdk/src/logger";
2323

2424
import { _t } from "../../../../../languageHandler";
25-
import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
2625
import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch";
2726
import Modal from "../../../../../Modal";
2827
import QuestionDialog from "../../../dialogs/QuestionDialog";
@@ -39,14 +38,14 @@ import ErrorDialog from "../../../dialogs/ErrorDialog";
3938
import SettingsFieldset from '../../SettingsFieldset';
4039
import ExternalLink from '../../../elements/ExternalLink';
4140
import PosthogTrackers from "../../../../../PosthogTrackers";
41+
import MatrixClientContext from "../../../../../contexts/MatrixClientContext";
4242

4343
interface IProps {
4444
roomId: string;
4545
closeSettingsFn: () => void;
4646
}
4747

4848
interface IState {
49-
restrictedAllowRoomIds?: string[];
5049
guestAccess: GuestAccess;
5150
history: HistoryVisibility;
5251
hasAliases: boolean;
@@ -55,52 +54,33 @@ interface IState {
5554
}
5655

5756
export default class SecurityRoomSettingsTab extends React.Component<IProps, IState> {
58-
constructor(props) {
59-
super(props);
57+
static contextType = MatrixClientContext;
58+
public context!: React.ContextType<typeof MatrixClientContext>;
59+
60+
constructor(props, context) {
61+
super(props, context);
62+
63+
const state = context.getRoom(this.props.roomId).currentState;
6064

6165
this.state = {
62-
guestAccess: GuestAccess.Forbidden,
63-
history: HistoryVisibility.Shared,
64-
hasAliases: false,
65-
encrypted: false,
66+
guestAccess: this.pullContentPropertyFromEvent<GuestAccess>(
67+
state.getStateEvents(EventType.RoomGuestAccess, ""),
68+
'guest_access',
69+
GuestAccess.Forbidden,
70+
),
71+
history: this.pullContentPropertyFromEvent<HistoryVisibility>(
72+
state.getStateEvents(EventType.RoomHistoryVisibility, ""),
73+
'history_visibility',
74+
HistoryVisibility.Shared,
75+
),
76+
hasAliases: false, // async loaded in componentDidMount
77+
encrypted: context.isRoomEncrypted(this.props.roomId),
6678
showAdvancedSection: false,
6779
};
6880
}
6981

70-
// TODO: [REACT-WARNING] Move this to constructor
71-
UNSAFE_componentWillMount() { // eslint-disable-line
72-
const cli = MatrixClientPeg.get();
73-
cli.on(RoomStateEvent.Events, this.onStateEvent);
74-
75-
const room = cli.getRoom(this.props.roomId);
76-
const state = room.currentState;
77-
78-
const joinRuleEvent = state.getStateEvents(EventType.RoomJoinRules, "");
79-
const joinRule: JoinRule = this.pullContentPropertyFromEvent<JoinRule>(
80-
joinRuleEvent,
81-
'join_rule',
82-
JoinRule.Invite,
83-
);
84-
const restrictedAllowRoomIds = joinRule === JoinRule.Restricted
85-
? joinRuleEvent?.getContent().allow
86-
?.filter(a => a.type === RestrictedAllowType.RoomMembership)
87-
?.map(a => a.room_id)
88-
: undefined;
89-
90-
const guestAccess: GuestAccess = this.pullContentPropertyFromEvent<GuestAccess>(
91-
state.getStateEvents(EventType.RoomGuestAccess, ""),
92-
'guest_access',
93-
GuestAccess.Forbidden,
94-
);
95-
const history: HistoryVisibility = this.pullContentPropertyFromEvent<HistoryVisibility>(
96-
state.getStateEvents(EventType.RoomHistoryVisibility, ""),
97-
'history_visibility',
98-
HistoryVisibility.Shared,
99-
);
100-
101-
const encrypted = MatrixClientPeg.get().isRoomEncrypted(this.props.roomId);
102-
this.setState({ restrictedAllowRoomIds, guestAccess, history, encrypted });
103-
82+
componentDidMount() {
83+
this.context.on(RoomStateEvent.Events, this.onStateEvent);
10484
this.hasAliases().then(hasAliases => this.setState({ hasAliases }));
10585
}
10686

@@ -109,7 +89,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
10989
}
11090

11191
componentWillUnmount() {
112-
MatrixClientPeg.get().removeListener(RoomStateEvent.Events, this.onStateEvent);
92+
this.context.removeListener(RoomStateEvent.Events, this.onStateEvent);
11393
}
11494

11595
private onStateEvent = (e: MatrixEvent) => {
@@ -123,7 +103,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
123103
};
124104

125105
private onEncryptionChange = async () => {
126-
if (MatrixClientPeg.get().getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) {
106+
if (this.context.getRoom(this.props.roomId)?.getJoinRule() === JoinRule.Public) {
127107
const dialog = Modal.createTrackedDialog('Confirm Public Encrypted Room', '', QuestionDialog, {
128108
title: _t('Are you sure you want to add encryption to this public room?'),
129109
description: <div>
@@ -178,7 +158,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
178158

179159
const beforeEncrypted = this.state.encrypted;
180160
this.setState({ encrypted: true });
181-
MatrixClientPeg.get().sendStateEvent(
161+
this.context.sendStateEvent(
182162
this.props.roomId, EventType.RoomEncryption,
183163
{ algorithm: "m.megolm.v1.aes-sha2" },
184164
).catch((e) => {
@@ -196,8 +176,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
196176

197177
this.setState({ guestAccess });
198178

199-
const client = MatrixClientPeg.get();
200-
client.sendStateEvent(this.props.roomId, EventType.RoomGuestAccess, {
179+
this.context.sendStateEvent(this.props.roomId, EventType.RoomGuestAccess, {
201180
guest_access: guestAccess,
202181
}, "").catch((e) => {
203182
logger.error(e);
@@ -227,7 +206,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
227206
if (beforeHistory === history) return;
228207

229208
this.setState({ history: history });
230-
MatrixClientPeg.get().sendStateEvent(this.props.roomId, EventType.RoomHistoryVisibility, {
209+
this.context.sendStateEvent(this.props.roomId, EventType.RoomHistoryVisibility, {
231210
history_visibility: history,
232211
}, "").catch((e) => {
233212
logger.error(e);
@@ -236,11 +215,11 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
236215
};
237216

238217
private updateBlacklistDevicesFlag = (checked: boolean) => {
239-
MatrixClientPeg.get().getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked);
218+
this.context.getRoom(this.props.roomId).setBlacklistUnverifiedDevices(checked);
240219
};
241220

242221
private async hasAliases(): Promise<boolean> {
243-
const cli = MatrixClientPeg.get();
222+
const cli = this.context;
244223
if (await cli.doesServerSupportUnstableFeature("org.matrix.msc2432")) {
245224
const response = await cli.unstableGetLocalAliases(this.props.roomId);
246225
const localAliases = response.aliases;
@@ -254,7 +233,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
254233
}
255234

256235
private renderJoinRule() {
257-
const client = MatrixClientPeg.get();
236+
const client = this.context;
258237
const room = client.getRoom(this.props.roomId);
259238

260239
let aliasWarning = null;
@@ -333,7 +312,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
333312
return null;
334313
}
335314

336-
const client = MatrixClientPeg.get();
315+
const client = this.context;
337316
const history = this.state.history;
338317
const state = client.getRoom(this.props.roomId).currentState;
339318
const canChangeHistory = state.mayClientSendStateEvent(EventType.RoomHistoryVisibility, client);
@@ -380,7 +359,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
380359
};
381360

382361
private renderAdvanced() {
383-
const client = MatrixClientPeg.get();
362+
const client = this.context;
384363
const guestAccess = this.state.guestAccess;
385364
const state = client.getRoom(this.props.roomId).currentState;
386365
const canSetGuestAccess = state.mayClientSendStateEvent(EventType.RoomGuestAccess, client);
@@ -400,7 +379,7 @@ export default class SecurityRoomSettingsTab extends React.Component<IProps, ISt
400379
}
401380

402381
render() {
403-
const client = MatrixClientPeg.get();
382+
const client = this.context;
404383
const room = client.getRoom(this.props.roomId);
405384
const isEncrypted = this.state.encrypted;
406385
const hasEncryptionPermission = room.currentState.mayClientSendStateEvent(EventType.RoomEncryption, client);

0 commit comments

Comments
 (0)