This commit is contained in:
Will Hunt
2024-12-02 13:56:55 +00:00
parent 773492d458
commit 74fa5e4bdf
2 changed files with 36 additions and 19 deletions
+26 -9
View File
@@ -7,9 +7,9 @@ Please see LICENSE in the repository root for full details.
import { useMemo, FC, CSSProperties, useState, useEffect } from "react"; import { useMemo, FC, CSSProperties, useState, useEffect } from "react";
import { Avatar as CompoundAvatar } from "@vector-im/compound-web"; import { Avatar as CompoundAvatar } from "@vector-im/compound-web";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { useClient } from "./ClientContext"; import { useClient } from "./ClientContext";
import { MatrixClient } from "matrix-js-sdk/src/client";
export enum Size { export enum Size {
XS = "xs", XS = "xs",
@@ -45,7 +45,17 @@ export function getAvatarUrl(
const height = Math.floor(avatarSize * window.devicePixelRatio); const height = Math.floor(avatarSize * window.devicePixelRatio);
// scale is more suitable for larger sizes // scale is more suitable for larger sizes
const resizeMethod = avatarSize <= 96 ? "crop" : "scale"; const resizeMethod = avatarSize <= 96 ? "crop" : "scale";
return mxcUrl ? client.mxcUrlToHttp(mxcUrl, width, height, resizeMethod, false, true, true) : null; return mxcUrl
? client.mxcUrlToHttp(
mxcUrl,
width,
height,
resizeMethod,
false,
true,
true,
)
: null;
} }
export const Avatar: FC<Props> = ({ export const Avatar: FC<Props> = ({
@@ -72,7 +82,7 @@ export const Avatar: FC<Props> = ({
useEffect(() => { useEffect(() => {
if (!client || !src || !sizePx) { if (!client || !src || !sizePx) {
return; return;
}; }
const token = client.getAccessToken(); const token = client.getAccessToken();
if (!token) { if (!token) {
return; return;
@@ -86,17 +96,24 @@ export const Avatar: FC<Props> = ({
let objectUrl: string | undefined; let objectUrl: string | undefined;
fetch(resolveSrc, { fetch(resolveSrc, {
headers: { headers: {
'Authorization': `Bearer ${token}` Authorization: `Bearer ${token}`,
} },
}).then((req) => req.blob()).then((res) => { })
.then(async (req) => req.blob())
.then((res) => {
objectUrl = URL.createObjectURL(res); objectUrl = URL.createObjectURL(res);
setAvatarUrl(objectUrl); setAvatarUrl(objectUrl);
}).catch((ex) => { })
console.warn("Failed to get avatar URL", ex); .catch(() => {
// Error will likely be evident from browser logs.
setAvatarUrl(undefined); setAvatarUrl(undefined);
}); });
() => objectUrl && URL.revokeObjectURL(objectUrl); return (): void => {
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
};
}, [client, src, sizePx]); }, [client, src, sizePx]);
return ( return (