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

PullRequestCommentForm.tsx

142 lines4.5 KB

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";
12​
13export function PullRequestCommentForm({
14 environmentId,
15 reference,
16 detail,
17 actionPending,
18 textareaRef,
19 onCommentAction,
20 onCommented,
21 onClose,
22}: {
23 environmentId: EnvironmentId;
24 reference: PullRequestRef;
25 detail: PullRequestDetailView;
26 actionPending: boolean;
27 textareaRef: RefObject<HTMLTextAreaElement | null>;
28 onCommentAction: (
29 body: string,
30 action: "close" | "reopen",
31 ) => Promise<{ readonly commentPosted: boolean }>;
32 onCommented: () => void;
33 onClose: () => void;
34}) {
35 const [body, setBody] = useState("");
36 const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(null);
37 const postComment = useAtomCommand(pullRequestEnvironment.comment, {
38 reportFailure: false,
39 });
40 const followUpAction =
41 detail.state === "open" &&
42 detail.capabilities.actions.includes("close") &&
43 detail.viewerPermissions.actions.includes("close")
44 ? ("close" as const)
45 : detail.state === "closed" &&
46 detail.capabilities.actions.includes("reopen") &&
47 detail.viewerPermissions.actions.includes("reopen")
48 ? ("reopen" as const)
49 : null;
50​
51 const submit = async (action: "comment" | "close" | "reopen") => {
52 const trimmed = body.trim();
53 if (trimmed.length === 0 || submitting !== null || actionPending) return;
54 setSubmitting(action);
55 if (action !== "comment") {
56 const result = await onCommentAction(trimmed, action);
57 if (result.commentPosted) {
58 setBody("");
59 onClose();
60 }
61 setSubmitting(null);
62 return;
63 }
64 const result = await postComment({
65 environmentId,
66 input: {
67 ...reference,
68 body: trimmed,
69 },
70 });
71 if (result._tag === "Failure") {
72 setSubmitting(null);
73 toastManager.add({ type: "error", title: "Could not post the comment" });
74 return;
75 }
76 setBody("");
77 setSubmitting(null);
78 onClose();
79 onCommented();
80 };
81​
82 return (
83 <div className="space-y-2">
84 <Textarea
85 ref={textareaRef}
86 // Locked while posting: the body is cleared on success, which would otherwise throw
87 // away a new draft typed while the request was still in flight.
88 disabled={submitting !== null || actionPending}
89 value={body}
90 rows={3}
91 placeholder="Leave a comment"
92 aria-label="Comment on this pull request"
93 onChange={(event) => setBody(event.target.value)}
94 onKeyDown={(event) => {
95 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
96 if (
97 event.key === "Enter" &&
98 (event.metaKey || event.ctrlKey) &&
99 !event.shiftKey &&
100 !event.altKey
101 ) {
102 event.preventDefault();
103 event.stopPropagation();
104 if (!event.repeat) void submit("comment");
105 }
106 }}
107 />
108 <div className="flex flex-wrap justify-end gap-2">
109 {followUpAction === null ? null : (
110 <Button
111 size="xs"
112 variant={followUpAction === "close" ? "destructive-outline" : "outline"}
113 disabled={body.trim().length === 0 || submitting !== null || actionPending}
114 onClick={() => void submit(followUpAction)}
115 >
116 {followUpAction === "close" ? (
117 <PullRequestGlyph.closed className="size-3.5" />
118 ) : (
119 <PullRequestGlyph.reopen className="size-3.5" />
120 )}
121 {submitting === followUpAction
122 ? followUpAction === "close"
123 ? "Closing..."
124 : "Reopening..."
125 : followUpAction === "close"
126 ? "Close with comment"
127 : "Reopen with comment"}
128 </Button>
129 )}
130 <Button
131 size="xs"
132 variant="outline"
133 disabled={body.trim().length === 0 || submitting !== null || actionPending}
134 onClick={() => void submit("comment")}
135 >
136 <SendIcon className="size-3.5" />
137 {submitting === "comment" ? "Posting..." : "Comment"}
138 </Button>
139 </div>
140 </div>
141 );