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

PullRequestReviewForm.tsx

175 lines6.2 KB

1import { useAtomCommand } from "~/state/use-atom-command";
2/**
3 * The review half of the floating composer: the summary and the verdict that sends it, together
4 * with whatever line comments the review is holding. The count of those lives on the composer's
5 * trigger and mode toggle, and each pending card can be dropped from the diff, so neither is
6 * repeated here. The popover around it belongs to PullRequestComposer.
7 */
8import type { EnvironmentId, PullRequestRef, PullRequestReviewVerdict } from "@t3tools/contracts";
9import { CheckIcon, MessageSquareIcon, XCircleIcon } from "lucide-react";
10import { useState, type ReactNode, type RefObject } from "react";
11​
12import { pullRequestEnvironment } from "~/state/pullRequests";
13​
14import { Button } from "../ui/button";
15import { Select, SelectItem, SelectPopup, SelectTrigger } from "../ui/select";
16import { Textarea } from "../ui/textarea";
17import { toastManager } from "../ui/toast";
18import {
19 pullRequestReviewKey,
20 usePendingReviewComments,
21 usePullRequestReviewStore,
22} from "./pullRequestReviewStore";
23​
24const VERDICTS: ReadonlyArray<{
25 readonly value: PullRequestReviewVerdict;
26 readonly label: string;
27 readonly sent: string;
28 readonly icon: ReactNode;
29}> = [
30 {
31 value: "comment",
32 label: "Comment",
33 sent: "Review submitted",
34 icon: <MessageSquareIcon className="size-3" />,
35 },
36 {
37 value: "approve",
38 label: "Approve",
39 sent: "Pull request approved",
40 icon: <CheckIcon className="size-3" />,
41 },
42 {
43 value: "request-changes",
44 label: "Request changes",
45 sent: "Changes requested",
46 icon: <XCircleIcon className="size-3" />,
47 },
48];
49​
50export function PullRequestReviewForm({
51 environmentId,
52 reference,
53 verdicts,
54 requestChangesSummaryRequired,
55 textareaRef,
56 pending,
57 onPendingChange,
58 onSubmitted,
59}: {
60 environmentId: EnvironmentId;
61 reference: PullRequestRef;
62 verdicts: ReadonlyArray<PullRequestReviewVerdict>;
63 requestChangesSummaryRequired: boolean;
64 textareaRef: RefObject<HTMLTextAreaElement | null>;
65 pending: boolean;
66 onPendingChange: (pending: boolean) => void;
67 onSubmitted: () => void;
68}) {
69 const [requestedVerdict, setRequestedVerdict] = useState<PullRequestReviewVerdict>("comment");
70 const comments = usePendingReviewComments(reference);
71 const reviewKey = pullRequestReviewKey(reference);
72 // The panel stays mounted while the selected pull request changes. Keeping summaries beside
73 // the keyed line-comment drafts makes the selected pull request's body correct on the first
74 // render, before an effect could reset state left behind by the previous one.
75 const body = usePullRequestReviewStore((store) => store.summaries[reviewKey] ?? "");
76 const removeComments = usePullRequestReviewStore((store) => store.removeComments);
77 const setSummary = usePullRequestReviewStore((store) => store.setSummary);
78 const clearSummary = usePullRequestReviewStore((store) => store.clearSummary);
79 const submitReview = useAtomCommand(pullRequestEnvironment.submitReview, {
80 reportFailure: false,
81 });
82​
83 const offered = VERDICTS.filter((verdict) => verdicts.includes(verdict.value));
84 const selectedVerdict =
85 offered.find((verdict) => verdict.value === requestedVerdict) ?? offered[0];
86​
87 const submit = async (verdict: (typeof VERDICTS)[number]) => {
88 if (pending) return;
89 const submittedBody = body;
90 const submittedComments = comments;
91 onPendingChange(true);
92 const result = await submitReview({
93 environmentId,
94 input: {
95 ...reference,
96 verdict: verdict.value,
97 body: submittedBody,
98 comments: submittedComments,
99 },
100 });
101 onPendingChange(false);
102 if (result._tag === "Failure") {
103 // The draft is kept: whatever went wrong, retyping the review is not the answer.
104 toastManager.add({ type: "error", title: "The review could not be submitted" });
105 return;
106 }
107 // More remarks may have been added while the host was accepting this snapshot. Leave those,
108 // and any summary revised in the meantime, ready for the next review.
109 removeComments(
110 reviewKey,
111 submittedComments.map((comment) => comment.id),
112 );
113 clearSummary(reviewKey, submittedBody);
114 toastManager.add({ type: "success", title: verdict.sent });
115 onSubmitted();
116 };
117​
118 // Forgejo requires a summary when requesting changes, even with inline comments.
119 const canSubmit = (verdict: PullRequestReviewVerdict) =>
120 verdict === "request-changes" && requestChangesSummaryRequired
121 ? body.trim().length > 0
122 : verdict === "approve" || body.trim().length > 0 || comments.length > 0;
123​
124 return (
125 <>
126 <Textarea
127 ref={textareaRef}
128 rows={3}
129 value={body}
130 placeholder={
131 requestChangesSummaryRequired && verdicts.includes("request-changes")
132 ? "Summarize your review (required to request changes)"
133 : "Summarize your review (optional)"
134 }
135 aria-label="Review summary"
136 onChange={(event) => setSummary(reviewKey, event.target.value)}
137 />
138 <div className="mt-2 flex justify-between gap-2">
139 <Select
140 value={selectedVerdict?.value ?? null}
141 disabled={pending}
142 onValueChange={(value) => {
143 if (value !== null) setRequestedVerdict(value);
144 }}
145 >
146 <SelectTrigger size="xs" className="w-auto min-w-0" aria-label="Review verdict">
147 <span className="flex items-center gap-1.5">
148 {selectedVerdict?.icon}
149 {selectedVerdict?.label}
150 </span>
151 </SelectTrigger>
152 <SelectPopup side="top" alignItemWithTrigger={false}>
153 {offered.map((verdict) => (
154 <SelectItem key={verdict.value} value={verdict.value}>
155 <span className="flex items-center gap-1.5">
156 {verdict.icon}
157 {verdict.label}
158 </span>
159 </SelectItem>
160 ))}
161 </SelectPopup>
162 </Select>
163 <Button
164 size="xs"
165 disabled={pending || selectedVerdict === undefined || !canSubmit(selectedVerdict.value)}
166 onClick={() => {
167 if (selectedVerdict !== undefined) void submit(selectedVerdict);
168 }}
169 >
170 {pending ? "Submitting..." : "Submit review"}
171 </Button>
172 </div>
173 </>
174 );