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

PullRequestReactions.tsx

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