Convert AdvancedRoomSettingsTab to Typescript

This commit is contained in:
Michael Telatynski 2021-06-08 16:28:02 +01:00
parent 4725a9e8fa
commit 9454a4e6c7

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2019 New Vector Ltd Copyright 2019, 2021 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -15,68 +15,75 @@ limitations under the License.
*/ */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import {_t} from "../../../../../languageHandler"; import { _t } from "../../../../../languageHandler";
import {MatrixClientPeg} from "../../../../../MatrixClientPeg"; import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
import * as sdk from "../../../../..";
import AccessibleButton from "../../../elements/AccessibleButton"; import AccessibleButton from "../../../elements/AccessibleButton";
import RoomUpgradeDialog from "../../../dialogs/RoomUpgradeDialog";
import DevtoolsDialog from "../../../dialogs/DevtoolsDialog";
import Modal from "../../../../../Modal"; import Modal from "../../../../../Modal";
import dis from "../../../../../dispatcher/dispatcher"; import dis from "../../../../../dispatcher/dispatcher";
import {replaceableComponent} from "../../../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../../../utils/replaceableComponent";
interface IProps {
roomId: string;
closeSettingsFn(): void;
}
interface IRecommendedVersion {
version: string;
needsUpgrade: boolean;
urgent: boolean;
}
interface IState {
upgradeRecommendation?: IRecommendedVersion;
oldRoomId?: string;
oldEventId?: string;
upgraded?: boolean;
}
@replaceableComponent("views.settings.tabs.room.AdvancedRoomSettingsTab") @replaceableComponent("views.settings.tabs.room.AdvancedRoomSettingsTab")
export default class AdvancedRoomSettingsTab extends React.Component { export default class AdvancedRoomSettingsTab extends React.Component<IProps, IState> {
static propTypes = { constructor(props, context) {
roomId: PropTypes.string.isRequired, super(props, context);
closeSettingsFn: PropTypes.func.isRequired,
};
constructor(props) {
super(props);
this.state = { this.state = {
// This is eventually set to the value of room.getRecommendedVersion() // This is eventually set to the value of room.getRecommendedVersion()
upgradeRecommendation: null, upgradeRecommendation: null,
}; };
}
// TODO: [REACT-WARNING] Move this to constructor
UNSAFE_componentWillMount() { // eslint-disable-line camelcase
// we handle lack of this object gracefully later, so don't worry about it failing here. // we handle lack of this object gracefully later, so don't worry about it failing here.
const room = MatrixClientPeg.get().getRoom(this.props.roomId); const room = MatrixClientPeg.get().getRoom(this.props.roomId);
room.getRecommendedVersion().then((v) => { room.getRecommendedVersion().then((v) => {
const tombstone = room.currentState.getStateEvents("m.room.tombstone", ""); const tombstone = room.currentState.getStateEvents("m.room.tombstone", "");
const additionalStateChanges = {}; const additionalStateChanges: Partial<IState> = {};
const createEvent = room.currentState.getStateEvents("m.room.create", ""); const createEvent = room.currentState.getStateEvents("m.room.create", "");
const predecessor = createEvent ? createEvent.getContent().predecessor : null; const predecessor = createEvent ? createEvent.getContent().predecessor : null;
if (predecessor && predecessor.room_id) { if (predecessor && predecessor.room_id) {
additionalStateChanges['oldRoomId'] = predecessor.room_id; additionalStateChanges.oldRoomId = predecessor.room_id;
additionalStateChanges['oldEventId'] = predecessor.event_id; additionalStateChanges.oldEventId = predecessor.event_id;
additionalStateChanges['hasPreviousRoom'] = true;
} }
this.setState({ this.setState({
upgraded: tombstone && tombstone.getContent().replacement_room, upgraded: !!tombstone?.getContent().replacement_room,
upgradeRecommendation: v, upgradeRecommendation: v,
...additionalStateChanges, ...additionalStateChanges,
}); });
}); });
} }
_upgradeRoom = (e) => { private upgradeRoom = (e) => {
const RoomUpgradeDialog = sdk.getComponent('dialogs.RoomUpgradeDialog');
const room = MatrixClientPeg.get().getRoom(this.props.roomId); const room = MatrixClientPeg.get().getRoom(this.props.roomId);
Modal.createTrackedDialog('Upgrade Room Version', '', RoomUpgradeDialog, {room: room}); Modal.createTrackedDialog('Upgrade Room Version', '', RoomUpgradeDialog, {room: room});
}; };
_openDevtools = (e) => { private openDevtools = (e) => {
const DevtoolsDialog = sdk.getComponent('dialogs.DevtoolsDialog');
Modal.createDialog(DevtoolsDialog, {roomId: this.props.roomId}); Modal.createDialog(DevtoolsDialog, {roomId: this.props.roomId});
}; };
_onOldRoomClicked = (e) => { private onOldRoomClicked = (e) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -113,7 +120,7 @@ export default class AdvancedRoomSettingsTab extends React.Component {
}, },
)} )}
</p> </p>
<AccessibleButton onClick={this._upgradeRoom} kind='primary'> <AccessibleButton onClick={this.upgradeRoom} kind='primary'>
{_t("Upgrade this room to the recommended room version")} {_t("Upgrade this room to the recommended room version")}
</AccessibleButton> </AccessibleButton>
</div> </div>
@ -121,13 +128,13 @@ export default class AdvancedRoomSettingsTab extends React.Component {
} }
let oldRoomLink; let oldRoomLink;
if (this.state.hasPreviousRoom) { if (this.state.oldRoomId) {
let name = _t("this room"); let name = _t("this room");
const room = MatrixClientPeg.get().getRoom(this.props.roomId); const room = MatrixClientPeg.get().getRoom(this.props.roomId);
if (room && room.name) name = room.name; if (room && room.name) name = room.name;
oldRoomLink = ( oldRoomLink = (
<AccessibleButton element='a' onClick={this._onOldRoomClicked}> <AccessibleButton element='a' onClick={this.onOldRoomClicked}>
{_t("View older messages in %(roomName)s.", {roomName: name})} { _t("View older messages in %(roomName)s.", { roomName: name }) }
</AccessibleButton> </AccessibleButton>
); );
} }
@ -139,23 +146,23 @@ export default class AdvancedRoomSettingsTab extends React.Component {
<span className='mx_SettingsTab_subheading'>{_t("Room information")}</span> <span className='mx_SettingsTab_subheading'>{_t("Room information")}</span>
<div> <div>
<span>{_t("Internal room ID:")}</span>&nbsp; <span>{_t("Internal room ID:")}</span>&nbsp;
{this.props.roomId} { this.props.roomId }
</div> </div>
{unfederatableSection} { unfederatableSection }
</div> </div>
<div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'> <div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'>
<span className='mx_SettingsTab_subheading'>{_t("Room version")}</span> <span className='mx_SettingsTab_subheading'>{_t("Room version")}</span>
<div> <div>
<span>{_t("Room version:")}</span>&nbsp; <span>{_t("Room version:")}</span>&nbsp;
{room.getVersion()} { room.getVersion() }
</div> </div>
{oldRoomLink} { oldRoomLink }
{roomUpgradeButton} { roomUpgradeButton }
</div> </div>
<div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'> <div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'>
<span className='mx_SettingsTab_subheading'>{_t("Developer options")}</span> <span className='mx_SettingsTab_subheading'>{ _t("Developer options") }</span>
<AccessibleButton onClick={this._openDevtools} kind='primary'> <AccessibleButton onClick={this.openDevtools} kind='primary'>
{_t("Open Devtools")} { _t("Open Devtools") }
</AccessibleButton> </AccessibleButton>
</div> </div>
</div> </div>