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

PullRequestCommentForm.tsx

144 lines4.7 KB

1/**
2 * The comment half of the floating composer: a remark on the pull request itself, optionally
3 * the one that closes or reopens it. The popover around it belongs to PullRequestComposer.
4 */
5import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
6import { SendIcon } from "lucide-react";
7import { useState, type RefObject } from "react";
8​
9import { useAtomCommand } from "~/state/use-atom-command";
10import { pullRequestEnvironment } from "~/state/pullRequests";
11​
12import { Button } from "../ui/button";
13import { Textarea } from "../ui/textarea";
14import { toastManager } from "../ui/toast";
15import { PullRequestGlyph } from "./pullRequestIcons";
16​
17export function PullRequestCommentForm({
18 environmentId,
19 reference,
20 detail,
21 actionPending,
22 textareaRef,
23 onCommentAction,
24 onCommented,
25 onClose,
26}: {
27 environmentId: EnvironmentId;
28 reference: PullRequestRef;
29 detail: PullRequestDetailView;
30 actionPending: boolean;
31 textareaRef: RefObject<HTMLTextAreaElement | null>;
32 onCommentAction: (
33 body: string,
34 action: "close" | "reopen",
35 ) => Promise<{ readonly commentPosted: boolean }>;
36 onCommented: () => void;
37 onClose: () => void;
38}) {
39 const [body, setBody] = useState("");
40 const [submitting, setSubmitting] = useState<"comment" | "close" | "reopen" | null>(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 onClose();
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 onClose();
81 onCommented();
82 };
83​
84 return (
85 <div className="space-y-2">
86 <Textarea
87 ref={textareaRef}
88 // Locked while posting: the body is cleared on success, which would otherwise throw
89 // away a new draft typed while the request was still in flight.
90 disabled={submitting !== null || actionPending}
91 value={body}
92 rows={3}
93 placeholder="Leave a comment"
94 aria-label="Comment on this pull request"
95 onChange={(event) => setBody(event.target.value)}
96 onKeyDown={(event) => {
97 if (event.nativeEvent.isComposing || event.keyCode === 229) return;
98 if (
99 event.key === "Enter" &&
100 (event.metaKey || event.ctrlKey) &&
101 !event.shiftKey &&
102 !event.altKey
103 ) {
104 event.preventDefault();
105 event.stopPropagation();
106 if (!event.repeat) void submit("comment");
107 }
108 }}
109 />
110 <div className="flex flex-wrap justify-end gap-2">
111 {followUpAction === null ? null : (
112 <Button
113 size="xs"
114 variant={followUpAction === "close" ? "destructive-outline" : "outline"}
115 disabled={body.trim().length === 0 || submitting !== null || actionPending}
116 onClick={() => void submit(followUpAction)}
117 >
118 {followUpAction === "close" ? (
119 <PullRequestGlyph.closed className="size-3.5" />
120 ) : (
121 <PullRequestGlyph.reopen className="size-3.5" />
122 )}
123 {submitting === followUpAction
124 ? followUpAction === "close"
125 ? "Closing..."
126 : "Reopening..."
127 : followUpAction === "close"
128 ? "Close with comment"
129 : "Reopen with comment"}
130 </Button>
131 )}
132 <Button
133 size="xs"
134 variant="outline"
135 disabled={body.trim().length === 0 || submitting !== null || actionPending}
136 onClick={() => void submit("comment")}
137 >
138 <SendIcon className="size-3.5" />
139 {submitting === "comment" ? "Posting..." : "Comment"}
140 </Button>
141 </div>
142 </div>
143 );