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

PullRequestComposer.tsx

195 lines7.5 KB

1/**
2 * The single floating control over a pull request. Commenting on the change and submitting the
3 * review that carries the Code tab's line comments used to float as two buttons that crowded
4 * each other and read as the same offer twice; they are two modes of one composer now.
5 *
6 * Opening picks the mode with work waiting in it, so a reader who has collected line comments
7 * lands on the review and everyone else lands on the comment box. Either mode's draft survives
8 * the toggle: they are separate texts going to separate places, and merging them would send a
9 * summary as a comment or the reverse.
10 */
11import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
12import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react";
13import { useRef, useState } from "react";
14​
15import { Button } from "../ui/button";
16import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
17import { Toggle, ToggleGroup } from "../ui/toggle-group";
18import { PullRequestCommentForm } from "./PullRequestCommentForm";
19import { PullRequestReviewForm } from "./PullRequestReviewForm";
20import {
21 pullRequestReviewKey,
22 usePendingReviewComments,
23 usePullRequestReviewStore,
24} from "./pullRequestReviewStore";
25​
26export function PullRequestComposer({
27 environmentId,
28 reference,
29 detail,
30 actionPending,
31 onCommentAction,
32 onCommented,
33 onReviewSubmitted,
34}: {
35 environmentId: EnvironmentId;
36 reference: PullRequestRef;
37 detail: PullRequestDetailView;
38 actionPending: boolean;
39 onCommentAction: (
40 body: string,
41 action: "close" | "reopen",
42 ) => Promise<{ readonly commentPosted: boolean }>;
43 onCommented: () => void;
44 onReviewSubmitted: () => void;
45}) {
46 const [open, setOpen] = useState(false);
47 const [reviewPending, setReviewPending] = useState(false);
48 const [requestedMode, setRequestedMode] = useState<"comment" | "review">("comment");
49 const commentRef = useRef<HTMLTextAreaElement>(null);
50 const reviewRef = useRef<HTMLTextAreaElement>(null);
51 const pendingComments = usePendingReviewComments(reference);
52 const clearComments = usePullRequestReviewStore((store) => store.clear);
53 // A summary typed but not sent is review work too, and it outlives the popover. Selected as a
54 // boolean rather than the text, so typing one does not re-render the composer per keystroke.
55 const reviewKey = pullRequestReviewKey(reference);
56 const summaryStarted = usePullRequestReviewStore(
57 (store) => (store.summaries[reviewKey] ?? "").trim().length > 0,
58 );
59 const reviewStarted = pendingComments.length > 0 || summaryStarted;
60​
61 // What is offered is the intersection of two different questions: what this host can do at
62 // all, and what this account may do on this repository. Either one saying no means a control
63 // that would only ever end in a refusal.
64 const canComment = detail.capabilities.comment && detail.viewerPermissions.comment;
65 const verdicts = detail.capabilities.review.verdicts.filter((verdict) =>
66 detail.viewerPermissions.verdicts.includes(verdict),
67 );
68 if (!canComment && verdicts.length === 0) return null;
69​
70 const mode = canComment ? (verdicts.length === 0 ? "comment" : requestedMode) : "review";
71​
72 return (
73 <Popover
74 open={open}
75 onOpenChange={(next) => {
76 if (next) setRequestedMode(reviewStarted ? "review" : "comment");
77 setOpen(next);
78 }}
79 >
80 <PopoverTrigger
81 render={
82 <Button
83 size="icon"
84 variant="glass"
85 className="rounded-full shadow-lg [--glass-opacity:50%]"
86 />
87 }
88 // The only label this control has, so the pending count belongs in it: the badge beside
89 // the icon is decorative and a reader who cannot see it still needs the number.
90 aria-label={
91 pendingComments.length > 0
92 ? `Review pull request, ${pendingComments.length} ${pendingComments.length === 1 ? "comment" : "comments"} pending`
93 : reviewStarted || !canComment
94 ? "Review pull request"
95 : "Comment on pull request"
96 }
97 >
98 <MessageSquareIcon className="size-4" />
99 {pendingComments.length > 0 ? (
100 <span
101 aria-hidden
102 className="absolute -top-1 -right-1 flex h-3.5 min-w-3.5 items-center justify-center rounded-full bg-info px-1 text-[9px] font-semibold tabular-nums text-white"
103 >
104 {pendingComments.length}
105 </span>
106 ) : null}
107 </PopoverTrigger>
108 <PopoverPopup
109 keepMounted
110 side="top"
111 align="end"
112 sideOffset={8}
113 width="lg"
114 initialFocus={mode === "review" ? reviewRef : commentRef}
115 aria-label="Pull request composer"
116 >
117 <div className="mb-3 flex items-center justify-between gap-2">
118 {canComment && verdicts.length > 0 ? (
119 <ToggleGroup
120 aria-label="Composer mode"
121 variant="segmented"
122 value={[mode]}
123 onValueChange={(next) => {
124 const value = next[0];
125 if (value === "comment" || value === "review") setRequestedMode(value);
126 }}
127 >
128 <Toggle value="comment">Comment</Toggle>
129 <Toggle value="review">
130 {pendingComments.length > 0 ? `Review (${pendingComments.length})` : "Review"}
131 </Toggle>
132 </ToggleGroup>
133 ) : (
134 <PopoverTitle>
135 {mode === "review" ? "Review pull request" : "Comment on pull request"}
136 </PopoverTitle>
137 )}
138 <div className="flex items-center gap-1">
139 {mode === "review" && pendingComments.length > 0 ? (
140 <Button
141 size="icon-xs"
142 variant="ghost"
143 aria-label="Discard pending line comments"
144 title="Discard pending line comments"
145 disabled={reviewPending}
146 onClick={() => clearComments(reviewKey)}
147 >
148 <Trash2Icon className="size-3.5" />
149 </Button>
150 ) : null}
151 <PopoverClose
152 render={<Button size="icon-xs" variant="ghost" />}
153 aria-label="Close composer"
154 >
155 <XIcon className="size-3.5" />
156 </PopoverClose>
157 </div>
158 </div>
159 {/* Keep both forms mounted across toggles and dismissal so drafts and in-flight
160 submit guards survive closing and reopening the composer. */}
161 {verdicts.length > 0 ? (
162 <div hidden={mode !== "review"}>
163 <PullRequestReviewForm
164 environmentId={environmentId}
165 reference={reference}
166 verdicts={verdicts}
167 requestChangesSummaryRequired={detail.provider === "forgejo"}
168 textareaRef={reviewRef}
169 pending={reviewPending}
170 onPendingChange={setReviewPending}
171 onSubmitted={() => {
172 setOpen(false);
173 onReviewSubmitted();
174 }}
175 />
176 </div>
177 ) : null}
178 {canComment ? (
179 <div hidden={mode !== "comment"}>
180 <PullRequestCommentForm
181 environmentId={environmentId}
182 reference={reference}
183 detail={detail}
184 actionPending={actionPending}
185 textareaRef={commentRef}
186 onCommentAction={onCommentAction}
187 onCommented={onCommented}
188 onClose={() => setOpen(false)}
189 />
190 </div>
191 ) : null}
192 </PopoverPopup>
193 </Popover>
194 );