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

PullRequestReviewBar.tsx

161 lines5.6 KB

1/**
2 * The review form floated over the Code tab: how many comments the review is holding, its
3 * summary, and the verdict that sends the lot. Hidden entirely on a host that cannot take a
4 * review. The glass card frame belongs to the caller (PullRequestCodeTab), which is why this
5 * only contributes its own padding.
6 */
7import type { EnvironmentId, PullRequestRef, PullRequestReviewVerdict } from "@t3tools/contracts";
8import { CheckIcon, MessageSquareIcon, XCircleIcon } from "lucide-react";
9import { useState, type ReactNode } from "react";
10​
11import { pullRequestEnvironment } from "~/state/pullRequests";
12import { useAtomCommand } from "~/state/use-atom-command";
13​
14import { Button } from "../ui/button";
15import { Textarea } from "../ui/textarea";
16import { toastManager } from "../ui/toast";
17import {
18 pullRequestReviewKey,
19 usePendingReviewComments,
20 usePullRequestReviewStore,
21} from "./pullRequestReviewStore";
22​
23const VERDICTS: ReadonlyArray<{
24 readonly value: PullRequestReviewVerdict;
25 readonly label: string;
26 readonly sent: string;
27 readonly icon: ReactNode;
28}> = [
29 {
30 value: "comment",
31 label: "Comment",
32 sent: "Review submitted",
33 icon: <MessageSquareIcon className="size-3" />,
34 },
35 {
36 value: "approve",
37 label: "Approve",
38 sent: "Pull request approved",
39 icon: <CheckIcon className="size-3" />,
40 },
41 {
42 value: "request-changes",
43 label: "Request changes",
44 sent: "Changes requested",
45 icon: <XCircleIcon className="size-3" />,
46 },
47];
48​
49export function PullRequestReviewBar({
50 environmentId,
51 reference,
52 verdicts,
53 requestChangesSummaryRequired,
54 onSubmitted,
55}: {
56 environmentId: EnvironmentId;
57 reference: PullRequestRef;
58 verdicts: ReadonlyArray<PullRequestReviewVerdict>;
59 requestChangesSummaryRequired: boolean;
60 onSubmitted: () => void;
61}) {
62 const [pending, setPending] = useState(false);
63 const comments = usePendingReviewComments(reference);
64 const reviewKey = pullRequestReviewKey(reference);
65 // The panel stays mounted while the selected pull request changes. Keeping summaries beside
66 // the keyed line-comment drafts makes the selected pull request's body correct on the first
67 // render, before an effect could reset state left behind by the previous one.
68 const body = usePullRequestReviewStore((store) => store.summaries[reviewKey] ?? "");
69 const clear = usePullRequestReviewStore((store) => store.clear);
70 const removeComments = usePullRequestReviewStore((store) => store.removeComments);
71 const setSummary = usePullRequestReviewStore((store) => store.setSummary);
72 const clearSummary = usePullRequestReviewStore((store) => store.clearSummary);
73 const submitReview = useAtomCommand(pullRequestEnvironment.submitReview, {
74 reportFailure: false,
75 });
76​
77 const offered = VERDICTS.filter((verdict) => verdicts.includes(verdict.value));
78 if (offered.length === 0) return null;
79​
80 const submit = async (verdict: (typeof VERDICTS)[number]) => {
81 if (pending) return;
82 const submittedBody = body;
83 const submittedComments = comments;
84 setPending(true);
85 const result = await submitReview({
86 environmentId,
87 input: {
88 ...reference,
89 verdict: verdict.value,
90 body: submittedBody,
91 comments: submittedComments,
92 },
93 });
94 setPending(false);
95 if (result._tag === "Failure") {
96 // The draft is kept: whatever went wrong, retyping the review is not the answer.
97 toastManager.add({ type: "error", title: "The review could not be submitted" });
98 return;
99 }
100 // More remarks may have been added while the host was accepting this snapshot. Leave those,
101 // and any summary revised in the meantime, ready for the next review.
102 removeComments(
103 reviewKey,
104 submittedComments.map((comment) => comment.id),
105 );
106 clearSummary(reviewKey, submittedBody);
107 toastManager.add({ type: "success", title: verdict.sent });
108 onSubmitted();
109 };
110​
111 // Forgejo requires a summary when requesting changes, even with inline comments.
112 const canSubmit = (verdict: PullRequestReviewVerdict) =>
113 verdict === "request-changes" && requestChangesSummaryRequired
114 ? body.trim().length > 0
115 : verdict === "approve" || body.trim().length > 0 || comments.length > 0;
116​
117 return (
118 <div className="px-4 py-3">
119 <div className="flex items-center gap-2 text-xs text-muted-foreground">
120 <span>
121 {comments.length === 0
122 ? "No line comments yet"
123 : `${comments.length} ${comments.length === 1 ? "comment" : "comments"} pending`}
124 </span>
125 {comments.length > 0 ? (
126 <Button size="xs" variant="ghost" disabled={pending} onClick={() => clear(reviewKey)}>
127 Discard
128 </Button>
129 ) : null}
130 </div>
131 <Textarea
132 size="sm"
133 className="mt-2"
134 value={body}
135 placeholder={
136 requestChangesSummaryRequired && verdicts.includes("request-changes")
137 ? "Summarize your review (required to request changes)"
138 : "Summarize your review (optional)"
139 }
140 aria-label="Review summary"
141 onChange={(event) => setSummary(reviewKey, event.target.value)}
142 />
143 <div className="mt-2 flex flex-wrap justify-end gap-2">
144 {offered.map((verdict) => (
145 <Button
146 key={verdict.value}
147 size="xs"
148 variant={verdict.value === "comment" ? "outline" : "default"}
149 disabled={pending || !canSubmit(verdict.value)}
150 onClick={() => void submit(verdict)}
151 >
152 <span className="flex items-center gap-1.5">
153 {verdict.icon}
154 {verdict.label}
155 </span>
156 </Button>
157 ))}
158 </div>
159 </div>
160 );