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