interface ReactionRow { emoji: string; comment_id: number | null; user_id: number; } export function buildReactionCounts( reactions: ReactionRow[], commentId: number | null, userId: number | undefined, ): { emoji: string; count: number; userReacted: boolean }[] { const filtered = reactions.filter((r) => commentId === null ? r.comment_id === null : r.comment_id === commentId, ); const map = new Map(); for (const r of filtered) { const entry = map.get(r.emoji) ?? { count: 0, userReacted: false }; entry.count++; if (userId !== undefined && r.user_id === userId) entry.userReacted = true; map.set(r.emoji, entry); } return [...map.entries()].map(([emoji, d]) => ({ emoji, ...d })); }