Open History to see the last commit
1import { useAtomCommand } from "~/state/use-atom-command";
2import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
3import { SendIcon } from "lucide-react";
4import { useState, type RefObject } from "react";
5
6import { pullRequestEnvironment } from "~/state/pullRequests";
7
8import { Button } from "../ui/button";
9import { Textarea } from "../ui/textarea";
10import { toastManager } from "../ui/toast";
11import { PullRequestGlyph } from "./pullRequestIcons";
12import { pullRequestReviewKey } from "./pullRequestReviewStore";
13import { useInlineActionConfirm } from "./useInlineActionConfirm";
14
15export function PullRequestCommentForm({
16 environmentId,
17 reference,
18 detail,
19 actionPending,
20 textareaRef,
21 onCommentAction,
22 onCommented,
23 onClose,
24}: {
25 environmentId: EnvironmentId;
26 reference: PullRequestRef;
27 detail: PullRequestDetailView;
28 actionPending: boolean;
29 textareaRef: RefObject<HTMLTextAreaElement | null>;
30 onCommentAction: (
31 body: string,
32 action: "close" | "reopen",
33 ) => Promise<{ readonly commentPosted: boolean }>;
34 onCommented: () => void;
35 onClose: () => void;
36}) {
37 const [body, setBody] = useState("");
38 const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
39 const postComment = useAtomCommand(pullRequestEnvironment.comment, {
40 reportFailure: false,
41 });
42 const inlineConfirm = useInlineActionConfirm(pullRequestReviewKey(reference));
43 const followUpAction =
44 detail.state === "open" &&
45 detail.capabilities.actions.includes("close") &&
46 detail.viewerPermissions.actions.includes("close")
47 ? ("close" as const)
48 : detail.state === "closed" &&
49 detail.capabilities.actions.includes("reopen") &&
50 detail.viewerPermissions.actions.includes("reopen")
51 ? ("reopen" as const)
52 : null;
53
54 const submit = async (action: "comment" | "close" | "reopen") => {
55 const trimmed = body.trim();
56 if (trimmed.length === 0 || submitting !== null || actionPending) return;
57 setSubmitting(action);
58 if (action !== "comment") {
59 const result = await onCommentAction(trimmed, action);
60 if (result.commentPosted) {
61 setBody("");
62 onClose();
63 }
64 setSubmitting(null);
65 return;
66 }
67 const result = await postComment({
68 environmentId,
69 input: {
70 ...reference,
71 body: trimmed,
72 },
73 });
74 if (result._tag === "Failure") {
75 setSubmitting(null);
76 toastManager.add({ type: "error", title: "Could not post the comment" });
77 return;
78 }
79 setBody("");
80 setSubmitting(null);
81 onClose();
82 onCommented();
83 };
84
85 return (
86 <div className="space-y-2">
87 <Textarea
88 ref={textareaRef}
89 // Locked while posting: the body is cleared on success, which would otherwise throw
90 // away a new draft typed while the request was still in flight.
91 disabled={submitting !== null || actionPending}
92 value={body}
93 rows={3}
94 placeholder="Leave a comment"
95 aria-label="Comment on this pull request"
96 onChange={(event) => setBody(event.target.value)}
97 onKeyDown={(event) => {
98 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
99 if (
100 event.key === "Enter" &&
101 (event.metaKey || event.ctrlKey) &&
102 !event.shiftKey &&
103 !event.altKey
104 ) {
105 event.preventDefault();
106 event.stopPropagation();
107 if (!event.repeat) void submit("comment");
108 }
109 }}
110 />
111 <div className="flex flex-wrap justify-end gap-2">
112 {followUpAction === null ? null : (
113 <Button
114 size="xs"
115 variant={followUpAction === "close" ? "destructive-outline" : "outline"}
116 disabled={body.trim().length === 0 || submitting !== null || actionPending}
117 {...(followUpAction === "close"
118 ? inlineConfirm.props("close", () => void submit("close"))
119 : { onClick: () => void submit("reopen") })}
120 >
121 {followUpAction === "close" ? (
122 <PullRequestGlyph.closed className="size-3.5" />
123 ) : (
124 <PullRequestGlyph.reopen className="size-3.5" />
125 )}
126 {followUpAction === "close" ? (
127 // The longest label holds the width, so the confirming click lands on the button.
128 <span className="inline-grid">
129 <span aria-hidden className="invisible col-start-1 row-start-1">
130 Close with comment
131 </span>
132 <span className="col-start-1 row-start-1">
133 {submitting === "close"
134 ? "Closing..."
135 : inlineConfirm.isArmed("close")
136 ? "Confirm close?"
137 : "Close with comment"}
138 </span>
139 </span>
140 ) : submitting === "reopen" ? (
141 "Reopening..."
142 ) : (
143 "Reopen with comment"
144 )}
145 </Button>
146 )}
147 <Button
148 size="xs"
149 variant="outline"
150 disabled={body.trim().length === 0 || submitting !== null || actionPending}
151 onClick={() => void submit("comment")}
152 >
153 <SendIcon className="size-3.5" />
154 {submitting === "comment" ? "Posting..." : "Comment"}
155 </Button>
156 </div>
157 </div>
158 );
159}