element-web/src/components/views/elements/EventTilePreview.tsx

135 lines
3.8 KiB
TypeScript
Raw Normal View History

2020-06-16 17:41:10 +03: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 17:52:35 +03:00
import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import * as Avatar from '../../../Avatar';
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import EventTile from '../rooms/EventTile';
2020-09-16 13:26:15 +03:00
import SettingsStore from "../../../settings/SettingsStore";
import {UIFeature} from "../../../settings/UIFeature";
2020-06-16 17:41:10 +03:00
interface IProps {
/**
* The text to be displayed in the message preview
*/
message: string;
/**
* Whether to use the irc layout or not
*/
useIRCLayout: boolean;
/**
* classnames to apply to the wrapper of the preview
*/
className: string;
}
/* eslint-disable camelcase */
2020-06-16 17:41:10 +03:00
interface IState {
userId: string;
displayname: string;
avatar_url: string;
2020-06-16 17:41:10 +03:00
}
2020-09-01 11:07:46 +03:00
/* eslint-enable camelcase */
2020-06-16 17:41:10 +03:00
const AVATAR_SIZE = 32;
export default class EventTilePreview extends React.Component<IProps, IState> {
2020-06-16 17:41:10 +03:00
constructor(props: IProps) {
super(props);
this.state = {
userId: "@erim:fink.fink",
displayname: "Erimayas Fink",
avatar_url: null,
2020-06-18 17:26:53 +03:00
};
2020-06-16 17:41:10 +03:00
}
async componentDidMount() {
// Fetch current user data
2020-06-18 17:26:53 +03:00
const client = MatrixClientPeg.get();
const userId = client.getUserId();
2020-06-16 17:41:10 +03:00
const profileInfo = await client.getProfileInfo(userId);
2020-09-01 11:07:46 +03:00
const avatarUrl = Avatar.avatarUrlForUser(
2020-06-16 17:41:10 +03:00
{avatarUrl: profileInfo.avatar_url},
AVATAR_SIZE, AVATAR_SIZE, "crop");
this.setState({
userId,
displayname: profileInfo.displayname,
2020-09-01 11:07:46 +03:00
avatar_url: avatarUrl,
2020-06-16 17:41:10 +03:00
});
}
2020-09-01 11:07:46 +03:00
private fakeEvent({userId, displayname, avatar_url: avatarUrl}: IState) {
2020-06-16 17:41:10 +03:00
// Fake it till we make it
const rawEvent = {
type: "m.room.message",
sender: userId,
content: {
"m.new_content": {
msgtype: "m.text",
body: this.props.message,
displayname: displayname,
avatar_url: avatarUrl,
2020-06-16 17:41:10 +03:00
},
msgtype: "m.text",
body: this.props.message,
displayname: displayname,
avatar_url: avatarUrl,
},
unsigned: {
age: 97,
},
event_id: "$9999999999999999999999999999999999999999999",
room_id: "!999999999999999999:example.org",
};
const event = new MatrixEvent(rawEvent);
2020-06-16 17:41:10 +03:00
// Fake it more
event.sender = {
name: displayname,
userId: userId,
2020-06-16 17:41:10 +03:00
getAvatarUrl: (..._) => {
2020-09-01 11:07:46 +03:00
return avatarUrl;
2020-06-16 17:41:10 +03:00
},
2020-06-18 17:26:53 +03:00
};
2020-06-16 17:41:10 +03:00
return event;
}
public render() {
const event = this.fakeEvent(this.state);
const className = classnames(this.props.className, {
"mx_IRCLayout": this.props.useIRCLayout,
"mx_GroupLayout": !this.props.useIRCLayout,
});
2020-06-16 17:41:10 +03:00
return <div className={className}>
2020-09-16 13:26:15 +03:00
<EventTile
mxEvent={event}
useIRCLayout={this.props.useIRCLayout}
enableFlair={SettingsStore.getValue(UIFeature.Flair)}
/>
2020-06-18 17:26:53 +03:00
</div>;
2020-06-16 17:41:10 +03:00
}
}