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

PullRequestReactions.tsx

202 lines7.2 KB

1import { useAtomCommand } from "~/state/use-atom-command";
2import type {
3 EnvironmentId,
4 PullRequestReaction,
5 PullRequestReactionContent,
6 PullRequestRef,
7} from "@t3tools/contracts";
8import { msg, useGT, useLocale, useMessages } from "gt-react";
9import { SmilePlusIcon } from "lucide-react";
10import { useState } from "react";
11​
12import { m } from "~/i18n/messages";
13import { cn } from "~/lib/utils";
14import { pullRequestEnvironment } from "~/state/pullRequests";
15​
16import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
17import { toastManager } from "../ui/toast";
18import { SharedTooltipTrigger } from "../ui/shared-tooltip";
19import {
20 applyPendingPullRequestReactions,
21 type PendingPullRequestReaction,
22 PULL_REQUEST_REACTION_ORDER,
23 pullRequestReactionEmoji,
24 pullRequestReactionName,
25 pullRequestReactionTooltip,
26 pullRequestReactionsSignature,
27 retainPendingPullRequestReactions,
28} from "@t3tools/client-runtime/pull-request-reactions";
29​
30const PILL_CLASS =
31 "inline-flex h-6 shrink-0 items-center gap-1 rounded-full border px-2 text-xs outline-none transition-colors focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring";
32​
33const EMPTY_PENDING: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction> =
34 new Map();
35​
36/** Reaction pills with a picker. `onReact` resolves to whether the host saved the change. */
37export function ReactionBar({
38 reactions,
39 canReact,
40 onReact,
41 className,
42}: {
43 readonly reactions: ReadonlyArray<PullRequestReaction>;
44 readonly canReact: boolean;
45 readonly onReact: (content: PullRequestReactionContent, reacted: boolean) => Promise<boolean>;
46 readonly className?: string | undefined;
47}) {
48 const gt = useGT();
49 const messages = useMessages();
50 const locale = useLocale();
51 const [pickerOpen, setPickerOpen] = useState(false);
52 const [pending, setPending] = useState<{
53 readonly signature: string;
54 readonly values: ReadonlyMap<PullRequestReactionContent, PendingPullRequestReaction>;
55 }>({ signature: "", values: EMPTY_PENDING });
56​
57 const signature = pullRequestReactionsSignature(reactions);
58 let values = pending.values;
59 if (pending.signature !== signature && values.size > 0) {
60 values = retainPendingPullRequestReactions(reactions, values);
61 setPending({ signature, values });
62 }
63 const shown = applyPendingPullRequestReactions(reactions, values);
64​
65 const toggle = async (content: PullRequestReactionContent, reacted: boolean) => {
66 const entry: PendingPullRequestReaction = { reacted };
67 setPending({ signature, values: new Map([...values, [content, entry]]) });
68 const saved = await onReact(content, reacted);
69 setPending((current) => {
70 if (current.values.get(content) !== entry) return current;
71 const next = new Map(current.values);
72 if (saved) next.set(content, { ...entry, saved });
73 else next.delete(content);
74 return { signature: current.signature, values: next };
75 });
76 if (saved) return;
77 toastManager.add({ type: "error", title: m(msg("The reaction could not be saved")) });
78 };
79​
80 if (shown.length === 0 && !canReact) return null;
81​
82 return (
83 <div className={cn("flex min-w-0 max-w-full flex-wrap items-center gap-1", className)}>
84 {shown.map((reaction) => (
85 <SharedTooltipTrigger
86 key={reaction.content}
87 render={
88 <button
89 type="button"
90 aria-pressed={reaction.viewerHasReacted}
91 aria-label={gt({
92 context: "reaction button: reaction name and how many people used it",
93 })`${{ reaction: pullRequestReactionName(reaction.content, messages) }}, ${{ count: reaction.count }}`}
94 disabled={!canReact}
95 className={cn(
96 PILL_CLASS,
97 reaction.viewerHasReacted
98 ? "border-primary/60 bg-primary/10 text-foreground"
99 : "border-border/70 bg-muted/40 text-muted-foreground",
100 canReact ? "hover:border-primary/60" : "cursor-default",
101 )}
102 onClick={() => void toggle(reaction.content, !reaction.viewerHasReacted)}
103 />
104 }
105 label={pullRequestReactionTooltip(reaction, messages, locale)}
106 >
107 <span aria-hidden>{pullRequestReactionEmoji(reaction.content)}</span>
108 <span className="tabular-nums">{reaction.count}</span>
109 </SharedTooltipTrigger>
110 ))}
111​
112 {canReact ? (
113 <Popover open={pickerOpen} onOpenChange={setPickerOpen}>
114 <PopoverTrigger
115 render={
116 <button
117 type="button"
118 aria-label={gt`Add a reaction`}
119 className={cn(
120 PILL_CLASS,
121 "border-border/70 px-1.5 text-muted-foreground hover:border-primary/60 hover:text-foreground",
122 )}
123 />
124 }
125 >
126 <SmilePlusIcon aria-hidden className="size-3.5" />
127 </PopoverTrigger>
128 <PopoverPopup align="start" side="top" padding="compact">
129 <div className="flex items-center gap-0.5">
130 {PULL_REQUEST_REACTION_ORDER.map((content) => {
131 const reacted =
132 shown.find((reaction) => reaction.content === content)?.viewerHasReacted ?? false;
133 return (
134 <button
135 key={content}
136 type="button"
137 aria-pressed={reacted}
138 aria-label={pullRequestReactionName(content, messages)}
139 className={cn(
140 "flex size-7 items-center justify-center rounded-md text-base outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
141 reacted && "bg-primary/10",
142 )}
143 onClick={() => {
144 setPickerOpen(false);
145 void toggle(content, !reacted);
146 }}
147 >
148 <span aria-hidden>{pullRequestReactionEmoji(content)}</span>
149 </button>
150 );
151 })}
152 </div>
153 </PopoverPopup>
154 </Popover>
155 ) : null}
156 </div>
157 );
159​
160export function PullRequestReactionBar({
161 reactions,
162 canReact,
163 subjectId,
164 environmentId,
165 reference,
166 onRefresh,
167 className,
168}: {
169 readonly reactions: ReadonlyArray<PullRequestReaction>;
170 readonly canReact: boolean;
171 /** Omit to react to the pull request description. */
172 readonly subjectId?: string | undefined;
173 readonly environmentId: EnvironmentId;
174 readonly reference: PullRequestRef;
175 readonly onRefresh: () => void;
176 readonly className?: string | undefined;
177}) {
178 const setReaction = useAtomCommand(pullRequestEnvironment.setReaction, {
179 reportFailure: false,
180 });
181 return (
182 <ReactionBar
183 reactions={reactions}
184 canReact={canReact}
185 className={className}
186 onReact={async (content, reacted) => {
187 const result = await setReaction({
188 environmentId,
189 input: {
190 ...reference,
191 ...(subjectId === undefined ? {} : { subjectId }),
192 content,
193 reacted,
194 },
195 });
196 if (result._tag === "Failure") return false;
197 onRefresh();
198 return true;
199 }}
200 />
201 );