reactions.ts
Raw
1interface ReactionRow {
2 emoji: string;
3 comment_id: number | null;
4 user_id: number;
5}
6
7export function buildReactionCounts(
8 reactions: ReactionRow[],
9 commentId: number | null,
10 userId: number | undefined,
11): { emoji: string; count: number; userReacted: boolean }[] {
12 const filtered = reactions.filter((r) =>
13 commentId === null ? r.comment_id === null : r.comment_id === commentId,
14 );
15 const map = new Map<string, { count: number; userReacted: boolean }>();
16 for (const r of filtered) {
17 const entry = map.get(r.emoji) ?? { count: 0, userReacted: false };
18 entry.count++;
19 if (userId !== undefined && r.user_id === userId)
20 entry.userReacted = true;
21 map.set(r.emoji, entry);
22 }
23 return [...map.entries()].map(([emoji, d]) => ({ emoji, ...d }));
24}
25