element-call-Github/src/Avatar.tsx

132 lines
3.0 KiB
TypeScript
Raw Normal View History

/*
Copyright 2022 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { useMemo, CSSProperties, HTMLAttributes, FC } from "react";
2021-12-08 09:59:55 +08:00
import classNames from "classnames";
2022-05-19 07:00:59 +08:00
import { MatrixClient } from "matrix-js-sdk/src/client";
2022-05-06 18:32:09 +08:00
2022-05-19 07:00:59 +08:00
import { getAvatarUrl } from "./matrix-utils";
import { useClient } from "./ClientContext";
2021-12-08 09:59:55 +08:00
import styles from "./Avatar.module.css";
const backgroundColors = [
"#5C56F5",
"#03B381",
"#368BD6",
"#AC3BA8",
"#E64F7A",
"#FF812D",
"#2DC2C5",
"#74D12C",
];
2022-05-19 07:00:59 +08: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 18:32:09 +08:00
function hashStringToArrIndex(str: string, arrLength: number) {
2021-12-08 09:59:55 +08:00
let sum = 0;
for (let i = 0; i < str.length; i++) {
sum += str.charCodeAt(i);
}
return sum % arrLength;
}
2022-05-19 07:00:59 +08:00
const resolveAvatarSrc = (client: MatrixClient, src: string, size: number) =>
src?.startsWith("mxc://") ? client && getAvatarUrl(client, src, size) : src;
interface Props extends HTMLAttributes<HTMLDivElement> {
2022-05-06 18:32:09 +08:00
bgKey?: string;
src?: string;
2022-05-19 07:00:59 +08:00
size?: Size | number;
className?: string;
2022-05-19 07:00:59 +08:00
style?: CSSProperties;
fallback: string;
2022-05-06 18:32:09 +08:00
}
export const Avatar: FC<Props> = ({
2021-12-08 09:59:55 +08:00
bgKey,
src,
fallback,
2022-05-19 07:00:59 +08:00
size = Size.MD,
2021-12-08 09:59:55 +08:00
className,
2022-05-19 07:00:59 +08:00
style = {},
2021-12-08 09:59:55 +08:00
...rest
2022-05-06 18:32:09 +08:00
}) => {
2022-05-19 07:00:59 +08: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(() => {
if (!client || !src || !sizePx) return undefined;
return resolveAvatarSrc(client, src, sizePx);
}, [client, src, sizePx]);
2022-05-19 07:00:59 +08:00
2021-12-08 09:59:55 +08:00
const backgroundColor = useMemo(() => {
const index = hashStringToArrIndex(
bgKey || fallback || src || "",
2021-12-08 09:59:55 +08:00
backgroundColors.length
);
return backgroundColors[index];
}, [bgKey, src, fallback]);
/* eslint-disable jsx-a11y/alt-text */
2021-12-08 09:59:55 +08:00
return (
<div
2022-05-19 07:00:59 +08:00
className={classNames(styles.avatar, sizeClass, className)}
style={{ backgroundColor, ...sizeStyle, ...style }}
2021-12-08 09:59:55 +08:00
{...rest}
>
2022-05-19 07:00:59 +08:00
{resolvedSrc ? (
<img src={resolvedSrc} />
2021-12-08 09:59:55 +08:00
) : typeof fallback === "string" ? (
<span>{fallback}</span>
) : (
fallback
)}
</div>
);
2022-05-06 18:32:09 +08:00
};