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