2015-09-18 14:34:36 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-09-18 14:34:36 +01:00
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2015-09-18 14:34:36 +01:00
|
|
|
*/
|
2021-07-02 14:51:55 +01:00
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import React, { type ComponentProps } from "react";
|
|
|
|
|
import { type Room, RoomStateEvent, type MatrixEvent, EventType, RoomType } from "matrix-js-sdk/src/matrix";
|
2020-07-06 22:42:46 +01:00
|
|
|
|
|
|
|
|
import BaseAvatar from "./BaseAvatar";
|
|
|
|
|
import ImageView from "../elements/ImageView";
|
2021-06-18 16:13:55 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2018-04-21 15:47:31 +12:00
|
|
|
import Modal from "../../../Modal";
|
2019-12-19 18:19:56 -07:00
|
|
|
import * as Avatar from "../../../Avatar";
|
2021-07-16 14:19:36 -04:00
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
2021-06-18 16:13:55 +01:00
|
|
|
import { mediaFromMxc } from "../../../customisations/Media";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type IOOBData } from "../../../stores/ThreepidInviteStore";
|
2023-01-05 17:05:21 +01:00
|
|
|
import { LocalRoom } from "../../../models/LocalRoom";
|
2023-02-28 10:25:36 +00:00
|
|
|
import { filterBoolean } from "../../../utils/arrays";
|
2015-11-26 12:02:31 +00:00
|
|
|
|
2021-02-26 10:23:09 +00:00
|
|
|
interface IProps extends Omit<ComponentProps<typeof BaseAvatar>, "name" | "idName" | "url" | "onClick"> {
|
2016-03-02 11:59:17 +00:00
|
|
|
// 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-06 22:42:46 +01:00
|
|
|
room?: Room;
|
2023-02-28 10:25:36 +00:00
|
|
|
oobData: IOOBData & {
|
2021-07-02 14:51:55 +01:00
|
|
|
roomId?: string;
|
|
|
|
|
};
|
2020-07-06 22:42:46 +01:00
|
|
|
viewAvatarOnClick?: boolean;
|
2020-11-05 15:50:42 +00:00
|
|
|
onClick?(): void;
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
2016-01-15 16:13:25 +00:00
|
|
|
|
2020-07-06 22:42:46 +01:00
|
|
|
interface IState {
|
|
|
|
|
urls: string[];
|
|
|
|
|
}
|
2015-09-18 14:34:36 +01:00
|
|
|
|
2024-06-06 18:35:44 +01:00
|
|
|
export function idNameForRoom(room: Room): string {
|
|
|
|
|
const dmMapUserId = DMRoomMap.shared().getUserIdForRoomId(room.roomId);
|
|
|
|
|
// If the room is a DM, we use the other user's ID for the color hash
|
|
|
|
|
// in order to match the room avatar with their avatar
|
|
|
|
|
if (dmMapUserId) return dmMapUserId;
|
|
|
|
|
|
|
|
|
|
if (room instanceof LocalRoom && room.targets.length === 1) {
|
|
|
|
|
return room.targets[0].userId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return room.roomId;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 22:42:46 +01:00
|
|
|
export default class RoomAvatar extends React.Component<IProps, IState> {
|
|
|
|
|
public static defaultProps = {
|
2023-08-24 04:48:35 +01:00
|
|
|
size: "36px",
|
2020-07-06 22:42:46 +01:00
|
|
|
oobData: {},
|
|
|
|
|
};
|
2015-10-22 13:08:35 +01:00
|
|
|
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2020-07-06 22:42:46 +01:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
urls: RoomAvatar.getImageUrls(this.props),
|
2020-07-07 15:40:05 +01:00
|
|
|
};
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentDidMount(): void {
|
2023-06-15 08:46:19 +01:00
|
|
|
MatrixClientPeg.safeGet().on(RoomStateEvent.Events, this.onRoomStateEvents);
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
2018-03-14 14:29:55 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2022-10-24 09:58:36 +01:00
|
|
|
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
2018-03-14 14:29:55 +00:00
|
|
|
|
2020-07-06 22:42:46 +01:00
|
|
|
public static getDerivedStateFromProps(nextProps: IProps): IState {
|
|
|
|
|
return {
|
|
|
|
|
urls: RoomAvatar.getImageUrls(nextProps),
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-01-15 17:05:05 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onRoomStateEvents = (ev: MatrixEvent): void => {
|
2022-02-24 14:39:25 +00:00
|
|
|
if (ev.getRoomId() !== this.props.room?.roomId || ev.getType() !== EventType.RoomAvatar) return;
|
2018-03-14 14:29:55 +00:00
|
|
|
|
|
|
|
|
this.setState({
|
2020-07-06 22:42:46 +01:00
|
|
|
urls: RoomAvatar.getImageUrls(this.props),
|
2018-03-14 14:29:55 +00:00
|
|
|
});
|
2020-07-07 15:40:05 +01:00
|
|
|
};
|
2018-03-14 14:29:55 +00:00
|
|
|
|
2020-07-06 22:42:46 +01:00
|
|
|
private static getImageUrls(props: IProps): string[] {
|
2023-02-28 10:25:36 +00:00
|
|
|
let oobAvatar: string | null = null;
|
2021-03-05 18:45:09 -07:00
|
|
|
if (props.oobData.avatarUrl) {
|
|
|
|
|
oobAvatar = mediaFromMxc(props.oobData.avatarUrl).getThumbnailOfSourceHttp(
|
2023-08-24 04:48:35 +01:00
|
|
|
parseInt(props.size, 10),
|
|
|
|
|
parseInt(props.size, 10),
|
|
|
|
|
"crop",
|
2021-03-05 18:45:09 -07:00
|
|
|
);
|
|
|
|
|
}
|
2023-02-28 10:25:36 +00:00
|
|
|
|
|
|
|
|
return filterBoolean([
|
2021-03-05 18:45:09 -07:00
|
|
|
oobAvatar, // highest priority
|
2020-07-06 22:42:46 +01:00
|
|
|
RoomAvatar.getRoomAvatarUrl(props),
|
2023-02-28 10:25:36 +00:00
|
|
|
]);
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
2016-01-15 17:05:05 +00:00
|
|
|
|
2023-02-28 10:25:36 +00:00
|
|
|
private static getRoomAvatarUrl(props: IProps): string | null {
|
2017-07-11 14:31:07 +01:00
|
|
|
if (!props.room) return null;
|
2016-03-01 18:23:57 +00:00
|
|
|
|
2023-08-24 04:48:35 +01:00
|
|
|
return Avatar.avatarUrlForRoom(props.room, parseInt(props.size, 10), parseInt(props.size, 10), "crop");
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
2015-09-18 14:34:36 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onRoomAvatarClick = (): void => {
|
2023-02-28 10:25:36 +00:00
|
|
|
const avatarUrl = Avatar.avatarUrlForRoom(this.props.room ?? null, undefined, undefined, undefined);
|
2023-04-17 08:31:58 +01:00
|
|
|
if (!avatarUrl) return;
|
2018-04-21 15:47:31 +12:00
|
|
|
const params = {
|
|
|
|
|
src: avatarUrl,
|
2023-02-28 10:25:36 +00:00
|
|
|
name: this.props.room?.name,
|
2018-04-21 15:47:31 +12:00
|
|
|
};
|
|
|
|
|
|
2023-02-28 10:25:36 +00:00
|
|
|
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox", undefined, true);
|
2020-07-07 15:40:05 +01:00
|
|
|
};
|
2016-03-01 18:23:57 +00:00
|
|
|
|
2023-01-05 17:05:21 +01:00
|
|
|
private get roomIdName(): string | undefined {
|
|
|
|
|
const room = this.props.room;
|
|
|
|
|
|
|
|
|
|
if (room) {
|
2024-06-06 18:35:44 +01:00
|
|
|
return idNameForRoom(room);
|
|
|
|
|
} else {
|
|
|
|
|
return this.props.oobData?.roomId;
|
2023-01-05 17:05:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2021-07-02 14:51:55 +01:00
|
|
|
const { room, oobData, viewAvatarOnClick, onClick, className, ...otherProps } = this.props;
|
2023-02-28 10:25:36 +00:00
|
|
|
const roomName = room?.name ?? oobData.name ?? "?";
|
2016-03-01 18:23:57 +00:00
|
|
|
|
2016-01-15 16:13:25 +00:00
|
|
|
return (
|
2021-07-02 14:51:55 +01:00
|
|
|
<BaseAvatar
|
|
|
|
|
{...otherProps}
|
2023-08-24 04:48:35 +01:00
|
|
|
type={(room?.getType() ?? this.props.oobData?.roomType) === RoomType.Space ? "square" : "round"}
|
2021-07-19 16:51:43 +01:00
|
|
|
name={roomName}
|
2023-01-05 17:05:21 +01:00
|
|
|
idName={this.roomIdName}
|
2018-04-21 15:47:31 +12:00
|
|
|
urls={this.state.urls}
|
2020-11-05 15:50:42 +00:00
|
|
|
onClick={viewAvatarOnClick && this.state.urls[0] ? this.onRoomAvatarClick : onClick}
|
2020-07-06 22:42:46 +01:00
|
|
|
/>
|
2016-01-15 16:13:25 +00:00
|
|
|
);
|
2020-07-06 22:42:46 +01:00
|
|
|
}
|
|
|
|
|
}
|