element-call-Github/src/Facepile.tsx

98 lines
2.5 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.
*/
2022-10-10 21:19:10 +08:00
import React, { HTMLAttributes, useMemo } from "react";
2021-11-11 07:44:08 +08:00
import classNames from "classnames";
2022-08-13 04:46:53 +08:00
import { MatrixClient } from "matrix-js-sdk/src/client";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
2022-10-10 21:19:10 +08:00
import { useTranslation } from "react-i18next";
2021-10-05 06:37:23 +08:00
import styles from "./Facepile.module.css";
import { Avatar, Size, sizes } from "./Avatar";
const overlapMap: Partial<Record<Size, number>> = {
[Size.XS]: 2,
[Size.SM]: 4,
[Size.MD]: 8,
2022-04-28 08:19:58 +08:00
};
interface Props extends HTMLAttributes<HTMLDivElement> {
className: string;
client: MatrixClient;
members: RoomMember[];
max?: number;
size?: Size;
}
2022-04-28 07:47:23 +08:00
export function Facepile({
className,
client,
members,
max = 3,
size = Size.XS,
2022-04-28 07:47:23 +08:00
...rest
}: Props) {
2022-10-10 21:19:10 +08:00
const { t } = useTranslation();
2022-05-19 07:00:59 +08:00
const _size = sizes.get(size);
2022-04-28 08:19:58 +08:00
const _overlap = overlapMap[size];
2022-04-28 07:47:23 +08:00
2022-10-10 21:19:10 +08:00
const title = useMemo(() => {
return members.reduce<string | null>(
2022-10-10 21:19:10 +08:00
(prev, curr) =>
prev === null
? curr.name
: t("{{names}}, {{name}}", { names: prev, name: curr.name }),
null
) as string;
}, [members, t]);
2022-10-10 21:19:10 +08:00
2021-10-05 06:37:23 +08:00
return (
<div
2022-04-28 08:19:58 +08:00
className={classNames(styles.facepile, styles[size], className)}
2022-10-10 21:19:10 +08:00
title={title}
style={{
width:
Math.min(members.length, max + 1) * (_size - _overlap) + _overlap,
}}
2021-12-08 09:59:55 +08:00
{...rest}
2021-10-05 06:37:23 +08:00
>
{members.slice(0, max).map((member, i) => {
const avatarUrl = member.getMxcAvatarUrl();
2021-12-21 01:12:28 +08:00
return (
<Avatar
key={member.userId}
2022-04-28 07:47:23 +08:00
size={size}
src={avatarUrl ?? undefined}
2021-12-21 01:12:28 +08:00
fallback={member.name.slice(0, 1).toUpperCase()}
className={styles.avatar}
2022-04-28 07:47:23 +08:00
style={{ left: i * (_size - _overlap) }}
2021-12-21 01:12:28 +08:00
/>
);
})}
{members.length > max && (
2021-12-08 09:59:55 +08:00
<Avatar
2021-11-11 07:44:08 +08:00
key="additional"
2022-04-28 07:47:23 +08:00
size={size}
fallback={`+${members.length - max}`}
2021-12-08 09:59:55 +08:00
className={styles.avatar}
2022-04-28 07:47:23 +08:00
style={{ left: max * (_size - _overlap) }}
2021-12-08 09:59:55 +08:00
/>
2021-11-11 07:44:08 +08:00
)}
2021-10-05 06:37:23 +08:00
</div>
);
}