Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 3e31fdb

Browse files
authored
read receipts: improve tooltips to show names of users (#8438)
1 parent 4831129 commit 3e31fdb

File tree

3 files changed

+124
-12
lines changed

3 files changed

+124
-12
lines changed

src/components/views/rooms/ReadReceiptGroup.tsx

+42-12
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ interface IAvatarPosition {
5252
position: number;
5353
}
5454

55-
function determineAvatarPosition(index: number, count: number, max: number): IAvatarPosition {
56-
const firstVisible = Math.max(0, count - max);
57-
58-
if (index >= firstVisible) {
55+
export function determineAvatarPosition(index: number, count: number, max: number): IAvatarPosition {
56+
if (index < max) {
5957
return {
6058
hidden: false,
61-
position: index - firstVisible,
59+
position: Math.min(count, max) - index - 1,
6260
};
6361
} else {
6462
return {
@@ -68,12 +66,49 @@ function determineAvatarPosition(index: number, count: number, max: number): IAv
6866
}
6967
}
7068

69+
export function readReceiptTooltip(members: string[], hasMore: boolean): string | null {
70+
if (hasMore) {
71+
return _t("%(members)s and more", {
72+
members: members.join(", "),
73+
});
74+
} else if (members.length > 1) {
75+
return _t("%(members)s and %(last)s", {
76+
last: members.pop(),
77+
members: members.join(", "),
78+
});
79+
} else if (members.length) {
80+
return members[0];
81+
} else {
82+
return null;
83+
}
84+
}
85+
7186
export function ReadReceiptGroup(
7287
{ readReceipts, readReceiptMap, checkUnmounting, suppressAnimation, isTwelveHour }: Props,
7388
) {
7489
const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu();
90+
91+
// If we are above MAX_READ_AVATARS, we’ll have to remove a few to have space for the +n count.
92+
const hasMore = readReceipts.length > MAX_READ_AVATARS;
93+
const maxAvatars = hasMore
94+
? MAX_READ_AVATARS_PLUS_N
95+
: MAX_READ_AVATARS;
96+
97+
const tooltipMembers: string[] = readReceipts.slice(0, maxAvatars)
98+
.map(it => it.roomMember?.name ?? it.userId);
99+
const tooltipText = readReceiptTooltip(tooltipMembers, hasMore);
100+
75101
const [{ showTooltip, hideTooltip }, tooltip] = useTooltip({
76-
label: _t("Seen by %(count)s people", { count: readReceipts.length }),
102+
label: (
103+
<>
104+
<div className="mx_Tooltip_title">
105+
{ _t("Seen by %(count)s people", { count: readReceipts.length }) }
106+
</div>
107+
<div className="mx_Tooltip_sub">
108+
{ tooltipText }
109+
</div>
110+
</>
111+
),
77112
alignment: Alignment.TopRight,
78113
});
79114

@@ -97,11 +132,6 @@ export function ReadReceiptGroup(
97132
);
98133
}
99134

100-
// If we are above MAX_READ_AVATARS, we’ll have to remove a few to have space for the +n count.
101-
const maxAvatars = readReceipts.length > MAX_READ_AVATARS
102-
? MAX_READ_AVATARS_PLUS_N
103-
: MAX_READ_AVATARS;
104-
105135
const avatars = readReceipts.map((receipt, index) => {
106136
const { hidden, position } = determineAvatarPosition(index, readReceipts.length, maxAvatars);
107137

@@ -130,7 +160,7 @@ export function ReadReceiptGroup(
130160
showTwelveHour={isTwelveHour}
131161
/>
132162
);
133-
});
163+
}).reverse();
134164

135165
let remText: JSX.Element;
136166
const remainder = readReceipts.length - maxAvatars;

src/i18n/strings/en_EN.json

+2
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,8 @@
17661766
"Preview": "Preview",
17671767
"View": "View",
17681768
"Join": "Join",
1769+
"%(members)s and more": "%(members)s and more",
1770+
"%(members)s and %(last)s": "%(members)s and %(last)s",
17691771
"Seen by %(count)s people|other": "Seen by %(count)s people",
17701772
"Seen by %(count)s people|one": "Seen by %(count)s person",
17711773
"Recently viewed": "Recently viewed",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { determineAvatarPosition, readReceiptTooltip } from "../../../../src/components/views/rooms/ReadReceiptGroup";
2+
3+
describe("ReadReceiptGroup", () => {
4+
describe("TooltipText", () => {
5+
it("returns '...and more' with hasMore", () => {
6+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve"], true))
7+
.toEqual("Alice, Bob, Charlie, Dan, Eve and more");
8+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan"], true))
9+
.toEqual("Alice, Bob, Charlie, Dan and more");
10+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie"], true))
11+
.toEqual("Alice, Bob, Charlie and more");
12+
expect(readReceiptTooltip(["Alice", "Bob"], true))
13+
.toEqual("Alice, Bob and more");
14+
expect(readReceiptTooltip(["Alice"], true))
15+
.toEqual("Alice and more");
16+
expect(readReceiptTooltip([], false))
17+
.toEqual(null);
18+
});
19+
it("returns a pretty list without hasMore", () => {
20+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan", "Eve"], false))
21+
.toEqual("Alice, Bob, Charlie, Dan and Eve");
22+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie", "Dan"], false))
23+
.toEqual("Alice, Bob, Charlie and Dan");
24+
expect(readReceiptTooltip(["Alice", "Bob", "Charlie"], false))
25+
.toEqual("Alice, Bob and Charlie");
26+
expect(readReceiptTooltip(["Alice", "Bob"], false))
27+
.toEqual("Alice and Bob");
28+
expect(readReceiptTooltip(["Alice"], false))
29+
.toEqual("Alice");
30+
expect(readReceiptTooltip([], false))
31+
.toEqual(null);
32+
});
33+
});
34+
describe("AvatarPosition", () => {
35+
// The avatar slots are numbered from right to left
36+
// That means currently, we’ve got the slots | 3 | 2 | 1 | 0 | each with 10px distance to the next one.
37+
// We want to fill slots so the first avatar is in the left-most slot without leaving any slots at the right
38+
// unoccupied.
39+
it("to handle the non-overflowing case correctly", () => {
40+
expect(determineAvatarPosition(0, 1, 4))
41+
.toEqual({ hidden: false, position: 0 });
42+
43+
expect(determineAvatarPosition(0, 2, 4))
44+
.toEqual({ hidden: false, position: 1 });
45+
expect(determineAvatarPosition(1, 2, 4))
46+
.toEqual({ hidden: false, position: 0 });
47+
48+
expect(determineAvatarPosition(0, 3, 4))
49+
.toEqual({ hidden: false, position: 2 });
50+
expect(determineAvatarPosition(1, 3, 4))
51+
.toEqual({ hidden: false, position: 1 });
52+
expect(determineAvatarPosition(2, 3, 4))
53+
.toEqual({ hidden: false, position: 0 });
54+
55+
expect(determineAvatarPosition(0, 4, 4))
56+
.toEqual({ hidden: false, position: 3 });
57+
expect(determineAvatarPosition(1, 4, 4))
58+
.toEqual({ hidden: false, position: 2 });
59+
expect(determineAvatarPosition(2, 4, 4))
60+
.toEqual({ hidden: false, position: 1 });
61+
expect(determineAvatarPosition(3, 4, 4))
62+
.toEqual({ hidden: false, position: 0 });
63+
});
64+
65+
it("to handle the overflowing case correctly", () => {
66+
expect(determineAvatarPosition(0, 6, 4))
67+
.toEqual({ hidden: false, position: 3 });
68+
expect(determineAvatarPosition(1, 6, 4))
69+
.toEqual({ hidden: false, position: 2 });
70+
expect(determineAvatarPosition(2, 6, 4))
71+
.toEqual({ hidden: false, position: 1 });
72+
expect(determineAvatarPosition(3, 6, 4))
73+
.toEqual({ hidden: false, position: 0 });
74+
expect(determineAvatarPosition(4, 6, 4))
75+
.toEqual({ hidden: true, position: 0 });
76+
expect(determineAvatarPosition(5, 6, 4))
77+
.toEqual({ hidden: true, position: 0 });
78+
});
79+
});
80+
});

0 commit comments

Comments
 (0)