Files
ThreadNet-Web/src/components/views/avatars/RoomAvatar.tsx
T

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

147 lines
4.7 KiB
TypeScript
Raw Normal View History

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
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
*/
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";
import Modal from "../../../Modal";
2019-12-19 18:19:56 -07:00
import * as Avatar from "../../../Avatar";
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";
import { LocalRoom } from "../../../models/LocalRoom";
import { filterBoolean } from "../../../utils/arrays";
2021-02-26 10:23:09 +00:00
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-06 22:42:46 +01:00
room?: Room;
oobData: IOOBData & {
roomId?: string;
};
2020-07-06 22:42:46 +01:00
viewAvatarOnClick?: boolean;
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
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 = {
size: "36px",
2020-07-06 22:42:46 +01:00
oobData: {},
};
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
}
public componentDidMount(): void {
MatrixClientPeg.safeGet().on(RoomStateEvent.Events, this.onRoomStateEvents);
2020-07-06 22:42:46 +01:00
}
public componentWillUnmount(): void {
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
2020-07-06 22:42:46 +01:00
}
2020-07-06 22:42:46 +01:00
public static getDerivedStateFromProps(nextProps: IProps): IState {
return {
urls: RoomAvatar.getImageUrls(nextProps),
};
}
private onRoomStateEvents = (ev: MatrixEvent): void => {
if (ev.getRoomId() !== this.props.room?.roomId || ev.getType() !== EventType.RoomAvatar) return;
this.setState({
2020-07-06 22:42:46 +01:00
urls: RoomAvatar.getImageUrls(this.props),
});
2020-07-07 15:40:05 +01:00
};
2020-07-06 22:42:46 +01:00
private static getImageUrls(props: IProps): string[] {
let oobAvatar: string | null = null;
if (props.oobData.avatarUrl) {
oobAvatar = mediaFromMxc(props.oobData.avatarUrl).getThumbnailOfSourceHttp(
parseInt(props.size, 10),
parseInt(props.size, 10),
"crop",
);
}
return filterBoolean([
oobAvatar, // highest priority
2020-07-06 22:42:46 +01:00
RoomAvatar.getRoomAvatarUrl(props),
]);
2020-07-06 22:42:46 +01:00
}
private static getRoomAvatarUrl(props: IProps): string | null {
2017-07-11 14:31:07 +01:00
if (!props.room) return null;
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
private onRoomAvatarClick = (): void => {
const avatarUrl = Avatar.avatarUrlForRoom(this.props.room ?? null, undefined, undefined, undefined);
if (!avatarUrl) return;
const params = {
src: avatarUrl,
name: this.props.room?.name,
};
Modal.createDialog(ImageView, params, "mx_Dialog_lightbox", undefined, true);
2020-07-07 15:40:05 +01:00
};
private get roomIdName(): string | undefined {
const room = this.props.room;
if (room) {
return idNameForRoom(room);
} else {
return this.props.oobData?.roomId;
}
}
public render(): React.ReactNode {
const { room, oobData, viewAvatarOnClick, onClick, className, ...otherProps } = this.props;
const roomName = room?.name ?? oobData.name ?? "?";
2016-01-15 16:13:25 +00:00
return (
<BaseAvatar
{...otherProps}
type={(room?.getType() ?? this.props.oobData?.roomType) === RoomType.Space ? "square" : "round"}
2021-07-19 16:51:43 +01:00
name={roomName}
idName={this.roomIdName}
urls={this.state.urls}
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
}
}