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

PullRequestCommentForm.tsx

159 lines5.4 KB

1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import { useAtomCommand } from "~/state/use-atom-command";
3import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
4import { SendIcon } from "lucide-react";
5import { useState, type RefObject } from "react";
6​
7import { pullRequestEnvironment } from "~/state/pullRequests";
8​
9import { Button } from "../ui/button";
10import { Textarea } from "../ui/textarea";
11import { toastManager } from "../ui/toast";
12import { PullRequestGlyph } from "./pullRequestIcons";
13import { pullRequestReviewKey } from "./pullRequestReviewStore";
14import { useInlineActionConfirm } from "./useInlineActionConfirm";
15​
16export function PullRequestCommentForm({
17 environmentId,
18 reference,
19 detail,
20 actionPending,
21 textareaRef,
22 onCommentAction,
23 onCommented,
24 onClose,
25}: {
26 environmentId: EnvironmentId;
27 reference: PullRequestRef;
28 detail: PullRequestDetailView;
29 actionPending: boolean;
30 textareaRef: RefObject<HTMLTextAreaElement | null>;
31 onCommentAction: (
32 body: string,
33 action: "close" | "reopen",
34 ) => Promise<{ readonly commentPosted: boolean }>;
35 onCommented: () => void;
36 onClose: () => void;
37}) {
38 const [body, setBody] = useState("");
39 const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
40 const postComment = useAtomCommand(pullRequestEnvironment.comment, {
41 reportFailure: false,
42 });
43 const inlineConfirm = useInlineActionConfirm(pullRequestReviewKey(reference));
44 const followUpAction =
45 detail.state === "open" &&
46 detail.capabilities.actions.includes("close") &&
47 detail.viewerPermissions.actions.includes("close")
48 ? ("close" as const)
49 : detail.state === "closed" &&
50 detail.capabilities.actions.includes("reopen") &&
51 detail.viewerPermissions.actions.includes("reopen")
52 ? ("reopen" as const)
53 : null;
54​
55 const submit = async (action: "comment" | "close" | "reopen") => {
56 const trimmed = body.trim();
57 if (trimmed.length === 0 || submitting !== null || actionPending) return;
58 setSubmitting(action);
59 if (action !== "comment") {
60 const result = await onCommentAction(trimmed, action);
61 if (result.commentPosted) {
62 setBody("");
63 onClose();
64 }
65 setSubmitting(null);
66 return;
67 }
68 const result = await postComment({
69 environmentId,
70 input: {
71 ...reference,
72 body: trimmed,
73 },
74 });
75 if (result._tag === "Failure") {
76 setSubmitting(null);
77 toastManager.add({ type: "error", title: "Could not post the comment" });
78 return;
79 }
80 setBody("");
81 setSubmitting(null);
82 onClose();
83 onCommented();
84 };
85​
86 return (
87 <div className="space-y-2">
88 <Textarea
89 ref={textareaRef}
90 // Lock while posting because success clears the draft.
91 disabled={submitting !== null || actionPending}
92 value={body}
93 rows={3}
94 placeholder="Leave a comment"
95 aria-label={`Comment on this ${sourceControlClients.get(detail.provider).changeRequest.singular}`}
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 );