Open History to see the last commit
1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
3import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react";
4import { T, useGT, plural, part, select } from "gt-react";
5import { useRef, useState } from "react";
6
7import { Button } from "../ui/button";
8import { OpenPopupRegistration } from "../ui/open-popups";
9import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
10import { Toggle, ToggleGroup } from "../ui/toggle-group";
11import { PullRequestCommentForm } from "./PullRequestCommentForm";
12import { PullRequestReviewForm } from "./PullRequestReviewForm";
13import {
14 pullRequestReviewKey,
15 usePendingReviewComments,
16 usePullRequestReviewStore,
17} from "./pullRequestReviewStore";
18import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea";
19
20import { changeRequestTermKind } from "./changeRequestTerm";
21
22export function PullRequestComposer({
23 environmentId,
24 reference,
25 detail,
26 shownHeadSha,
27 actionPending,
28 onCommentAction,
29 onCommented,
30 onReviewSubmitted,
31 openReviewRequest,
32}: {
33 environmentId: EnvironmentId;
34 reference: PullRequestRef;
35 detail: PullRequestDetailView;
36 /** The head the reader was shown, which a background refresh of `detail` can run ahead of. */
37 shownHeadSha: string | undefined;
38 actionPending: boolean;
39 onCommentAction: (
40 body: string,
41 action: "close" | "reopen",
42 ) => Promise<{ readonly commentPosted: boolean }>;
43 onCommented: () => void;
44 onReviewSubmitted: () => void;
45 openReviewRequest?: number | undefined;
46}) {
47 const gt = useGT();
48 const term = changeRequestTermKind(sourceControlClients.get(detail.provider).changeRequest);
49 const [open, setOpen] = useState(false);
50 const [reviewPending, setReviewPending] = useState(false);
51 const [requestedMode, setRequestedMode] = useState<"comment" | "review">("comment");
52 const commentRef = useRef<HTMLTextAreaElement>(null);
53 const reviewRef = useRef<HTMLTextAreaElement>(null);
54 const pendingComments = usePendingReviewComments(reference);
55 const clearComments = usePullRequestReviewStore((store) => store.clear);
56 // Unsent summaries outlive the popover; select a boolean to avoid per-keystroke renders.
57 const reviewKey = pullRequestReviewKey(reference);
58 const summaryStarted = usePullRequestReviewStore(
59 (store) => (store.summaries[reviewKey] ?? "").trim().length > 0,
60 );
61 const reviewStarted = pendingComments.length > 0 || summaryStarted;
62 const [answeredOpenRequest, setAnsweredOpenRequest] = useState(openReviewRequest);
63 if (openReviewRequest !== answeredOpenRequest) {
64 setAnsweredOpenRequest(openReviewRequest);
65 setRequestedMode("review");
66 setOpen(true);
67 }
68
69 const canComment = detail.capabilities.comment && detail.viewerPermissions.comment;
70 const verdicts = detail.capabilities.review.verdicts.filter((verdict) =>
71 detail.viewerPermissions.verdicts.includes(verdict),
72 );
73 if (!canComment && verdicts.length === 0) return null;
74
75 const mode = canComment ? (verdicts.length === 0 ? "comment" : requestedMode) : "review";
76
77 return (
78 <Popover
79 open={open}
80 onOpenChange={(next) => {
81 if (next) setRequestedMode(reviewStarted ? "review" : "comment");
82 setOpen(next);
83 }}
84 >
85 {open ? <OpenPopupRegistration /> : null}
86 <PopoverTrigger
87 render={<Button size="icon" variant="glass" />}
88 aria-label={
89 pendingComments.length > 0
90 ? gt`${plural(
91 { count: pendingComments.length },
92 {
93 one: part`Review ${select(
94 { term },
95 {
96 mr: "merge request",
97 cr: "change request",
98 other: "pull request",
99 },
100 )}, ${{ count: pendingComments.length }} comment pending`,
101 other: part`Review ${select(
102 { term },
103 {
104 mr: "merge request",
105 cr: "change request",
106 other: "pull request",
107 },
108 )}, ${{ count: pendingComments.length }} comments pending`,
109 },
110 )}`
111 : reviewStarted || !canComment
112 ? gt`Review ${select(
113 { term },
114 {
115 mr: "merge request",
116 cr: "change request",
117 other: "pull request",
118 },
119 )}`
120 : gt`Comment on ${select(
121 { term },
122 {
123 mr: "merge request",
124 cr: "change request",
125 other: "pull request",
126 },
127 )}`
128 }
129 >
130 <MessageSquareIcon className="size-4" />
131 {pendingComments.length > 0 ? (
132 <span
133 aria-hidden
134 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-3xs font-semibold tabular-nums text-white"
135 >
136 {pendingComments.length}
137 </span>
138 ) : null}
139 </PopoverTrigger>
140 <PopoverPopup
141 keepMounted
142 side="top"
143 align="end"
144 sideOffset={8}
145 width="lg"
146 initialFocus={mode === "review" ? reviewRef : commentRef}
147 aria-label={gt`${select(
148 { term },
149 {
150 mr: "Merge request",
151 cr: "Change request",
152 other: "Pull request",
153 },
154 )} composer`}
155 >
156 <div className="mb-3 flex items-center justify-between gap-2">
157 {canComment && verdicts.length > 0 ? (
158 <ToggleGroup
159 aria-label={gt`Composer mode`}
160 variant="segmented"
161 value={[mode]}
162 onValueChange={(next) => {
163 const value = next[0];
164 if (value === "comment" || value === "review") setRequestedMode(value);
165 }}
166 >
167 <Toggle value="comment">
168 <T context="noun, composer mode">Comment</T>
169 </Toggle>
170 <Toggle value="review">
171 {pendingComments.length > 0
172 ? gt`Review (${{ count: pendingComments.length }})`
173 : gt({ context: "noun, composer mode" })`Review`}
174 </Toggle>
175 </ToggleGroup>
176 ) : (
177 <PopoverTitle>
178 {mode === "review"
179 ? gt`Review ${select(
180 { term },
181 {
182 mr: "merge request",
183 cr: "change request",
184 other: "pull request",
185 },
186 )}`
187 : gt`Comment on ${select(
188 { term },
189 {
190 mr: "merge request",
191 cr: "change request",
192 other: "pull request",
193 },
194 )}`}
195 </PopoverTitle>
196 )}
197 <div className="flex items-center gap-1">
198 {mode === "review" && pendingComments.length > 0 ? (
199 <Button
200 size="icon-xs"
201 variant="ghost"
202 aria-label={gt`Discard pending line comments`}
203 title={gt`Discard pending line comments`}
204 disabled={reviewPending}
205 onClick={() => clearComments(reviewKey)}
206 >
207 <Trash2Icon className="size-3.5" />
208 </Button>
209 ) : null}
210 <PopoverClose
211 render={<Button size="icon-xs" variant="ghost" />}
212 aria-label={gt`Close composer`}
213 >
214 <XIcon className="size-3.5" />
215 </PopoverClose>
216 </div>
217 </div>
218 {/* Keep forms mounted to preserve drafts and submit guards across dismissal. */}
219 {verdicts.length > 0 ? (
220 <div hidden={mode !== "review"}>
221 <PullRequestReviewForm
222 active={open && mode === "review"}
223 environmentId={environmentId}
224 reference={reference}
225 headSha={shownHeadSha}
226 completionScope={pullRequestCompletionScope(detail, environmentId, reference)}
227 term={term}
228 verdicts={verdicts}
229 requestChangesSummaryRequired={sourceControlClients
230 .get(detail.provider)
231 .reviewSummaryRequired("request-changes")}
232 textareaRef={reviewRef}
233 pending={reviewPending}
234 onPendingChange={setReviewPending}
235 onSubmitted={() => {
236 setOpen(false);
237 onReviewSubmitted();
238 }}
239 // A refusal usually means new commits, so show what the host has now.
240 onRefused={onReviewSubmitted}
241 />
242 </div>
243 ) : null}
244 {canComment ? (
245 <div hidden={mode !== "comment"}>
246 <PullRequestCommentForm
247 active={open && mode === "comment"}
248 environmentId={environmentId}
249 reference={reference}
250 detail={detail}
251 actionPending={actionPending}
252 textareaRef={commentRef}
253 onCommentAction={onCommentAction}
254 onCommented={onCommented}
255 onClose={() => setOpen(false)}
256 />
257 </div>
258 ) : null}
259 </PopoverPopup>
260 </Popover>
261 );
262}