element-call/src/Avatar.tsx

116 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-05-18 23:00:59 +00:00
import React, { useMemo, CSSProperties } from "react";
2021-12-08 01:59:55 +00:00
import classNames from "classnames";
2022-05-18 23:00:59 +00:00
import { MatrixClient } from "matrix-js-sdk/src/client";
2022-05-06 10:32:09 +00:00
2022-05-18 23:00:59 +00:00
import { getAvatarUrl } from "./matrix-utils";
import { useClient } from "./ClientContext";
2021-12-08 01:59:55 +00:00
import styles from "./Avatar.module.css";
const backgroundColors = [
"#5C56F5",
"#03B381",
"#368BD6",
"#AC3BA8",
"#E64F7A",
"#FF812D",
"#2DC2C5",
"#74D12C",
];
2022-05-18 23:00:59 +00: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],
]);
2022-05-06 10:32:09 +00:00
function hashStringToArrIndex(str: string, arrLength: number) {
2021-12-08 01:59:55 +00:00
let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += str.charCodeAt(i);
}
return sum % arrLength;
}
2022-05-18 23:00:59 +00:00
const resolveAvatarSrc = (client: MatrixClient, src: string, size: number) =>
src?.startsWith("mxc://") ? client && getAvatarUrl(client, src, size) : src;
2022-05-06 10:32:09 +00:00
interface Props extends React.HTMLAttributes<HTMLDivElement> {
bgKey?: string;
src: string;
fallback: string;
2022-05-18 23:00:59 +00:00
size?: Size | number;
2022-05-06 10:32:09 +00:00
className: string;
2022-05-18 23:00:59 +00:00
style?: CSSProperties;
2022-05-06 10:32:09 +00:00
}
export const Avatar: React.FC<Props> = ({
2021-12-08 01:59:55 +00:00
bgKey,
src,
fallback,
2022-05-18 23:00:59 +00:00
size = Size.MD,
2021-12-08 01:59:55 +00:00
className,
2022-05-18 23:00:59 +00:00
style = {},
2021-12-08 01:59:55 +00:00
...rest
2022-05-06 10:32:09 +00:00
}) => {
2022-05-18 23:00:59 +00:00
const { client } = useClient();
const [sizeClass, sizePx, sizeStyle] = useMemo(
() =>
Object.values(Size).includes(size as Size)
? [styles[size as string], sizes.get(size as Size), {}]
: [
null,
size as number,
{
width: size,
height: size,
borderRadius: size,
fontSize: Math.round((size as number) / 2),
},
],
[size]
);
const resolvedSrc = useMemo(
() => resolveAvatarSrc(client, src, sizePx),
[client, src, sizePx]
);
2021-12-08 01:59:55 +00:00
const backgroundColor = useMemo(() => {
const index = hashStringToArrIndex(
bgKey || fallback || src || "",
2021-12-08 01:59:55 +00:00
backgroundColors.length
);
return backgroundColors[index];
}, [bgKey, src, fallback]);
/* eslint-disable jsx-a11y/alt-text */
2021-12-08 01:59:55 +00:00
return (
<div
2022-05-18 23:00:59 +00:00
className={classNames(styles.avatar, sizeClass, className)}
style={{ backgroundColor, ...sizeStyle, ...style }}
2021-12-08 01:59:55 +00:00
{...rest}
>
2022-05-18 23:00:59 +00:00
{resolvedSrc ? (
<img src={resolvedSrc} />
2021-12-08 01:59:55 +00:00
) : typeof fallback === "string" ? (
<span>{fallback}</span>
) : (
fallback
)}
</div>
);
2022-05-06 10:32:09 +00:00
};