2023-01-03 16:55:26 +00:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2023-01-03 16:55:26 +00:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-01-03 16:55:26 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-12-11 09:27:55 +00:00
|
|
|
import {
|
|
|
|
|
useMemo,
|
|
|
|
|
type FC,
|
|
|
|
|
type CSSProperties,
|
|
|
|
|
useState,
|
|
|
|
|
useEffect,
|
|
|
|
|
} from "react";
|
2023-08-31 15:46:09 +02:00
|
|
|
import { Avatar as CompoundAvatar } from "@vector-im/compound-web";
|
2024-12-11 09:27:55 +00:00
|
|
|
import { type MatrixClient } from "matrix-js-sdk/src/client";
|
2022-05-06 11:32:09 +01:00
|
|
|
|
2024-12-06 18:30:05 +00:00
|
|
|
import { useClientState } from "./ClientContext";
|
2021-12-07 17:59:55 -08:00
|
|
|
|
2022-05-18 19:00:59 -04:00
|
|
|
export enum Size {
|
|
|
|
|
XS = "xs",
|
|
|
|
|
SM = "sm",
|
|
|
|
|
MD = "md",
|
|
|
|
|
LG = "lg",
|
|
|
|
|
XL = "xl",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const sizes = new Map([
|
|
|
|
|
[Size.XS, 22],
|
|
|
|
|
[Size.SM, 32],
|
|
|
|
|
[Size.MD, 36],
|
|
|
|
|
[Size.LG, 42],
|
|
|
|
|
[Size.XL, 90],
|
|
|
|
|
]);
|
|
|
|
|
|
2024-12-18 15:31:45 +00:00
|
|
|
export interface Props {
|
2023-08-31 15:46:09 +02:00
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
className?: string;
|
2022-07-30 10:06:28 +02:00
|
|
|
src?: string;
|
2022-05-18 19:00:59 -04:00
|
|
|
size?: Size | number;
|
2024-11-25 20:22:02 +00:00
|
|
|
style?: CSSProperties;
|
2022-05-06 11:32:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 18:30:05 +00:00
|
|
|
export function getAvatarUrl(
|
|
|
|
|
client: MatrixClient,
|
|
|
|
|
mxcUrl: string | null,
|
|
|
|
|
avatarSize = 96,
|
|
|
|
|
): string | null {
|
|
|
|
|
const width = Math.floor(avatarSize * window.devicePixelRatio);
|
|
|
|
|
const height = Math.floor(avatarSize * window.devicePixelRatio);
|
|
|
|
|
// scale is more suitable for larger sizes
|
|
|
|
|
const resizeMethod = avatarSize <= 96 ? "crop" : "scale";
|
|
|
|
|
return mxcUrl
|
|
|
|
|
? client.mxcUrlToHttp(
|
|
|
|
|
mxcUrl,
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
resizeMethod,
|
|
|
|
|
false,
|
|
|
|
|
true,
|
|
|
|
|
true,
|
|
|
|
|
)
|
|
|
|
|
: null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-30 18:21:18 -04:00
|
|
|
export const Avatar: FC<Props> = ({
|
2023-08-31 15:46:09 +02:00
|
|
|
className,
|
|
|
|
|
id,
|
|
|
|
|
name,
|
2021-12-07 17:59:55 -08:00
|
|
|
src,
|
2022-05-18 19:00:59 -04:00
|
|
|
size = Size.MD,
|
2024-11-25 20:22:02 +00:00
|
|
|
style,
|
|
|
|
|
...props
|
2022-05-06 11:32:09 +01:00
|
|
|
}) => {
|
2024-12-06 18:30:05 +00:00
|
|
|
const clientState = useClientState();
|
2022-05-18 19:00:59 -04:00
|
|
|
|
2023-08-31 15:46:09 +02:00
|
|
|
const sizePx = useMemo(
|
2022-05-18 19:00:59 -04:00
|
|
|
() =>
|
|
|
|
|
Object.values(Size).includes(size as Size)
|
2023-08-31 15:46:09 +02:00
|
|
|
? sizes.get(size as Size)
|
|
|
|
|
: (size as number),
|
2023-10-11 10:42:04 -04:00
|
|
|
[size],
|
2022-05-18 19:00:59 -04:00
|
|
|
);
|
|
|
|
|
|
2024-12-06 18:30:05 +00:00
|
|
|
const [avatarUrl, setAvatarUrl] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (clientState?.state !== "valid") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const { authenticated, supportedFeatures } = clientState;
|
|
|
|
|
const client = authenticated?.client;
|
|
|
|
|
|
|
|
|
|
if (!client || !src || !sizePx || !supportedFeatures.thumbnails) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = client.getAccessToken();
|
|
|
|
|
if (!token) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const resolveSrc = getAvatarUrl(client, src, sizePx);
|
|
|
|
|
if (!resolveSrc) {
|
|
|
|
|
setAvatarUrl(undefined);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let objectUrl: string | undefined;
|
|
|
|
|
fetch(resolveSrc, {
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
.then(async (req) => req.blob())
|
|
|
|
|
.then((blob) => {
|
|
|
|
|
objectUrl = URL.createObjectURL(blob);
|
|
|
|
|
setAvatarUrl(objectUrl);
|
|
|
|
|
})
|
|
|
|
|
.catch((ex) => {
|
|
|
|
|
setAvatarUrl(undefined);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (): void => {
|
|
|
|
|
if (objectUrl) {
|
|
|
|
|
URL.revokeObjectURL(objectUrl);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, [clientState, src, sizePx]);
|
2022-05-18 19:00:59 -04:00
|
|
|
|
2021-12-07 17:59:55 -08:00
|
|
|
return (
|
2023-08-31 15:46:09 +02:00
|
|
|
<CompoundAvatar
|
|
|
|
|
className={className}
|
|
|
|
|
id={id}
|
|
|
|
|
name={name}
|
|
|
|
|
size={`${sizePx}px`}
|
2024-12-06 18:30:05 +00:00
|
|
|
src={avatarUrl}
|
2024-11-25 20:22:02 +00:00
|
|
|
style={style}
|
|
|
|
|
{...props}
|
2023-08-31 15:46:09 +02:00
|
|
|
/>
|
2021-12-07 17:59:55 -08:00
|
|
|
);
|
2022-05-06 11:32:09 +01:00
|
|
|
};
|