mirror of https://github.com/vector-im/riot-web
Convert EmbeddedPage to TS
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>pull/21833/head
parent
716aba2a0e
commit
e882576f86
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
|
||||||
import request from 'browser-request';
|
import request from 'browser-request';
|
||||||
import { _t } from '../../languageHandler';
|
import { _t } from '../../languageHandler';
|
||||||
import sanitizeHtml from 'sanitize-html';
|
import sanitizeHtml from 'sanitize-html';
|
||||||
|
@ -27,37 +26,43 @@ import classnames from 'classnames';
|
||||||
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
import MatrixClientContext from "../../contexts/MatrixClientContext";
|
||||||
import AutoHideScrollbar from "./AutoHideScrollbar";
|
import AutoHideScrollbar from "./AutoHideScrollbar";
|
||||||
|
|
||||||
export default class EmbeddedPage extends React.PureComponent {
|
interface IProps {
|
||||||
static propTypes = {
|
// URL to request embedded page content from
|
||||||
// URL to request embedded page content from
|
url?: string;
|
||||||
url: PropTypes.string,
|
// Class name prefix to apply for a given instance
|
||||||
// Class name prefix to apply for a given instance
|
className?: string;
|
||||||
className: PropTypes.string,
|
// Whether to wrap the page in a scrollbar
|
||||||
// Whether to wrap the page in a scrollbar
|
scrollbar?: boolean;
|
||||||
scrollbar: PropTypes.bool,
|
// Map of keys to replace with values, e.g {$placeholder: "value"}
|
||||||
// Map of keys to replace with values, e.g {$placeholder: "value"}
|
replaceMap?: Map<string, string>;
|
||||||
replaceMap: PropTypes.object,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
static contextType = MatrixClientContext;
|
interface IState {
|
||||||
|
page: string;
|
||||||
|
}
|
||||||
|
|
||||||
constructor(props, context) {
|
export default class EmbeddedPage extends React.PureComponent<IProps, IState> {
|
||||||
|
public static contextType = MatrixClientContext;
|
||||||
|
private unmounted = true;
|
||||||
|
private dispatcherRef: string;
|
||||||
|
|
||||||
|
constructor(props: IProps, context: typeof MatrixClientContext) {
|
||||||
super(props, context);
|
super(props, context);
|
||||||
|
|
||||||
this._dispatcherRef = null;
|
this.dispatcherRef = null;
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
page: '',
|
page: '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
translate(s) {
|
private translate(s: string): string {
|
||||||
// default implementation - skins may wish to extend this
|
// default implementation - skins may wish to extend this
|
||||||
return sanitizeHtml(_t(s));
|
return sanitizeHtml(_t(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
public componentDidMount(): void {
|
||||||
this._unmounted = false;
|
this.unmounted = false;
|
||||||
|
|
||||||
if (!this.props.url) {
|
if (!this.props.url) {
|
||||||
return;
|
return;
|
||||||
|
@ -70,7 +75,7 @@ export default class EmbeddedPage extends React.PureComponent {
|
||||||
request(
|
request(
|
||||||
{ method: "GET", url: this.props.url },
|
{ method: "GET", url: this.props.url },
|
||||||
(err, response, body) => {
|
(err, response, body) => {
|
||||||
if (this._unmounted) {
|
if (this.unmounted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,22 +97,22 @@ export default class EmbeddedPage extends React.PureComponent {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
this._dispatcherRef = dis.register(this.onAction);
|
this.dispatcherRef = dis.register(this.onAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
public componentWillUnmount(): void {
|
||||||
this._unmounted = true;
|
this.unmounted = true;
|
||||||
if (this._dispatcherRef !== null) dis.unregister(this._dispatcherRef);
|
if (this.dispatcherRef !== null) dis.unregister(this.dispatcherRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
onAction = (payload) => {
|
private onAction = (payload): void => {
|
||||||
// HACK: Workaround for the context's MatrixClient not being set up at render time.
|
// HACK: Workaround for the context's MatrixClient not being set up at render time.
|
||||||
if (payload.action === 'client_started') {
|
if (payload.action === 'client_started') {
|
||||||
this.forceUpdate();
|
this.forceUpdate();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
public render(): JSX.Element {
|
||||||
// HACK: Workaround for the context's MatrixClient not updating.
|
// HACK: Workaround for the context's MatrixClient not updating.
|
||||||
const client = this.context || MatrixClientPeg.get();
|
const client = this.context || MatrixClientPeg.get();
|
||||||
const isGuest = client ? client.isGuest() : true;
|
const isGuest = client ? client.isGuest() : true;
|
Loading…
Reference in New Issue