Open History to see the last commit
1import type {
2 PullRequestComment,
3 PullRequestDetail,
4 PullRequestReaction,
5 PullRequestReactionContent,
6} from "@t3tools/contracts";
7
8export function canReactPullRequestComment(
9 detail: Pick<PullRequestDetail, "provider" | "capabilities">,
10 comment: Pick<PullRequestComment, "kind">,
11): boolean {
12 return (
13 detail.capabilities.reactions === true &&
14 !(detail.provider === "gitcafe" && comment.kind === "review")
15 );
16}
17
18export const PULL_REQUEST_REACTION_ORDER: ReadonlyArray<PullRequestReactionContent> = [
19 "thumbs-up",
20 "thumbs-down",
21 "laugh",
22 "hooray",
23 "confused",
24 "heart",
25 "rocket",
26 "eyes",
27];
28
29const REACTION_EMOJI: Record<PullRequestReactionContent, string> = {
30 "thumbs-up": "👍",
31 "thumbs-down": "👎",
32 laugh: "😄",
33 hooray: "🎉",
34 confused: "😕",
35 heart: "❤️",
36 rocket: "🚀",
37 eyes: "👀",
38};
39
40const REACTION_NAME: Record<PullRequestReactionContent, string> = {
41 "thumbs-up": "thumbs up",
42 "thumbs-down": "thumbs down",
43 laugh: "laugh",
44 hooray: "hooray",
45 confused: "confused",
46 heart: "heart",
47 rocket: "rocket",
48 eyes: "eyes",
49};
50
51export function pullRequestReactionEmoji(content: PullRequestReactionContent): string {
52 return REACTION_EMOJI[content];
53}
54
55export function pullRequestReactionName(content: PullRequestReactionContent): string {
56 return REACTION_NAME[content];
57}
58
59const NAMED_ACTOR_LIMIT = 3;
60
61function joinNames(parts: ReadonlyArray<string>): string {
62 if (parts.length <= 1) return parts[0] ?? "";
63 if (parts.length === 2) return `${parts[0]} and ${parts[1]}`;
64 return `${parts.slice(0, -1).join(", ")}, and ${parts.at(-1)}`;
65}
66
67function countRemainder(count: number, named: boolean): string {
68 if (named) return `${count} ${count === 1 ? "other" : "others"}`;
69 return `${count} ${count === 1 ? "person" : "people"}`;
70}
71
72/** Hosts omit the viewer from actors; add You only if count has room, since viewer login is unavailable. */
73export function pullRequestReactionTooltip(reaction: PullRequestReaction): string {
74 const viewerHasRoom = reaction.actors.length < reaction.count;
75 const names =
76 reaction.viewerHasReacted && viewerHasRoom ? ["You", ...reaction.actors] : [...reaction.actors];
77 const shown = names.slice(0, Math.min(NAMED_ACTOR_LIMIT, reaction.count));
78 const others = Math.max(0, reaction.count - shown.length);
79 const parts = [...shown, ...(others > 0 ? [countRemainder(others, shown.length > 0)] : [])];
80 return `${joinNames(parts)} reacted with ${pullRequestReactionName(reaction.content)} emoji`;
81}
82
83/** Changes whenever the host reports different reactions, so pending overlays can reset. */
84export function pullRequestReactionsSignature(
85 reactions: ReadonlyArray<PullRequestReaction>,
86): string {
87 return reactions
88 .map((reaction) => `${reaction.content}:${reaction.count}:${reaction.viewerHasReacted ? 1 : 0}`)
89 .join(" ");
90}
91
92export function applyPendingPullRequestReactions(
93 reactions: ReadonlyArray<PullRequestReaction>,
94 pending: ReadonlyMap<PullRequestReactionContent, boolean>,
95): ReadonlyArray<PullRequestReaction> {
96 if (pending.size === 0) return reactions;
97 const byContent = new Map(reactions.map((reaction) => [reaction.content, reaction] as const));
98 for (const [content, reacted] of pending) {
99 const current = byContent.get(content);
100 if (current === undefined) {
101 if (reacted) {
102 byContent.set(content, { content, count: 1, actors: [], viewerHasReacted: true });
103 }
104 continue;
105 }
106 if (current.viewerHasReacted === reacted) continue;
107 const count = current.count + (reacted ? 1 : -1);
108 if (count <= 0) byContent.delete(content);
109 else byContent.set(content, { ...current, count, viewerHasReacted: reacted });
110 }
111 return PULL_REQUEST_REACTION_ORDER.flatMap((content) => byContent.get(content) ?? []);
112}