From 3ffe752730bfc8bb8b8e9930de807860c5ca02da Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 29 Nov 2021 11:06:19 +0000 Subject: [PATCH] Fix RoomUpgradeWarningBar exploding (#7214) --- .../views/rooms/RoomUpgradeWarningBar.tsx | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/components/views/rooms/RoomUpgradeWarningBar.tsx b/src/components/views/rooms/RoomUpgradeWarningBar.tsx index eb334ab825..b9fdc1111c 100644 --- a/src/components/views/rooms/RoomUpgradeWarningBar.tsx +++ b/src/components/views/rooms/RoomUpgradeWarningBar.tsx @@ -22,10 +22,10 @@ import { RoomState } from 'matrix-js-sdk/src/models/room-state'; import Modal from '../../../Modal'; import { _t } from '../../../languageHandler'; -import { MatrixClientPeg } from "../../../MatrixClientPeg"; import { replaceableComponent } from "../../../utils/replaceableComponent"; import RoomUpgradeDialog from '../dialogs/RoomUpgradeDialog'; import AccessibleButton from '../elements/AccessibleButton'; +import MatrixClientContext from "../../../contexts/MatrixClientContext"; interface IProps { room: Room; @@ -37,18 +37,24 @@ interface IState { @replaceableComponent("views.rooms.RoomUpgradeWarningBar") export default class RoomUpgradeWarningBar extends React.PureComponent { - public componentDidMount(): void { - const tombstone = this.props.room.currentState.getStateEvents("m.room.tombstone", ""); - this.setState({ upgraded: tombstone && tombstone.getContent().replacement_room }); + static contextType = MatrixClientContext; + public context!: React.ContextType; - MatrixClientPeg.get().on("RoomState.events", this.onStateEvents); + constructor(props, context) { + super(props, context); + + const tombstone = this.props.room.currentState.getStateEvents("m.room.tombstone", ""); + this.state = { + upgraded: tombstone?.getContent().replacement_room, + }; + } + + public componentDidMount(): void { + this.context.on("RoomState.events", this.onStateEvents); } public componentWillUnmount(): void { - const cli = MatrixClientPeg.get(); - if (cli) { - cli.removeListener("RoomState.events", this.onStateEvents); - } + this.context.removeListener("RoomState.events", this.onStateEvents); } private onStateEvents = (event: MatrixEvent, state: RoomState): void => {