Open History to see the last commit
1import { mFallback, msg, plural, part, type Msg } from "gt-i18n";
2import type { M } from "gt-i18n";
3import { formatConjunctionList } from "./listFormat.ts";
4import type {
5 PullRequestComment,
6 PullRequestDetail,
7 PullRequestReaction,
8 PullRequestReactionContent,
9} from "@t3tools/contracts";
10
11export function canReactPullRequestComment(
12 detail: Pick<PullRequestDetail, "provider" | "capabilities">,
13 comment: Pick<PullRequestComment, "kind">,
14): boolean {
15 return (
16 detail.capabilities.reactions === true &&
17 !(detail.provider === "gitcafe" && comment.kind === "review")
18 );
19}
20
21export const PULL_REQUEST_REACTION_ORDER: ReadonlyArray<PullRequestReactionContent> = [
22 "thumbs-up",
23 "thumbs-down",
24 "laugh",
25 "hooray",
26 "confused",
27 "heart",
28 "rocket",
29 "eyes",
30];
31
32const REACTION_EMOJI: Record<PullRequestReactionContent, string> = {
33 "thumbs-up": "👍",
34 "thumbs-down": "👎",
35 laugh: "😄",
36 hooray: "🎉",
37 confused: "😕",
38 heart: "❤️",
39 rocket: "🚀",
40 eyes: "👀",
41};
42
43const REACTION_NAME: Record<PullRequestReactionContent, string | Msg> = {
44 "thumbs-up": msg({ context: "reaction emoji name" })`thumbs up`,
45 "thumbs-down": msg({ context: "reaction emoji name" })`thumbs down`,
46 laugh: msg({ context: "reaction emoji name" })`laugh`,
47 hooray: msg({ context: "reaction emoji name" })`hooray`,
48 confused: msg({ context: "reaction emoji name" })`confused`,
49 heart: msg({ context: "reaction emoji name" })`heart`,
50 rocket: msg({ context: "reaction emoji name" })`rocket`,
51 eyes: msg({ context: "reaction emoji name" })`eyes`,
52};
53
54export function pullRequestReactionEmoji(content: PullRequestReactionContent): string {
55 return REACTION_EMOJI[content];
56}
57
58export function pullRequestReactionName(
59 content: PullRequestReactionContent,
60 resolve: M = mFallback,
61): string {
62 return resolve(REACTION_NAME[content]);
63}
64
65const NAMED_ACTOR_LIMIT = 3;
66
67function countRemainder(count: number, named: boolean, resolve: M): string {
68 return named
69 ? resolve(
70 msg`${plural(
71 { count },
72 {
73 one: part`${{ count }} other`,
74 other: part`${{ count }} others`,
75 },
76 )}`,
77 )
78 : resolve(
79 msg`${plural(
80 { count },
81 {
82 one: part`${{ count }} person`,
83 other: part`${{ count }} people`,
84 },
85 )}`,
86 );
87}
88
89function reactedWith(content: PullRequestReactionContent, names: string, resolve: M): string {
90 switch (content) {
91 case "thumbs-up":
92 return resolve(msg`${{ names }} reacted with thumbs up emoji`);
93 case "thumbs-down":
94 return resolve(msg`${{ names }} reacted with thumbs down emoji`);
95 case "laugh":
96 return resolve(msg`${{ names }} reacted with laugh emoji`);
97 case "hooray":
98 return resolve(msg`${{ names }} reacted with hooray emoji`);
99 case "confused":
100 return resolve(msg`${{ names }} reacted with confused emoji`);
101 case "heart":
102 return resolve(msg`${{ names }} reacted with heart emoji`);
103 case "rocket":
104 return resolve(msg`${{ names }} reacted with rocket emoji`);
105 case "eyes":
106 return resolve(msg`${{ names }} reacted with eyes emoji`);
107 }
108}
109
110/** Hosts omit the viewer from actors; add You only if count has room, since viewer login is unavailable. */
111export function pullRequestReactionTooltip(
112 reaction: PullRequestReaction,
113 resolve: M = mFallback,
114 locale = "en",
115): string {
116 const viewerHasRoom = reaction.actors.length < reaction.count;
117 const names =
118 reaction.viewerHasReacted && viewerHasRoom
119 ? [
120 resolve(msg({ context: "the viewer, in a list of people who reacted" })`You`),
121 ...reaction.actors,
122 ]
123 : [...reaction.actors];
124 const shown = names.slice(0, Math.min(NAMED_ACTOR_LIMIT, reaction.count));
125 const others = Math.max(0, reaction.count - shown.length);
126 const parts = [
127 ...shown,
128 ...(others > 0 ? [countRemainder(others, shown.length > 0, resolve)] : []),
129 ];
130 return reactedWith(reaction.content, formatConjunctionList(parts, locale, resolve), resolve);
131}
132
133/** Changes whenever the host reports different reactions, so pending overlays can reset. */
134export function pullRequestReactionsSignature(
135 reactions: ReadonlyArray<PullRequestReaction>,
136): string {
137 return reactions
138 .map((reaction) => `${reaction.content}:${reaction.count}:${reaction.viewerHasReacted ? 1 : 0}`)
139 .join(" ");
140}
141
142/** One toggle awaiting the host. Its identity tells it apart from a later toggle of the same reaction. */
143export interface PendingPullRequestReaction {
144 readonly reacted: boolean;
145 /** The host saved it, so the next reactions it reports already account for it. */
146 readonly saved?: boolean;
147}
148
149/** The toggles still worth showing once the host reports new reactions. */
150export function retainPendingPullRequestReactions(
151 reactions: ReadonlyArray<PullRequestReaction>,
152 pending: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction>,
153): ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction> {
154 const reacted = new Set(
155 reactions.filter((reaction) => reaction.viewerHasReacted).map((reaction) => reaction.content),
156 );
157 return new Map(
158 [...pending].filter(
159 ([content, entry]) => !entry.saved && reacted.has(content) !== entry.reacted,
160 ),
161 );
162}
163
164export function applyPendingPullRequestReactions(
165 reactions: ReadonlyArray<PullRequestReaction>,
166 pending: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction>,
167): ReadonlyArray<PullRequestReaction> {
168 if (pending.size === 0) return reactions;
169 const byContent = new Map(reactions.map((reaction) => [reaction.content, reaction] as const));
170 for (const [content, { reacted }] of pending) {
171 const current = byContent.get(content);
172 if (current === undefined) {
173 if (reacted) {
174 byContent.set(content, { content, count: 1, actors: [], viewerHasReacted: true });
175 }
176 continue;
177 }
178 if (current.viewerHasReacted === reacted) continue;
179 const count = current.count + (reacted ? 1 : -1);
180 if (count <= 0) byContent.delete(content);
181 else byContent.set(content, { ...current, count, viewerHasReacted: reacted });
182 }
183 return PULL_REQUEST_REACTION_ORDER.flatMap((content) => byContent.get(content) ?? []);
184}