Migrate IntegrationManager to TypeScript

pull/21833/head
Germain Souquet 2021-08-14 11:27:17 +02:00
parent dfd986751f
commit 447beb8294
1 changed files with 32 additions and 29 deletions

View File

@ -1,6 +1,7 @@
/* /*
Copyright 2015, 2016 OpenMarket Ltd Copyright 2015, 2016 OpenMarket Ltd
Copyright 2019 The Matrix.org Foundation C.I.C. Copyright 2019 The Matrix.org Foundation C.I.C.
Copyright 2021 New Vector Ltd
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.
@ -16,53 +17,55 @@ limitations under the License.
*/ */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import * as sdk from '../../../index'; import * as sdk from '../../../index';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import dis from '../../../dispatcher/dispatcher'; import dis from '../../../dispatcher/dispatcher';
import { Key } from "../../../Keyboard"; import { Key } from "../../../Keyboard";
import { replaceableComponent } from "../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../utils/replaceableComponent";
import { ActionPayload } from '../../../dispatcher/payloads';
interface IProps {
// false to display an error saying that we couldn't connect to the integration manager
connected: boolean;
// true to display a loading spinner
loading: boolean;
// The source URL to load
url?: string;
// callback when the manager is dismissed
onFinished: () => void;
}
interface IState {
errored: boolean;
}
@replaceableComponent("views.settings.IntegrationManager") @replaceableComponent("views.settings.IntegrationManager")
export default class IntegrationManager extends React.Component { export default class IntegrationManager extends React.Component<IProps, IState> {
static propTypes = { private dispatcherRef: string;
// false to display an error saying that we couldn't connect to the integration manager
connected: PropTypes.bool.isRequired,
// true to display a loading spinner public static defaultProps = {
loading: PropTypes.bool.isRequired,
// The source URL to load
url: PropTypes.string,
// callback when the manager is dismissed
onFinished: PropTypes.func.isRequired,
};
static defaultProps = {
connected: true, connected: true,
loading: false, loading: false,
}; };
constructor(props) { public state = {
super(props); errored: false,
};
this.state = { public componentDidMount(): void {
errored: false,
};
}
componentDidMount() {
this.dispatcherRef = dis.register(this.onAction); this.dispatcherRef = dis.register(this.onAction);
document.addEventListener("keydown", this.onKeyDown); document.addEventListener("keydown", this.onKeyDown);
} }
componentWillUnmount() { public componentWillUnmount(): void {
dis.unregister(this.dispatcherRef); dis.unregister(this.dispatcherRef);
document.removeEventListener("keydown", this.onKeyDown); document.removeEventListener("keydown", this.onKeyDown);
} }
onKeyDown = (ev) => { private onKeyDown = (ev: KeyboardEvent): void => {
if (ev.key === Key.ESCAPE) { if (ev.key === Key.ESCAPE) {
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
@ -70,17 +73,17 @@ export default class IntegrationManager extends React.Component {
} }
}; };
onAction = (payload) => { private onAction = (payload: ActionPayload): void => {
if (payload.action === 'close_scalar') { if (payload.action === 'close_scalar') {
this.props.onFinished(); this.props.onFinished();
} }
}; };
onError = () => { private onError = (): void => {
this.setState({ errored: true }); this.setState({ errored: true });
}; };
render() { public render(): JSX.Element {
if (this.props.loading) { if (this.props.loading) {
const Spinner = sdk.getComponent("elements.Spinner"); const Spinner = sdk.getComponent("elements.Spinner");
return ( return (