Convert ReplyTile to TS

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
pull/21833/head
Šimon Brandner 2021-07-02 14:51:51 +02:00
parent 0fe10e4502
commit 9a1b73f867
No known key found for this signature in database
GPG Key ID: 9760693FDD98A790
1 changed files with 31 additions and 53 deletions

View File

@ -15,66 +15,52 @@ limitations under the License.
*/ */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import { _t, _td } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import * as sdk from '../../../index';
import dis from '../../../dispatcher/dispatcher'; import dis from '../../../dispatcher/dispatcher';
import SettingsStore from "../../../settings/SettingsStore"; import SettingsStore from "../../../settings/SettingsStore";
import { MatrixClient } from 'matrix-js-sdk';
import { objectHasDiff } from '../../../utils/objects';
import { getHandlerTile } from "./EventTile"; import { getHandlerTile } from "./EventTile";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { RoomPermalinkCreator } from '../../../utils/permalinks/Permalinks';
import SenderProfile from "../messages/SenderProfile";
import TextualBody from "../messages/TextualBody";
import MImageReplyBody from "../messages/MImageReplyBody";
import * as sdk from '../../../index';
class ReplyTile extends React.Component { interface IProps {
static contextTypes = { mxEvent: MatrixEvent;
matrixClient: PropTypes.instanceOf(MatrixClient).isRequired, isRedacted?: boolean;
} permalinkCreator?: RoomPermalinkCreator;
highlights?: Array<string>;
static propTypes = { highlightLink?: string;
mxEvent: PropTypes.object.isRequired, onHeightChanged?(): void;
isRedacted: PropTypes.bool, }
permalinkCreator: PropTypes.object,
onHeightChanged: PropTypes.func,
}
export default class ReplyTile extends React.PureComponent<IProps> {
static defaultProps = { static defaultProps = {
onHeightChanged: function() {}, onHeightChanged: () => {},
} };
constructor(props, context) { constructor(props: IProps) {
super(props, context); super(props);
this.state = {};
this.onClick = this.onClick.bind(this);
this._onDecrypted = this._onDecrypted.bind(this);
} }
componentDidMount() { componentDidMount() {
this.props.mxEvent.on("Event.decrypted", this._onDecrypted); this.props.mxEvent.on("Event.decrypted", this.onDecrypted);
}
shouldComponentUpdate(nextProps, nextState) {
if (objectHasDiff(this.state, nextState)) {
return true;
}
return objectHasDiff(this.props, nextProps);
} }
componentWillUnmount() { componentWillUnmount() {
this.props.mxEvent.removeListener("Event.decrypted", this._onDecrypted); this.props.mxEvent.removeListener("Event.decrypted", this.onDecrypted);
} }
_onDecrypted() { private onDecrypted = (): void => {
this.forceUpdate(); this.forceUpdate();
if (this.props.onHeightChanged) { if (this.props.onHeightChanged) {
this.props.onHeightChanged(); this.props.onHeightChanged();
} }
} };
onClick(e) { private onClick = (e: React.MouseEvent): void => {
// This allows the permalink to be opened in a new tab/window or copied as // This allows the permalink to be opened in a new tab/window or copied as
// matrix.to, but also for it to enable routing within Riot when clicked. // matrix.to, but also for it to enable routing within Riot when clicked.
e.preventDefault(); e.preventDefault();
@ -84,11 +70,9 @@ class ReplyTile extends React.Component {
highlighted: true, highlighted: true,
room_id: this.props.mxEvent.getRoomId(), room_id: this.props.mxEvent.getRoomId(),
}); });
} };
render() { render() {
const SenderProfile = sdk.getComponent('messages.SenderProfile');
const content = this.props.mxEvent.getContent(); const content = this.props.mxEvent.getContent();
const msgtype = content.msgtype; const msgtype = content.msgtype;
const eventType = this.props.mxEvent.getType(); const eventType = this.props.mxEvent.getType();
@ -118,6 +102,7 @@ class ReplyTile extends React.Component {
{ _t('This event could not be displayed') } { _t('This event could not be displayed') }
</div>; </div>;
} }
const EventTileType = sdk.getComponent(tileHandler); const EventTileType = sdk.getComponent(tileHandler);
const classes = classNames({ const classes = classNames({
@ -135,18 +120,12 @@ class ReplyTile extends React.Component {
const needsSenderProfile = msgtype !== 'm.image' && tileHandler !== 'messages.RoomCreate' && !isInfoMessage; const needsSenderProfile = msgtype !== 'm.image' && tileHandler !== 'messages.RoomCreate' && !isInfoMessage;
if (needsSenderProfile) { if (needsSenderProfile) {
let text = null; sender = <SenderProfile
if (msgtype === 'm.image') text = _td('%(senderName)s sent an image');
else if (msgtype === 'm.video') text = _td('%(senderName)s sent a video');
else if (msgtype === 'm.file') text = _td('%(senderName)s uploaded a file');
sender = <SenderProfile onClick={this.onSenderProfileClick}
mxEvent={this.props.mxEvent} mxEvent={this.props.mxEvent}
enableFlair={false} enableFlair={false}
text={text} />; />;
} }
const MImageReplyBody = sdk.getComponent('messages.MImageReplyBody');
const TextualBody = sdk.getComponent('messages.TextualBody');
const msgtypeOverrides = { const msgtypeOverrides = {
"m.image": MImageReplyBody, "m.image": MImageReplyBody,
// We don't want a download link for files, just the file name is enough. // We don't want a download link for files, just the file name is enough.
@ -163,7 +142,8 @@ class ReplyTile extends React.Component {
<div className={classes}> <div className={classes}>
<a href={permalink} onClick={this.onClick}> <a href={permalink} onClick={this.onClick}>
{ sender } { sender }
<EventTileType ref="tile" <EventTileType
ref="tile"
mxEvent={this.props.mxEvent} mxEvent={this.props.mxEvent}
highlights={this.props.highlights} highlights={this.props.highlights}
highlightLink={this.props.highlightLink} highlightLink={this.props.highlightLink}
@ -177,5 +157,3 @@ class ReplyTile extends React.Component {
); );
} }
} }
export default ReplyTile;