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

PullRequestReviewBar.tsx

153 lines5.3 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 onSubmitted,
54}: {
55 environmentId: EnvironmentId;
56 reference: PullRequestRef;
57 verdicts: ReadonlyArray<PullRequestReviewVerdict>;
58 onSubmitted: () => void;
59}) {
60 const [pending, setPending] = useState(false);
61 const comments = usePendingReviewComments(reference);
62 const reviewKey = pullRequestReviewKey(reference);
63 // The panel stays mounted while the selected pull request changes. Keeping summaries beside
64 // the keyed line-comment drafts makes the selected pull request's body correct on the first
65 // render, before an effect could reset state left behind by the previous one.
66 const body = usePullRequestReviewStore((store) => store.summaries[reviewKey] ?? "");
67 const clear = usePullRequestReviewStore((store) => store.clear);
68 const removeComments = usePullRequestReviewStore((store) => store.removeComments);
69 const setSummary = usePullRequestReviewStore((store) => store.setSummary);
70 const clearSummary = usePullRequestReviewStore((store) => store.clearSummary);
71 const submitReview = useAtomCommand(pullRequestEnvironment.submitReview, {
72 reportFailure: false,
73 });
74​
75 const offered = VERDICTS.filter((verdict) => verdicts.includes(verdict.value));
76 if (offered.length === 0) return null;
77​
78 const submit = async (verdict: (typeof VERDICTS)[number]) => {
79 if (pending) return;
80 const submittedBody = body;
81 const submittedComments = comments;
82 setPending(true);
83 const result = await submitReview({
84 environmentId,
85 input: {
86 ...reference,
87 verdict: verdict.value,
88 body: submittedBody,
89 comments: submittedComments,
90 },
91 });
92 setPending(false);
93 if (result._tag === "Failure") {
94 // The draft is kept: whatever went wrong, retyping the review is not the answer.
95 toastManager.add({ type: "error", title: "The review could not be submitted" });
96 return;
97 }
98 // More remarks may have been added while the host was accepting this snapshot. Leave those,
99 // and any summary revised in the meantime, ready for the next review.
100 removeComments(
101 reviewKey,
102 submittedComments.map((comment) => comment.id),
103 );
104 clearSummary(reviewKey, submittedBody);
105 toastManager.add({ type: "success", title: verdict.sent });
106 onSubmitted();
107 };
108​
109 // An approval needs no words; anything else does, unless it carries line comments instead.
110 const canSubmit = (verdict: PullRequestReviewVerdict) =>
111 verdict === "approve" || body.trim().length > 0 || comments.length > 0;
112​
113 return (
114 <div className="px-4 py-3">
115 <div className="flex items-center gap-2 text-xs text-muted-foreground">
116 <span>
117 {comments.length === 0
118 ? "No line comments yet"
119 : `${comments.length} ${comments.length === 1 ? "comment" : "comments"} pending`}
120 </span>
121 {comments.length > 0 ? (
122 <Button size="xs" variant="ghost" disabled={pending} onClick={() => clear(reviewKey)}>
123 Discard
124 </Button>
125 ) : null}
126 </div>
127 <Textarea
128 size="sm"
129 className="mt-2"
130 value={body}
131 placeholder="Summarize your review (optional)"
132 aria-label="Review summary"
133 onChange={(event) => setSummary(reviewKey, event.target.value)}
134 />
135 <div className="mt-2 flex flex-wrap justify-end gap-2">
136 {offered.map((verdict) => (
137 <Button
138 key={verdict.value}
139 size="xs"
140 variant={verdict.value === "comment" ? "outline" : "default"}
141 disabled={pending || !canSubmit(verdict.value)}
142 onClick={() => void submit(verdict)}
143 >
144 <span className="flex items-center gap-1.5">
145 {verdict.icon}
146 {verdict.label}
147 </span>
148 </Button>
149 ))}
150 </div>
151 </div>
152 );