Open History to see the last commit
1import { useAtomCommand } from "~/state/use-atom-command";
2import type {
3 EnvironmentId,
4 PullRequestReaction,
5 PullRequestReactionContent,
6 PullRequestRef,
7} from "@t3tools/contracts";
8import { SmilePlusIcon } from "lucide-react";
9import { useState } from "react";
10
11import { cn } from "~/lib/utils";
12import { pullRequestEnvironment } from "~/state/pullRequests";
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-inset focus-visible:ring-ring";
27
28const EMPTY_PENDING: ReadonlyMap<PullRequestReactionContent, boolean> = new Map();
29
30function reactionsSignature(reactions: ReadonlyArray<PullRequestReaction>): string {
31 return reactions
32 .map((reaction) => `${reaction.content}:${reaction.count}:${reaction.viewerHasReacted ? 1 : 0}`)
33 .join(" ");
34}
35
36export function PullRequestReactionBar({
37 reactions,
38 canReact,
39 subjectId,
40 environmentId,
41 reference,
42 onRefresh,
43 className,
44}: {
45 readonly reactions: ReadonlyArray<PullRequestReaction>;
46 readonly canReact: boolean;
47 /** Absent reacts to the change request itself, which is where its description's reactions live. */
48 readonly subjectId?: string | undefined;
49 readonly environmentId: EnvironmentId;
50 readonly reference: PullRequestRef;
51 readonly onRefresh: () => void;
52 readonly className?: string | undefined;
53}) {
54 const [pickerOpen, setPickerOpen] = useState(false);
55 const [pending, setPending] = useState<{
56 readonly signature: string;
57 readonly values: ReadonlyMap<PullRequestReactionContent, boolean>;
58 }>({ signature: "", values: EMPTY_PENDING });
59 const setReaction = useAtomCommand(pullRequestEnvironment.setReaction, {
60 reportFailure: false,
61 });
62
63 const signature = reactionsSignature(reactions);
64 const values = pending.signature === signature ? pending.values : EMPTY_PENDING;
65 const shown = applyPendingPullRequestReactions(reactions, values);
66
67 const toggle = async (content: PullRequestReactionContent, reacted: boolean) => {
68 setPending({ signature, values: new Map([...values, [content, reacted]]) });
69 const result = await setReaction({
70 environmentId,
71 input: {
72 ...reference,
73 ...(subjectId === undefined ? {} : { subjectId }),
74 content,
75 reacted,
76 },
77 });
78 if (result._tag === "Failure") {
79 setPending((current) => {
80 const next = new Map(current.values);
81 next.delete(content);
82 return { signature: current.signature, values: next };
83 });
84 toastManager.add({ type: "error", title: "The reaction could not be saved" });
85 return;
86 }
87 onRefresh();
88 };
89
90 if (shown.length === 0 && !canReact) return null;
91
92 return (
93 <div className={cn("flex min-w-0 max-w-full flex-wrap items-center gap-1", className)}>
94 {shown.map((reaction) => (
95 <Tooltip key={reaction.content}>
96 <TooltipTrigger
97 render={
98 <button
99 type="button"
100 aria-pressed={reaction.viewerHasReacted}
101 aria-label={`${pullRequestReactionName(reaction.content)}, ${reaction.count}`}
102 disabled={!canReact}
103 className={cn(
104 PILL_CLASS,
105 reaction.viewerHasReacted
106 ? "border-primary/60 bg-primary/10 text-foreground"
107 : "border-border/70 bg-muted/40 text-muted-foreground",
108 canReact ? "hover:border-primary/60" : "cursor-default",
109 )}
110 onClick={() => void toggle(reaction.content, !reaction.viewerHasReacted)}
111 />
112 }
113 >
114 <span aria-hidden>{pullRequestReactionEmoji(reaction.content)}</span>
115 <span className="tabular-nums">{reaction.count}</span>
116 </TooltipTrigger>
117 <TooltipPopup side="top">{pullRequestReactionTooltip(reaction)}</TooltipPopup>
118 </Tooltip>
119 ))}
120
121 {canReact ? (
122 <Popover open={pickerOpen} onOpenChange={setPickerOpen}>
123 <PopoverTrigger
124 render={
125 <button
126 type="button"
127 aria-label="Add a reaction"
128 className={cn(
129 PILL_CLASS,
130 "border-border/70 px-1.5 text-muted-foreground hover:border-primary/60 hover:text-foreground",
131 )}
132 />
133 }
134 >
135 <SmilePlusIcon aria-hidden className="size-3.5" />
136 </PopoverTrigger>
137 <PopoverPopup align="start" side="top" padding="compact">
138 <div className="flex items-center gap-0.5">
139 {PULL_REQUEST_REACTION_ORDER.map((content) => {
140 const reacted =
141 shown.find((reaction) => reaction.content === content)?.viewerHasReacted ?? false;
142 return (
143 <button
144 key={content}
145 type="button"
146 aria-pressed={reacted}
147 aria-label={pullRequestReactionName(content)}
148 className={cn(
149 "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",
150 reacted && "bg-primary/10",
151 )}
152 onClick={() => {
153 setPickerOpen(false);
154 void toggle(content, !reacted);
155 }}
156 >
157 <span aria-hidden>{pullRequestReactionEmoji(content)}</span>
158 </button>
159 );
160 })}
161 </div>
162 </PopoverPopup>
163 </Popover>
164 ) : null}
165 </div>
166 );
167}