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

PullRequestComposer.tsx

190 lines7.0 KB

1import {
2 capitalizeTerm,
3 sourceControlClients,
4} from "@t3tools/client-runtime/source-control-clients";
5import type { EnvironmentId, PullRequestDetailView, PullRequestRef } from "@t3tools/contracts";
6import { MessageSquareIcon, Trash2Icon, XIcon } from "lucide-react";
7import { useRef, useState } from "react";
8​
9import { Button } from "../ui/button";
10import { Popover, PopoverClose, PopoverPopup, PopoverTitle, PopoverTrigger } from "../ui/popover";
11import { Toggle, ToggleGroup } from "../ui/toggle-group";
12import { PullRequestCommentForm } from "./PullRequestCommentForm";
13import { PullRequestReviewForm } from "./PullRequestReviewForm";
14import {
15 pullRequestReviewKey,
16 usePendingReviewComments,
17 usePullRequestReviewStore,
18} from "./pullRequestReviewStore";
19​
20export function PullRequestComposer({
21 environmentId,
22 reference,
23 detail,
24 actionPending,
25 onCommentAction,
26 onCommented,
27 onReviewSubmitted,
28 openReviewRequest,
29}: {
30 environmentId: EnvironmentId;
31 reference: PullRequestRef;
32 detail: PullRequestDetailView;
33 actionPending: boolean;
34 onCommentAction: (
35 body: string,
36 action: "close" | "reopen",
37 ) => Promise<{ readonly commentPosted: boolean }>;
38 onCommented: () => void;
39 onReviewSubmitted: () => void;
40 openReviewRequest?: number | undefined;
41}) {
42 const term = sourceControlClients.get(detail.provider).changeRequest.singular;
43 const [open, setOpen] = useState(false);
44 const [reviewPending, setReviewPending] = useState(false);
45 const [requestedMode, setRequestedMode] = useState<"comment" | "review">("comment");
46 const commentRef = useRef<HTMLTextAreaElement>(null);
47 const reviewRef = useRef<HTMLTextAreaElement>(null);
48 const pendingComments = usePendingReviewComments(reference);
49 const clearComments = usePullRequestReviewStore((store) => store.clear);
50 // A summary typed but not sent is review work too, and it outlives the popover. Selected as a
51 // boolean rather than the text, so typing one does not re-render the composer per keystroke.
52 const reviewKey = pullRequestReviewKey(reference);
53 const summaryStarted = usePullRequestReviewStore(
54 (store) => (store.summaries[reviewKey] ?? "").trim().length > 0,
55 );
56 const reviewStarted = pendingComments.length > 0 || summaryStarted;
57 const [answeredOpenRequest, setAnsweredOpenRequest] = useState(openReviewRequest);
58 if (openReviewRequest !== answeredOpenRequest) {
59 setAnsweredOpenRequest(openReviewRequest);
60 setRequestedMode("review");
61 setOpen(true);
62 }
63​
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 aria-label={
83 pendingComments.length > 0
84 ? `Review ${term}, ${pendingComments.length} ${pendingComments.length === 1 ? "comment" : "comments"} pending`
85 : reviewStarted || !canComment
86 ? `Review ${term}`
87 : `Comment on ${term}`
88 }
89 >
90 <MessageSquareIcon className="size-4" />
91 {pendingComments.length > 0 ? (
92 <span
93 aria-hidden
94 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"
95 >
96 {pendingComments.length}
97 </span>
98 ) : null}
99 </PopoverTrigger>
100 <PopoverPopup
101 keepMounted
102 side="top"
103 align="end"
104 sideOffset={8}
105 width="lg"
106 initialFocus={mode === "review" ? reviewRef : commentRef}
107 aria-label={`${capitalizeTerm(term)} composer`}
108 >
109 <div className="mb-3 flex items-center justify-between gap-2">
110 {canComment && verdicts.length > 0 ? (
111 <ToggleGroup
112 aria-label="Composer mode"
113 variant="segmented"
114 value={[mode]}
115 onValueChange={(next) => {
116 const value = next[0];
117 if (value === "comment" || value === "review") setRequestedMode(value);
118 }}
119 >
120 <Toggle value="comment">Comment</Toggle>
121 <Toggle value="review">
122 {pendingComments.length > 0 ? `Review (${pendingComments.length})` : "Review"}
123 </Toggle>
124 </ToggleGroup>
125 ) : (
126 <PopoverTitle>
127 {mode === "review" ? `Review ${term}` : `Comment on ${term}`}
128 </PopoverTitle>
129 )}
130 <div className="flex items-center gap-1">
131 {mode === "review" && pendingComments.length > 0 ? (
132 <Button
133 size="icon-xs"
134 variant="ghost"
135 aria-label="Discard pending line comments"
136 title="Discard pending line comments"
137 disabled={reviewPending}
138 onClick={() => clearComments(reviewKey)}
139 >
140 <Trash2Icon className="size-3.5" />
141 </Button>
142 ) : null}
143 <PopoverClose
144 render={<Button size="icon-xs" variant="ghost" />}
145 aria-label="Close composer"
146 >
147 <XIcon className="size-3.5" />
148 </PopoverClose>
149 </div>
150 </div>
151 {/* Keep both forms mounted across toggles and dismissal so drafts and in-flight
152 submit guards survive closing and reopening the composer. */}
153 {verdicts.length > 0 ? (
154 <div hidden={mode !== "review"}>
155 <PullRequestReviewForm
156 environmentId={environmentId}
157 reference={reference}
158 term={term}
159 verdicts={verdicts}
160 requestChangesSummaryRequired={sourceControlClients
161 .get(detail.provider)
162 .reviewSummaryRequired("request-changes")}
163 textareaRef={reviewRef}
164 pending={reviewPending}
165 onPendingChange={setReviewPending}
166 onSubmitted={() => {
167 setOpen(false);
168 onReviewSubmitted();
169 }}
170 />
171 </div>
172 ) : null}
173 {canComment ? (
174 <div hidden={mode !== "comment"}>
175 <PullRequestCommentForm
176 environmentId={environmentId}
177 reference={reference}
178 detail={detail}
179 actionPending={actionPending}
180 textareaRef={commentRef}
181 onCommentAction={onCommentAction}
182 onCommented={onCommented}
183 onClose={() => setOpen(false)}
184 />
185 </div>
186 ) : null}
187 </PopoverPopup>
188 </Popover>
189 );