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

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

104 lines
3.3 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2019-2022 The Matrix.org Foundation C.I.C.
Copyright 2015, 2016 OpenMarket Ltd
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.
*/
import React, { type JSX, forwardRef, type ReactNode, type Ref, useContext } from "react";
2025-02-05 13:25:06 +00:00
import { type RoomMember, type ResizeMethod } from "matrix-js-sdk/src/matrix";
2021-06-29 10:36:24 +05:30
2020-05-13 20:41:41 -06:00
import dis from "../../../dispatcher/dispatcher";
2021-06-01 12:40:03 +05:30
import { Action } from "../../../dispatcher/actions";
2020-07-06 22:42:46 +01:00
import BaseAvatar from "./BaseAvatar";
2021-06-01 12:40:03 +05:30
import { mediaFromMxc } from "../../../customisations/Media";
2022-03-24 15:35:01 -06:00
import { CardContext } from "../right_panel/context";
import UserIdentifierCustomisations from "../../../customisations/UserIdentifier";
import { useRoomMemberProfile } from "../../../hooks/room/useRoomMemberProfile";
import { _t } from "../../../languageHandler";
2020-09-29 10:10:32 +01:00
interface IProps extends Omit<React.ComponentProps<typeof BaseAvatar>, "name" | "idName" | "url"> {
member: RoomMember | null;
2020-09-03 15:00:14 +01:00
fallbackUserId?: string;
size: string;
2021-03-08 17:03:14 -07:00
resizeMethod?: ResizeMethod;
// Whether the onClick of the avatar should be overridden to dispatch `Action.ViewUser`
2020-09-03 15:00:14 +01:00
viewUserOnClick?: boolean;
pushUserOnClick?: boolean;
2020-09-03 15:00:14 +01:00
title?: string;
style?: any;
forceHistorical?: boolean; // true to deny `useOnlyCurrentProfiles` usage. Default false.
2022-04-22 17:09:44 +02:00
hideTitle?: boolean;
children?: ReactNode;
2020-07-06 22:42:46 +01:00
}
function MemberAvatar(
{
size,
resizeMethod = "crop",
viewUserOnClick,
forceHistorical,
fallbackUserId,
hideTitle,
member: propsMember,
...props
}: IProps,
ref: Ref<HTMLElement>,
): JSX.Element {
const card = useContext(CardContext);
const member = useRoomMemberProfile({
userId: propsMember?.userId,
member: propsMember,
forceHistorical: forceHistorical,
});
const name = member?.name ?? fallbackUserId;
let title: string | undefined = props.title;
let imageUrl: string | null | undefined;
if (member?.name) {
if (member.getMxcAvatarUrl()) {
imageUrl = mediaFromMxc(member.getMxcAvatarUrl() ?? "").getThumbnailOfSourceHttp(
parseInt(size, 10),
parseInt(size, 10),
resizeMethod,
);
}
if (!title) {
title =
UserIdentifierCustomisations.getDisplayUserIdentifier(member?.userId ?? "", {
roomId: member?.roomId ?? "",
}) ?? fallbackUserId;
}
2020-07-06 22:42:46 +01:00
}
2016-08-01 17:10:46 +01:00
return (
<BaseAvatar
{...props}
size={size}
name={name ?? ""}
title={hideTitle ? undefined : title}
idName={member?.userId ?? fallbackUserId}
url={imageUrl}
onClick={
viewUserOnClick
? () => {
dis.dispatch({
action: Action.ViewUser,
member: propsMember,
push: card.isCard,
});
}
: props.onClick
}
altText={_t("common|user_avatar")}
ref={ref}
/>
);
2020-07-06 22:42:46 +01:00
}
export default forwardRef(MemberAvatar);