element-web/src/components/views/avatars/RoomAvatar.tsx

145 lines
4.4 KiB
TypeScript
Raw Normal View History

2015-09-18 16:34:36 +03:00
/*
2016-01-07 07:06:39 +03:00
Copyright 2015, 2016 OpenMarket Ltd
2015-09-18 16:34:36 +03:00
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.
*/
2021-06-18 18:13:55 +03:00
import React, { ComponentProps } from 'react';
import { Room } from 'matrix-js-sdk/src/models/room';
import { ResizeMethod } from 'matrix-js-sdk/src/@types/partials';
2020-07-07 00:42:46 +03:00
import BaseAvatar from './BaseAvatar';
import ImageView from '../elements/ImageView';
2021-06-18 18:13:55 +03:00
import { MatrixClientPeg } from '../../../MatrixClientPeg';
import Modal from '../../../Modal';
import * as Avatar from '../../../Avatar';
2021-06-18 18:13:55 +03:00
import { replaceableComponent } from "../../../utils/replaceableComponent";
import { mediaFromMxc } from "../../../customisations/Media";
import { IOOBData } from '../../../stores/ThreepidInviteStore';
interface IProps extends Omit<ComponentProps<typeof BaseAvatar>, "name" | "idName" | "url" | "onClick"> {
// Room may be left unset here, but if it is,
// oobData.avatarUrl should be set (else there
// would be nowhere to get the avatar from)
2020-07-07 00:42:46 +03:00
room?: Room;
oobData?: IOOBData;
2020-07-07 00:42:46 +03:00
width?: number;
height?: number;
resizeMethod?: ResizeMethod;
2020-07-07 00:42:46 +03:00
viewAvatarOnClick?: boolean;
onClick?(): void;
2020-07-07 00:42:46 +03:00
}
interface IState {
urls: string[];
}
@replaceableComponent("views.avatars.RoomAvatar")
2020-07-07 00:42:46 +03:00
export default class RoomAvatar extends React.Component<IProps, IState> {
public static defaultProps = {
width: 36,
height: 36,
resizeMethod: 'crop',
oobData: {},
};
constructor(props: IProps) {
super(props);
this.state = {
urls: RoomAvatar.getImageUrls(this.props),
2020-07-07 17:40:05 +03:00
};
2020-07-07 00:42:46 +03:00
}
2020-07-07 00:42:46 +03:00
public componentDidMount() {
MatrixClientPeg.get().on("RoomState.events", this.onRoomStateEvents);
2020-07-07 00:42:46 +03:00
}
2020-07-07 00:42:46 +03:00
public componentWillUnmount() {
2018-03-14 17:55:36 +03:00
const cli = MatrixClientPeg.get();
if (cli) {
cli.removeListener("RoomState.events", this.onRoomStateEvents);
}
2020-07-07 00:42:46 +03:00
}
2020-07-07 00:42:46 +03:00
public static getDerivedStateFromProps(nextProps: IProps): IState {
return {
urls: RoomAvatar.getImageUrls(nextProps),
};
}
2020-07-07 00:42:46 +03:00
// TODO: type when js-sdk has types
private onRoomStateEvents = (ev: any) => {
if (!this.props.room ||
ev.getRoomId() !== this.props.room.roomId ||
ev.getType() !== 'm.room.avatar'
) return;
this.setState({
2020-07-07 00:42:46 +03:00
urls: RoomAvatar.getImageUrls(this.props),
});
2020-07-07 17:40:05 +03:00
};
2020-07-07 00:42:46 +03:00
private static getImageUrls(props: IProps): string[] {
let oobAvatar = null;
if (props.oobData.avatarUrl) {
oobAvatar = mediaFromMxc(props.oobData.avatarUrl).getThumbnailOfSourceHttp(
props.width,
props.height,
props.resizeMethod,
);
}
return [
oobAvatar, // highest priority
2020-07-07 00:42:46 +03:00
RoomAvatar.getRoomAvatarUrl(props),
].filter(function(url) {
2020-07-07 17:40:05 +03:00
return (url !== null && url !== "");
});
2020-07-07 00:42:46 +03:00
}
2020-07-07 00:42:46 +03:00
private static getRoomAvatarUrl(props: IProps): string {
2017-07-11 16:31:07 +03:00
if (!props.room) return null;
return Avatar.avatarUrlForRoom(props.room, props.width, props.height, props.resizeMethod);
2020-07-07 00:42:46 +03:00
}
2015-09-18 16:34:36 +03:00
2020-07-07 00:42:46 +03:00
private onRoomAvatarClick = () => {
const avatarUrl = Avatar.avatarUrlForRoom(
this.props.room,
null,
null,
null,
);
const params = {
src: avatarUrl,
name: this.props.room.name,
};
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox", null, true);
2020-07-07 17:40:05 +03:00
};
2020-07-07 00:42:46 +03:00
public render() {
2021-06-29 15:11:58 +03:00
const { room, oobData, viewAvatarOnClick, onClick, ...otherProps } = this.props;
const roomName = room ? room.name : oobData.name;
2016-01-15 19:13:25 +03:00
return (
<BaseAvatar {...otherProps}
name={roomName}
idName={room ? room.roomId : null}
urls={this.state.urls}
onClick={viewAvatarOnClick && this.state.urls[0] ? this.onRoomAvatarClick : onClick}
2020-07-07 00:42:46 +03:00
/>
2016-01-15 19:13:25 +03:00
);
2020-07-07 00:42:46 +03:00
}
}