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