Open History to see the last commit
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={<Button size="icon" variant="glass" />}
82 // The only label this control has, so the pending count belongs in it: the badge beside
83 // the icon is decorative and a reader who cannot see it still needs the number.
84 aria-label={
85 pendingComments.length > 0
86 ? `Review pull request, ${pendingComments.length} ${pendingComments.length === 1 ? "comment" : "comments"} pending`
87 : reviewStarted || !canComment
88 ? "Review pull request"
89 : "Comment on pull request"
90 }
91 >
92 <MessageSquareIcon className="size-4" />
93 {pendingComments.length > 0 ? (
94 <span
95 aria-hidden
96 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"
97 >
98 {pendingComments.length}
99 </span>
100 ) : null}
101 </PopoverTrigger>
102 <PopoverPopup
103 keepMounted
104 side="top"
105 align="end"
106 sideOffset={8}
107 width="lg"
108 initialFocus={mode === "review" ? reviewRef : commentRef}
109 aria-label="Pull request composer"
110 >
111 <div className="mb-3 flex items-center justify-between gap-2">
112 {canComment && verdicts.length > 0 ? (
113 <ToggleGroup
114 aria-label="Composer mode"
115 variant="segmented"
116 value={[mode]}
117 onValueChange={(next) => {
118 const value = next[0];
119 if (value === "comment" || value === "review") setRequestedMode(value);
120 }}
121 >
122 <Toggle value="comment">Comment</Toggle>
123 <Toggle value="review">
124 {pendingComments.length > 0 ? `Review (${pendingComments.length})` : "Review"}
125 </Toggle>
126 </ToggleGroup>
127 ) : (
128 <PopoverTitle>
129 {mode === "review" ? "Review pull request" : "Comment on pull request"}
130 </PopoverTitle>
131 )}
132 <div className="flex items-center gap-1">
133 {mode === "review" && pendingComments.length > 0 ? (
134 <Button
135 size="icon-xs"
136 variant="ghost"
137 aria-label="Discard pending line comments"
138 title="Discard pending line comments"
139 disabled={reviewPending}
140 onClick={() => clearComments(reviewKey)}
141 >
142 <Trash2Icon className="size-3.5" />
143 </Button>
144 ) : null}
145 <PopoverClose
146 render={<Button size="icon-xs" variant="ghost" />}
147 aria-label="Close composer"
148 >
149 <XIcon className="size-3.5" />
150 </PopoverClose>
151 </div>
152 </div>
153 {/* Keep both forms mounted across toggles and dismissal so drafts and in-flight
154 submit guards survive closing and reopening the composer. */}
155 {verdicts.length > 0 ? (
156 <div hidden={mode !== "review"}>
157 <PullRequestReviewForm
158 environmentId={environmentId}
159 reference={reference}
160 verdicts={verdicts}
161 requestChangesSummaryRequired={detail.provider === "forgejo"}
162 textareaRef={reviewRef}
163 pending={reviewPending}
164 onPendingChange={setReviewPending}
165 onSubmitted={() => {
166 setOpen(false);
167 onReviewSubmitted();
168 }}
169 />
170 </div>
171 ) : null}
172 {canComment ? (
173 <div hidden={mode !== "comment"}>
174 <PullRequestCommentForm
175 environmentId={environmentId}
176 reference={reference}
177 detail={detail}
178 actionPending={actionPending}
179 textareaRef={commentRef}
180 onCommentAction={onCommentAction}
181 onCommented={onCommented}
182 onClose={() => setOpen(false)}
183 />
184 </div>
185 ) : null}
186 </PopoverPopup>
187 </Popover>
188 );
189}