2020-06-16 15:41:10 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
|
import classnames from "classnames";
|
2020-06-16 15:52:35 +01:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-06-22 22:27:12 +01:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2023-04-17 02:37:29 -07:00
|
|
|
import { MsgType } from "matrix-js-sdk/src/@types/event";
|
2020-06-18 13:58:35 +01:00
|
|
|
|
|
|
|
|
import * as Avatar from "../../../Avatar";
|
|
|
|
|
import EventTile from "../rooms/EventTile";
|
2021-11-17 08:19:30 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-06-05 22:21:10 -04: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> {
|
2022-12-16 12:29:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00: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 */
|
2020-10-08 14:21:39 -06:00
|
|
|
const rawEvent = {
|
|
|
|
|
type: "m.room.message",
|
2021-02-26 16:02:46 -05:00
|
|
|
sender: this.props.userId,
|
2020-10-08 14:21:39 -06:00
|
|
|
content: {
|
|
|
|
|
"m.new_content": {
|
2023-04-17 02:37:29 -07:00
|
|
|
msgtype: MsgType.Text,
|
2021-02-26 16:02:46 -05:00
|
|
|
body: message,
|
|
|
|
|
displayname: this.props.displayName,
|
2021-03-31 15:47:26 -04:00
|
|
|
avatar_url: this.props.avatarUrl,
|
2020-06-16 15:41:10 +01:00
|
|
|
},
|
2023-04-17 02:37:29 -07:00
|
|
|
"msgtype": MsgType.Text,
|
2021-02-26 16:02:46 -05:00
|
|
|
"body": message,
|
|
|
|
|
"displayname": this.props.displayName,
|
2021-03-31 15:47:26 -04:00
|
|
|
"avatar_url": this.props.avatarUrl,
|
2020-10-08 14:21:39 -06:00
|
|
|
},
|
|
|
|
|
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 = {
|
2021-05-08 21:22:31 -04:00
|
|
|
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: (..._) => {
|
2021-03-31 15:47:26 -04:00
|
|
|
return Avatar.avatarUrlForUser({ avatarUrl: this.props.avatarUrl }, AVATAR_SIZE, AVATAR_SIZE, "crop");
|
2020-06-16 15:41:10 +01:00
|
|
|
},
|
2021-03-31 15:47:26 -04: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
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2020-08-29 01:11:08 +01:00
|
|
|
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-08-29 01:11:08 +01:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
}
|