Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

pullRequestReactions.logic.ts

95 lines3.4 KB

1import type { PullRequestReaction, PullRequestReactionContent } from "@t3tools/contracts";
2​
3export const PULL_REQUEST_REACTION_ORDER: ReadonlyArray<PullRequestReactionContent> = [
4 "thumbs-up",
5 "thumbs-down",
6 "laugh",
7 "hooray",
8 "confused",
9 "heart",
10 "rocket",
11 "eyes",
12];
13​
14const REACTION_EMOJI: Record<PullRequestReactionContent, string> = {
15 "thumbs-up": "πŸ‘",
16 "thumbs-down": "πŸ‘Ž",
17 laugh: "πŸ˜„",
18 hooray: "πŸŽ‰",
19 confused: "πŸ˜•",
20 heart: "❀️",
21 rocket: "πŸš€",
22 eyes: "πŸ‘€",
23};
24​
25const REACTION_NAME: Record<PullRequestReactionContent, string> = {
26 "thumbs-up": "thumbs up",
27 "thumbs-down": "thumbs down",
28 laugh: "laugh",
29 hooray: "hooray",
30 confused: "confused",
31 heart: "heart",
32 rocket: "rocket",
33 eyes: "eyes",
34};
35​
36export function pullRequestReactionEmoji(content: PullRequestReactionContent): string {
37 return REACTION_EMOJI[content];
39​
40export function pullRequestReactionName(content: PullRequestReactionContent): string {
41 return REACTION_NAME[content];
43​
44const NAMED_ACTOR_LIMIT = 3;
45​
46function joinNames(parts: ReadonlyArray<string>): string {
47 if (parts.length <= 1) return parts[0] ?? "";
48 if (parts.length === 2) return `${parts[0]} and ${parts[1]}`;
49 return `${parts.slice(0, -1).join(", ")}, and ${parts.at(-1)}`;
51​
52function countRemainder(count: number, named: boolean): string {
53 if (named) return `${count} ${count === 1 ? "other" : "others"}`;
54 return `${count} ${count === 1 ? "person" : "people"}`;
56​
57/**
58 * The viewer reads as "You" and comes first, because that is the name they are looking for; a
59 * compliant host already leaves the viewer's own login out of `actors` when they have reacted,
60 * leaving room for "You" without ever exceeding `count`. This function has no viewer login to
61 * match against `actors`, so it cannot tell a non-compliant host apart by name β€” but such a host
62 * gives itself away by leaving no room: `actors` alone already accounts for everyone `count`
63 * claims.
64 */
65export function pullRequestReactionTooltip(reaction: PullRequestReaction): string {
66 const viewerHasRoom = reaction.actors.length < reaction.count;
67 const names =
68 reaction.viewerHasReacted && viewerHasRoom ? ["You", ...reaction.actors] : [...reaction.actors];
69 const shown = names.slice(0, Math.min(NAMED_ACTOR_LIMIT, reaction.count));
70 const others = Math.max(0, reaction.count - shown.length);
71 const parts = [...shown, ...(others > 0 ? [countRemainder(others, shown.length > 0)] : [])];
72 return `${joinNames(parts)} reacted with ${pullRequestReactionName(reaction.content)} emoji`;
74​
75export function applyPendingPullRequestReactions(
76 reactions: ReadonlyArray<PullRequestReaction>,
77 pending: ReadonlyMap<PullRequestReactionContent, boolean>,
78): ReadonlyArray<PullRequestReaction> {
79 if (pending.size === 0) return reactions;
80 const byContent = new Map(reactions.map((reaction) => [reaction.content, reaction] as const));
81 for (const [content, reacted] of pending) {
82 const current = byContent.get(content);
83 if (current === undefined) {
84 if (reacted) {
85 byContent.set(content, { content, count: 1, actors: [], viewerHasReacted: true });
86 }
87 continue;
88 }
89 if (current.viewerHasReacted === reacted) continue;
90 const count = current.count + (reacted ? 1 : -1);
91 if (count <= 0) byContent.delete(content);
92 else byContent.set(content, { ...current, count, viewerHasReacted: reacted });
93 }
94 return PULL_REQUEST_REACTION_ORDER.flatMap((content) => byContent.get(content) ?? []);