The best way to control Claude Code, Codex and more from any computer or phone

main
ts 155 lines 5.39 KB
Raw
1 import { mFallback, msg } from "gt-i18n";
2 import type { MFunctionType } from "gt-i18n/types";
3 import { formatConjunctionList } from "./listFormat.ts";
4 import type {
5 PullRequestComment,
6 PullRequestDetail,
7 PullRequestReaction,
8 PullRequestReactionContent,
9 } from "@t3tools/contracts";
10
11 export 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
21 export 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
32 const 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
43 const REACTION_NAME: Record<PullRequestReactionContent, string> = {
44 "thumbs-up": msg("thumbs up", { $context: "reaction emoji name" }),
45 "thumbs-down": msg("thumbs down", { $context: "reaction emoji name" }),
46 laugh: msg("laugh", { $context: "reaction emoji name" }),
47 hooray: msg("hooray", { $context: "reaction emoji name" }),
48 confused: msg("confused", { $context: "reaction emoji name" }),
49 heart: msg("heart", { $context: "reaction emoji name" }),
50 rocket: msg("rocket", { $context: "reaction emoji name" }),
51 eyes: msg("eyes", { $context: "reaction emoji name" }),
52 };
53
54 export function pullRequestReactionEmoji(content: PullRequestReactionContent): string {
55 return REACTION_EMOJI[content];
56 }
57
58 export function pullRequestReactionName(
59 content: PullRequestReactionContent,
60 resolve: MFunctionType = mFallback,
61 ): string {
62 return resolve(REACTION_NAME[content]);
63 }
64
65 const NAMED_ACTOR_LIMIT = 3;
66
67 function countRemainder(count: number, named: boolean, resolve: MFunctionType): string {
68 return named
69 ? resolve(msg("{count, plural, one {{count} other} other {{count} others}}", { count }))
70 : resolve(msg("{count, plural, one {{count} person} other {{count} people}}", { count }));
71 }
72
73 function reactedWith(
74 content: PullRequestReactionContent,
75 names: string,
76 resolve: MFunctionType,
77 ): string {
78 switch (content) {
79 case "thumbs-up":
80 return resolve(msg("{names} reacted with thumbs up emoji", { names }));
81 case "thumbs-down":
82 return resolve(msg("{names} reacted with thumbs down emoji", { names }));
83 case "laugh":
84 return resolve(msg("{names} reacted with laugh emoji", { names }));
85 case "hooray":
86 return resolve(msg("{names} reacted with hooray emoji", { names }));
87 case "confused":
88 return resolve(msg("{names} reacted with confused emoji", { names }));
89 case "heart":
90 return resolve(msg("{names} reacted with heart emoji", { names }));
91 case "rocket":
92 return resolve(msg("{names} reacted with rocket emoji", { names }));
93 case "eyes":
94 return resolve(msg("{names} reacted with eyes emoji", { names }));
95 }
96 }
97
98 /** Hosts omit the viewer from actors; add You only if count has room, since viewer login is unavailable. */
99 export function pullRequestReactionTooltip(
100 reaction: PullRequestReaction,
101 resolve: MFunctionType = mFallback,
102 locale = "en",
103 ): string {
104 const viewerHasRoom = reaction.actors.length < reaction.count;
105 const names =
106 reaction.viewerHasReacted && viewerHasRoom
107 ? [
108 resolve(msg("You", { $context: "the viewer, in a list of people who reacted" })),
109 ...reaction.actors,
110 ]
111 : [...reaction.actors];
112 const shown = names.slice(0, Math.min(NAMED_ACTOR_LIMIT, reaction.count));
113 const others = Math.max(0, reaction.count - shown.length);
114 const parts = [
115 ...shown,
116 ...(others > 0 ? [countRemainder(others, shown.length > 0, resolve)] : []),
117 ];
118 return reactedWith(reaction.content, formatConjunctionList(parts, locale, resolve), resolve);
119 }
120
121 /** Changes whenever the host reports different reactions, so pending overlays can reset. */
122 export function pullRequestReactionsSignature(
123 reactions: ReadonlyArray<PullRequestReaction>,
124 ): string {
125 return reactions
126 .map((reaction) => `${reaction.content}:${reaction.count}:${reaction.viewerHasReacted ? 1 : 0}`)
127 .join(" ");
128 }
129
130 /** One toggle awaiting the host. Its identity tells it apart from a later toggle of the same reaction. */
131 export interface PendingPullRequestReaction {
132 readonly reacted: boolean;
133 }
134
135 export function applyPendingPullRequestReactions(
136 reactions: ReadonlyArray<PullRequestReaction>,
137 pending: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction>,
138 ): ReadonlyArray<PullRequestReaction> {
139 if (pending.size === 0) return reactions;
140 const byContent = new Map(reactions.map((reaction) => [reaction.content, reaction] as const));
141 for (const [content, { reacted }] of pending) {
142 const current = byContent.get(content);
143 if (current === undefined) {
144 if (reacted) {
145 byContent.set(content, { content, count: 1, actors: [], viewerHasReacted: true });
146 }
147 continue;
148 }
149 if (current.viewerHasReacted === reacted) continue;
150 const count = current.count + (reacted ? 1 : -1);
151 if (count <= 0) byContent.delete(content);
152 else byContent.set(content, { ...current, count, viewerHasReacted: reacted });
153 }
154 return PULL_REQUEST_REACTION_ORDER.flatMap((content) => byContent.get(content) ?? []);
155 }