/* Copyright 2022 The Matrix.org Foundation C.I.C. 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 React, { PropsWithChildren, useRef } from "react"; import { User } from "matrix-js-sdk/src/matrix"; import ReadReceiptMarker, { IReadReceiptInfo } from "./ReadReceiptMarker"; import { IReadReceiptProps } from "./EventTile"; import AccessibleButton from "../elements/AccessibleButton"; import MemberAvatar from "../avatars/MemberAvatar"; import AutoHideScrollbar from "../../structures/AutoHideScrollbar"; import { Alignment } from "../elements/Tooltip"; import { formatDate } from "../../../DateUtils"; import { Action } from "../../../dispatcher/actions"; import dis from "../../../dispatcher/dispatcher"; import ContextMenu, { aboveLeftOf, MenuItem, useContextMenu } from "../../structures/ContextMenu"; import { useTooltip } from "../../../utils/useTooltip"; import { _t } from "../../../languageHandler"; import { useRovingTabIndex } from "../../../accessibility/RovingTabIndex"; // #20547 Design specified that we should show the three latest read receipts const MAX_READ_AVATARS_PLUS_N = 3; // #21935 If we’ve got just 4, don’t show +1, just show all of them const MAX_READ_AVATARS = MAX_READ_AVATARS_PLUS_N + 1; const READ_AVATAR_OFFSET = 10; export const READ_AVATAR_SIZE = 16; interface Props { readReceipts: IReadReceiptProps[]; readReceiptMap: { [userId: string]: IReadReceiptInfo }; checkUnmounting?: () => boolean; suppressAnimation: boolean; isTwelveHour?: boolean; } interface IAvatarPosition { hidden: boolean; position: number; } export function determineAvatarPosition(index: number, max: number): IAvatarPosition { if (index < max) { return { hidden: false, position: index, }; } else { return { hidden: true, position: 0, }; } } export function readReceiptTooltip(members: string[], hasMore: boolean): string | undefined { if (hasMore) { return _t("%(members)s and more", { members: members.join(", "), }); } else if (members.length > 1) { return _t("%(members)s and %(last)s", { last: members.pop(), members: members.join(", "), }); } else if (members.length) { return members[0]; } } export function ReadReceiptGroup({ readReceipts, readReceiptMap, checkUnmounting, suppressAnimation, isTwelveHour, }: Props): JSX.Element { const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu(); // If we are above MAX_READ_AVATARS, we’ll have to remove a few to have space for the +n count. const hasMore = readReceipts.length > MAX_READ_AVATARS; const maxAvatars = hasMore ? MAX_READ_AVATARS_PLUS_N : MAX_READ_AVATARS; const tooltipMembers: string[] = readReceipts.slice(0, maxAvatars).map((it) => it.roomMember?.name ?? it.userId); const tooltipText = readReceiptTooltip(tooltipMembers, hasMore); const [{ showTooltip, hideTooltip }, tooltip] = useTooltip({ label: ( <>