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

PullRequestComposer.tsx

185 lines6.9 KB

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