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

PullRequestReactions.tsx

162 lines5.8 KB

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