Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions resources/js/lib/reactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ export function summarizeReactions(
const byEmoji = new Map<string, ReactionSummary>();

for (const reaction of reactions ?? []) {
const summary = byEmoji.get(reaction.emoji) ?? {
emoji: reaction.emoji,
count: 0,
isMine: false,
userIds: [],
};

byEmoji.set(reaction.emoji, {
...summary,
count: summary.count + 1,
isMine: summary.isMine || reaction.user_id === userId,
userIds: [...summary.userIds, reaction.user_id],
});
const summary = byEmoji.get(reaction.emoji);

if (summary) {
summary.count++;
summary.isMine ||= reaction.user_id === userId;
summary.userIds.push(reaction.user_id);
} else {
byEmoji.set(reaction.emoji, {
emoji: reaction.emoji,
count: 1,
isMine: reaction.user_id === userId,
userIds: [reaction.user_id],
});
}
}

return [...byEmoji.values()];
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Reactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,22 @@ await test('takes two quick taps close together as a double tap', () => {
assert.equal(isDoubleTap(first, { x: 160, y: 200, time: 1_100 }), false);
assert.equal(isDoubleTap(null, first), false);
});

await test('summarizes repeated reactions without changing their order', () => {
const reactions = Array.from({ length: 120 }, (_, index) => ({
user_id: String(index),
emoji: index % 2 === 0 ? '🚀' : '❤️',
}));
const summaries = summarizeReactions(reactions, '63');

assert.deepEqual(summaries.map(({ emoji, count, isMine }) => ({
emoji,
count,
isMine,
})), [
{ emoji: '🚀', count: 60, isMine: false },
{ emoji: '❤️', count: 60, isMine: true },
]);
assert.equal(summaries[0].userIds[0], '0');
assert.equal(summaries[0].userIds.at(-1), '118');
});