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

PullRequestComposer.tsx

192 lines7.6 KB

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