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

PullRequestCommentComposer.tsx

171 lines5.9 KB

1import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
2import { MessageSquareIcon, SendIcon, XIcon } from "lucide-react";
3import { useRef, useState } from "react";
4​
5import { useAtomCommand } from "~/state/use-atom-command";
6import { pullRequestEnvironment } from "~/state/pullRequests";
7​
8import { Button } from "../ui/button";
9import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
10import { Textarea } from "../ui/textarea";
11import { toastManager } from "../ui/toast";
12import { PullRequestGlyph } from "./pullRequestIcons";
13​
14export function PullRequestCommentComposer({
15 environmentId,
16 reference,
17 detail,
18 actionPending,
19 onCommentAction,
20 onCommented,
21}: {
22 environmentId: EnvironmentId;
23 reference: PullRequestRef;
24 detail: PullRequestDetailView;
25 actionPending: boolean;
26 onCommentAction: (
27 body: string,
28 action: "close" | "reopen",
29 ) => Promise<{ readonly commentPosted: boolean }>;
30 onCommented: () => void;
31}) {
32 const [open, setOpen] = useState(false);
33 const [body, setBody] = useState("");
34 const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
35 const textareaRef = useRef<HTMLTextAreaElement>(null);
36 const postComment = useAtomCommand(pullRequestEnvironment.comment, { reportFailure: false });
37 const followUpAction =
38 detail.state === "open" &&
39 detail.capabilities.actions.includes("close") &&
40 detail.viewerPermissions.actions.includes("close")
41 ? ("close" as const)
42 : detail.state === "closed" &&
43 detail.capabilities.actions.includes("reopen") &&
44 detail.viewerPermissions.actions.includes("reopen")
45 ? ("reopen" as const)
46 : null;
47​
48 const submit = async (action: "comment" | "close" | "reopen") => {
49 const trimmed = body.trim();
50 if (trimmed.length === 0 || submitting !== null || actionPending) return;
51 setSubmitting(action);
52 if (action !== "comment") {
53 const result = await onCommentAction(trimmed, action);
54 if (result.commentPosted) {
55 setBody("");
56 setOpen(false);
57 }
58 setSubmitting(null);
59 return;
60 }
61 const result = await postComment({
62 environmentId,
63 input: {
64 ...reference,
65 body: trimmed,
66 },
67 });
68 if (result._tag === "Failure") {
69 setSubmitting(null);
70 toastManager.add({ type: "error", title: "Could not post the comment" });
71 return;
72 }
73 setBody("");
74 setSubmitting(null);
75 setOpen(false);
76 onCommented();
77 };
78​
79 return (
80 <Popover open={open} onOpenChange={setOpen}>
81 <PopoverTrigger
82 render={
83 <Button
84 size="icon"
85 variant="glass"
86 className="rounded-full shadow-lg [--glass-opacity:50%]"
87 />
88 }
89 aria-label="Comment on pull request"
90 >
91 <MessageSquareIcon className="size-4" />
92 </PopoverTrigger>
93 <PopoverPopup
94 side="top"
95 align="end"
96 sideOffset={8}
97 className="w-96 max-w-[calc(100vw-2rem)]"
98 initialFocus={textareaRef}
99 >
100 <div className="mb-3 flex items-center justify-between gap-2">
101 <PopoverTitle className="text-sm">Comment on pull request</PopoverTitle>
102 <PopoverClose
103 render={<Button size="icon-xs" variant="ghost" />}
104 aria-label="Close comment composer"
105 >
106 <XIcon className="size-3.5" />
107 </PopoverClose>
108 </div>
109 <div className="space-y-2">
110 <Textarea
111 ref={textareaRef}
112 className="[&_textarea]:max-h-64"
113 // Locked while posting: the body is cleared on success, which would otherwise throw
114 // away a new draft typed while the request was still in flight.
115 disabled={submitting !== null || actionPending}
116 value={body}
117 rows={3}
118 placeholder="Leave a comment"
119 aria-label="Comment on this pull request"
120 onChange={(event) => setBody(event.target.value)}
121 onKeyDown={(event) => {
122 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
123 if (
124 event.key === "Enter" &&
125 (event.metaKey || event.ctrlKey) &&
126 !event.shiftKey &&
127 !event.altKey
128 ) {
129 event.preventDefault();
130 event.stopPropagation();
131 if (!event.repeat) void submit("comment");
132 }
133 }}
134 />
135 <div className="flex flex-wrap justify-end gap-2">
136 {followUpAction === null ? null : (
137 <Button
138 size="xs"
139 variant={followUpAction === "close" ? "destructive-outline" : "outline"}
140 disabled={body.trim().length === 0 || submitting !== null || actionPending}
141 onClick={() => void submit(followUpAction)}
142 >
143 {followUpAction === "close" ? (
144 <PullRequestGlyph.closed className="size-3.5" />
145 ) : (
146 <PullRequestGlyph.reopen className="size-3.5" />
147 )}
148 {submitting === followUpAction
149 ? followUpAction === "close"
150 ? "Closing..."
151 : "Reopening..."
152 : followUpAction === "close"
153 ? "Close with comment"
154 : "Reopen with comment"}
155 </Button>
156 )}
157 <Button
158 size="xs"
159 variant="outline"
160 disabled={body.trim().length === 0 || submitting !== null || actionPending}
161 onClick={() => void submit("comment")}
162 >
163 <SendIcon className="size-3.5" />
164 {submitting === "comment" ? "Posting..." : "Comment"}
165 </Button>
166 </div>
167 </div>
168 </PopoverPopup>
169 </Popover>
170 );