Files
ThreadNet-Web/src/components/views/elements/EventTilePreview.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-06-16 15:41:10 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2020-06-16 15:41:10 +01:00
Copyright 2020 The Matrix.org Foundation C.I.C.
2024-09-09 14:57:16 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
2020-06-16 15:41:10 +01:00
*/
import React from "react";
import classnames from "classnames";
import { MatrixEvent, RoomMember, MsgType } from "matrix-js-sdk/src/matrix";
2020-06-18 13:58:35 +01:00
import * as Avatar from "../../../Avatar";
import EventTile from "../rooms/EventTile";
import { Layout } from "../../../settings/enums/Layout";
2021-08-27 20:48:29 +02:00
import Spinner from "./Spinner";
2020-06-16 15:41:10 +01:00
interface IProps {
/**
* The text to be displayed in the message preview
*/
message: string;
/**
* Whether to use the irc layout or not
*/
2021-01-22 13:44:45 +01:00
layout: Layout;
2020-06-16 15:41:10 +01:00
/**
* classnames to apply to the wrapper of the preview
*/
className?: string;
2021-02-26 16:02:46 -05:00
/**
* The ID of the displayed user
*/
2021-08-27 19:32:38 +02:00
userId?: string;
2021-02-26 16:02:46 -05:00
/**
* The display name of the displayed user
*/
displayName?: string;
/**
* The mxc:// avatar URL of the displayed user
*/
avatarUrl?: string;
2020-06-16 15:41:10 +01:00
}
interface IState {
2021-02-26 16:02:46 -05:00
message: string;
2020-06-16 15:41:10 +01:00
}
const AVATAR_SIZE = 32;
2020-06-16 15:41:10 +01:00
2020-06-18 15:36:23 +01:00
export default class EventTilePreview extends React.Component<IProps, IState> {
public constructor(props: IProps) {
2020-06-16 15:41:10 +01:00
super(props);
this.state = {
2021-02-26 16:02:46 -05:00
message: props.message,
2020-06-18 15:26:53 +01:00
};
2020-06-16 15:41:10 +01:00
}
private fakeEvent({ message }: IState): MatrixEvent {
2020-06-16 15:41:10 +01:00
// Fake it till we make it
2020-10-08 14:50:17 -06:00
/* eslint-disable quote-props */
const rawEvent = {
type: "m.room.message",
2021-02-26 16:02:46 -05:00
sender: this.props.userId,
content: {
"m.new_content": {
msgtype: MsgType.Text,
2021-02-26 16:02:46 -05:00
body: message,
displayname: this.props.displayName,
avatar_url: this.props.avatarUrl,
2020-06-16 15:41:10 +01:00
},
"msgtype": MsgType.Text,
2021-02-26 16:02:46 -05:00
"body": message,
"displayname": this.props.displayName,
"avatar_url": this.props.avatarUrl,
},
unsigned: {
age: 97,
},
event_id: "$9999999999999999999999999999999999999999999",
room_id: "!999999999999999999:example.org",
};
const event = new MatrixEvent(rawEvent);
2020-10-08 14:50:17 -06:00
/* eslint-enable quote-props */
2020-06-16 15:41:10 +01:00
// Fake it more
event.sender = {
name: this.props.displayName || this.props.userId,
2021-06-16 18:23:44 -04:00
rawDisplayName: this.props.displayName,
2021-02-26 16:02:46 -05:00
userId: this.props.userId,
2020-06-16 15:41:10 +01:00
getAvatarUrl: (..._) => {
return Avatar.avatarUrlForUser({ avatarUrl: this.props.avatarUrl }, AVATAR_SIZE, AVATAR_SIZE, "crop");
2020-06-16 15:41:10 +01:00
},
getMxcAvatarUrl: () => this.props.avatarUrl,
2021-06-22 22:27:12 +01:00
} as RoomMember;
2020-06-16 15:41:10 +01:00
2020-06-22 12:28:15 +01:00
return event;
2020-06-18 13:58:35 +01:00
}
public render(): React.ReactNode {
const className = classnames(this.props.className, {
2021-01-22 13:44:45 +01:00
mx_IRCLayout: this.props.layout == Layout.IRC,
2021-08-28 14:04:00 +02:00
mx_EventTilePreview_loader: !this.props.userId,
});
2020-06-16 15:41:10 +01:00
2021-08-28 14:00:18 +02:00
if (!this.props.userId)
return (
<div className={className}>
<Spinner />
</div>
);
2021-08-27 20:10:01 +02:00
const event = this.fakeEvent(this.state);
2021-04-12 12:28:42 +01:00
return (
2023-04-21 10:48:48 +01:00
<div className={className} role="presentation">
<EventTile mxEvent={event} layout={this.props.layout} as="div" hideTimestamp inhibitInteraction />
2020-06-18 15:26:53 +01:00
</div>
);
2020-06-16 15:41:10 +01:00
}
}