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

PullRequestReactions.tsx

178 lines6.8 KB

1import type {
2 EnvironmentId,
3 PullRequestReaction,
4 PullRequestReactionContent,
5 PullRequestRef,
6} from "@t3tools/contracts";
7import { SmilePlusIcon } from "lucide-react";
8import { useState } from "react";
9​
10import { cn } from "~/lib/utils";
11import { pullRequestEnvironment } from "~/state/pullRequests";
12import { useAtomCommand } from "~/state/use-atom-command";
13​
14import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover";
15import { toastManager } from "../ui/toast";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
17import {
18 applyPendingPullRequestReactions,
19 PULL_REQUEST_REACTION_ORDER,
20 pullRequestReactionEmoji,
21 pullRequestReactionName,
22 pullRequestReactionTooltip,
23} from "./pullRequestReactions.logic";
24​
25const PILL_CLASS =
26 "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-ring";
27​
28const EMPTY_PENDING: ReadonlyMap<PullRequestReactionContent, boolean> = new Map();
29​
30/** What the host last said, so a press in flight is forgotten the moment the real counts land. */
31function reactionsSignature(reactions: ReadonlyArray<PullRequestReaction>): string {
32 return reactions
33 .map((reaction) => `${reaction.content}:${reaction.count}:${reaction.viewerHasReacted ? 1 : 0}`)
34 .join(" ");
36​
37/**
38 * The reaction pills under a remark, and the picker that adds one. The same bar serves the
39 * description, a conversation comment and a review thread's comments: what differs between them
40 * is only which subject the host is told about.
41 *
42 * The add button is revealed by hovering the remark it belongs to, the way GitHub's is, so the
43 * parent must carry `group`. It stays put once there is something to press it beside, while the
44 * picker is open, and whenever it is focused — a control only a mouse can find is no control.
45 */
46export function PullRequestReactionBar({
47 reactions,
48 canReact,
49 subjectId,
50 environmentId,
51 reference,
52 onRefresh,
53 className,
54}: {
55 readonly reactions: ReadonlyArray<PullRequestReaction>;
56 readonly canReact: boolean;
57 /** Absent reacts to the change request itself, which is where its description's reactions live. */
58 readonly subjectId?: string | undefined;
59 readonly environmentId: EnvironmentId;
60 readonly reference: PullRequestRef;
61 readonly onRefresh: () => void;
62 readonly className?: string | undefined;
63}) {
64 const [pickerOpen, setPickerOpen] = useState(false);
65 const [pending, setPending] = useState<{
66 readonly signature: string;
67 readonly values: ReadonlyMap<PullRequestReactionContent, boolean>;
68 }>({ signature: "", values: EMPTY_PENDING });
69 const setReaction = useAtomCommand(pullRequestEnvironment.setReaction, { reportFailure: false });
70​
71 const signature = reactionsSignature(reactions);
72 const values = pending.signature === signature ? pending.values : EMPTY_PENDING;
73 const shown = applyPendingPullRequestReactions(reactions, values);
74​
75 const toggle = async (content: PullRequestReactionContent, reacted: boolean) => {
76 setPending({ signature, values: new Map([...values, [content, reacted]]) });
77 const result = await setReaction({
78 environmentId,
79 input: {
80 ...reference,
81 ...(subjectId === undefined ? {} : { subjectId }),
82 content,
83 reacted,
84 },
85 });
86 if (result._tag === "Failure") {
87 setPending((current) => {
88 const next = new Map(current.values);
89 next.delete(content);
90 return { signature: current.signature, values: next };
91 });
92 toastManager.add({ type: "error", title: "The reaction could not be saved" });
93 return;
94 }
95 onRefresh();
96 };
97​
98 if (shown.length === 0 && !canReact) return null;
99​
100 return (
101 <div className={cn("flex flex-wrap items-center gap-1", className)}>
102 {shown.map((reaction) => (
103 <Tooltip key={reaction.content}>
104 <TooltipTrigger
105 render={
106 <button
107 type="button"
108 aria-pressed={reaction.viewerHasReacted}
109 aria-label={`${pullRequestReactionName(reaction.content)}, ${reaction.count}`}
110 disabled={!canReact}
111 className={cn(
112 PILL_CLASS,
113 reaction.viewerHasReacted
114 ? "border-primary/60 bg-primary/10 text-foreground"
115 : "border-border/70 bg-muted/40 text-muted-foreground",
116 canReact ? "hover:border-primary/60" : "cursor-default",
117 )}
118 onClick={() => void toggle(reaction.content, !reaction.viewerHasReacted)}
119 />
120 }
121 >
122 <span aria-hidden>{pullRequestReactionEmoji(reaction.content)}</span>
123 <span className="tabular-nums">{reaction.count}</span>
124 </TooltipTrigger>
125 <TooltipPopup side="top">{pullRequestReactionTooltip(reaction)}</TooltipPopup>
126 </Tooltip>
127 ))}
128​
129 {canReact ? (
130 <Popover open={pickerOpen} onOpenChange={setPickerOpen}>
131 <PopoverTrigger
132 render={
133 <button
134 type="button"
135 aria-label="Add a reaction"
136 className={cn(
137 PILL_CLASS,
138 "border-border/70 px-1.5 text-muted-foreground hover:border-primary/60 hover:text-foreground",
139 shown.length === 0 &&
140 !pickerOpen &&
141 "opacity-0 group-focus-within:opacity-100 group-hover:opacity-100 focus-visible:opacity-100",
142 )}
143 />
144 }
145 >
146 <SmilePlusIcon aria-hidden className="size-3.5" />
147 </PopoverTrigger>
148 <PopoverPopup align="start" className="w-auto" side="top" viewportClassName="py-2">
149 <div className="flex items-center gap-0.5">
150 {PULL_REQUEST_REACTION_ORDER.map((content) => {
151 const reacted =
152 shown.find((reaction) => reaction.content === content)?.viewerHasReacted ?? false;
153 return (
154 <button
155 key={content}
156 type="button"
157 aria-pressed={reacted}
158 aria-label={pullRequestReactionName(content)}
159 className={cn(
160 "flex size-7 items-center justify-center rounded-md text-base outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring",
161 reacted && "bg-primary/10",
162 )}
163 onClick={() => {
164 setPickerOpen(false);
165 void toggle(content, !reacted);
166 }}
167 >
168 <span aria-hidden>{pullRequestReactionEmoji(content)}</span>
169 </button>
170 );
171 })}
172 </div>
173 </PopoverPopup>
174 </Popover>
175 ) : null}
176 </div>
177 );