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

Commit 74751cd

Browse files
committed
TS conversion
Signed-off-by: Šimon Brandner <[email protected]>
1 parent 66a4776 commit 74751cd

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

src/components/views/messages/MFileBody.js renamed to src/components/views/messages/MFileBody.tsx

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright 2015, 2016, 2018, 2021 The Matrix.org Foundation C.I.C.
3+
Copyright 2021 Šimon Brandner <[email protected]>
34
45
Licensed under the Apache License, Version 2.0 (the "License");
56
you may not use this file except in compliance with the License.
@@ -14,17 +15,17 @@ See the License for the specific language governing permissions and
1415
limitations under the License.
1516
*/
1617

17-
import React, {createRef} from 'react';
18-
import PropTypes from 'prop-types';
18+
import React from 'react';
1919
import filesize from 'filesize';
2020
import { _t } from '../../../languageHandler';
21-
import {decryptFile} from '../../../utils/DecryptFile';
21+
import { decryptFile } from '../../../utils/DecryptFile';
2222
import Modal from '../../../Modal';
23-
import AccessibleButton from "../elements/AccessibleButton";
24-
import {replaceableComponent} from "../../../utils/replaceableComponent";
25-
import {mediaFromContent} from "../../../customisations/Media";
23+
import { replaceableComponent } from "../../../utils/replaceableComponent";
24+
import { mediaFromContent } from "../../../customisations/Media";
25+
import AccessibleButton from '../elements/AccessibleButton';
2626
import ErrorDialog from "../dialogs/ErrorDialog";
2727
import AccessibleTooltipButton from '../elements/AccessibleTooltipButton';
28+
import { MatrixEvent } from 'matrix-js-sdk';
2829

2930
// User supplied content can contain scripts, we have to be careful that
3031
// we don't accidentally run those script within the same origin as the
@@ -56,22 +57,20 @@ import AccessibleTooltipButton from '../elements/AccessibleTooltipButton';
5657
// the downside of using a sandboxed iframe is that the browers are overly
5758
// restrictive in what you are allowed to do with the generated URL.
5859

59-
@replaceableComponent("views.messages.MFileBody")
60-
export default class MFileBody extends React.Component {
61-
decrypting = false;
60+
interface IProps {
61+
mxEvent: MatrixEvent,
62+
decryptedBlob?: Blob; // already decrypted blob
63+
tileShape?: string; // the shape of the tile, used
64+
showGenericPlaceholder?: boolean; // whether or not to show the default placeholder for the file. Defaults to true.
65+
}
6266

63-
static propTypes = {
64-
/* the MatrixEvent to show */
65-
mxEvent: PropTypes.object.isRequired,
66-
/* already decrypted blob */
67-
decryptedBlob: PropTypes.object,
68-
/* called when the download link iframe is shown */
69-
onHeightChanged: PropTypes.func,
70-
/* the shape of the tile, used */
71-
tileShape: PropTypes.string,
72-
/* whether or not to show the default placeholder for the file. Defaults to true. */
73-
showGenericPlaceholder: PropTypes.bool,
74-
};
67+
interface IState {
68+
decryptedBlob: Blob;
69+
}
70+
71+
@replaceableComponent("views.messages.MFileBody")
72+
export default class MFileBody extends React.Component<IProps, IState> {
73+
private decrypting = false;
7574

7675
static defaultProps = {
7776
showGenericPlaceholder: true,
@@ -81,11 +80,8 @@ export default class MFileBody extends React.Component {
8180
super(props);
8281

8382
this.state = {
84-
decryptedBlob: (this.props.decryptedBlob ? this.props.decryptedBlob : null),
83+
decryptedBlob: this.props.decryptedBlob,
8584
};
86-
87-
this._iframe = createRef();
88-
this._dummyLink = createRef();
8985
}
9086

9187
/**
@@ -96,7 +92,7 @@ export default class MFileBody extends React.Component {
9692
* @param {boolean} withSize Whether to include size information. Default true.
9793
* @return {string} the human readable link text for the attachment.
9894
*/
99-
presentableTextForFile(content, withSize = true) {
95+
private presentableTextForFile(content: any, withSize = true): string {
10096
let linkText = _t("Attachment");
10197
if (content.body && content.body.length > 0) {
10298
// The content body should be the name of the file including a
@@ -117,18 +113,12 @@ export default class MFileBody extends React.Component {
117113
return linkText;
118114
}
119115

120-
_getContentUrl() {
116+
private getContentUrl(): string {
121117
const media = mediaFromContent(this.props.mxEvent.getContent());
122118
return media.srcHttp;
123119
}
124120

125-
componentDidUpdate(prevProps, prevState) {
126-
if (this.props.onHeightChanged && !prevState.decryptedBlob && this.state.decryptedBlob) {
127-
this.props.onHeightChanged();
128-
}
129-
}
130-
131-
async _decrypt(file) {
121+
private async decrypt(file: any) {
132122
try {
133123
const blob = await decryptFile(file);
134124
await this.setState({
@@ -143,7 +133,7 @@ export default class MFileBody extends React.Component {
143133
}
144134
}
145135

146-
_onDownloadClick = async () => {
136+
private onDownloadClick = async () => {
147137
const content = this.props.mxEvent.getContent();
148138
const fileSize = content.info ? content.info.size : null;
149139
const fileType = content.info ? content.info.mimetype : "application/octet-stream";
@@ -161,7 +151,7 @@ export default class MFileBody extends React.Component {
161151
// decrypt it
162152
if (this.decrypting) return;
163153
this.decrypting = true;
164-
await this._decrypt(content.file);
154+
await this.decrypt(content.file);
165155
this.decrypting = false;
166156
}
167157

@@ -192,6 +182,7 @@ export default class MFileBody extends React.Component {
192182

193183
iframe.src = "usercontent/"; // XXX: this path should probably be passed from the skin - Michael
194184
iframe.onload = onIframeLoad;
185+
// @ts-ignore - TS thinks sandbox is readonly but we know better
195186
iframe.sandbox = "allow-scripts allow-downloads allow-downloads-without-user-activation";
196187
document.body.appendChild(iframe);
197188
} else if (["application/pdf"].includes(fileType) && !fileTooBig) {
@@ -214,7 +205,7 @@ export default class MFileBody extends React.Component {
214205
render() {
215206
const content = this.props.mxEvent.getContent();
216207
const isEncrypted = content.file !== undefined;
217-
const contentUrl = this._getContentUrl();
208+
const contentUrl = this.getContentUrl();
218209

219210
if (!this.props.showGenericPlaceholder) {
220211
return (

0 commit comments

Comments
 (0)