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

PullRequestDetailActions.tsx

455 lines16.0 KB

1import type {
2 EnvironmentId,
3 PullRequestAction,
4 PullRequestMergeMethod,
5 PullRequestUpdateMethod,
6} from "@t3tools/contracts";
7import { CheckIcon, ChevronDownIcon, FolderGit2Icon, GitBranchIcon, PlayIcon } from "lucide-react";
8import type { ReactNode } from "react";
9​
10import { AnimatedWidth } from "~/components/AnimatedWidth";
11import { cn } from "~/lib/utils";
12import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
13import { Badge } from "../ui/badge";
14import { Button } from "../ui/button";
15import {
16 Menu,
17 MenuItem,
18 MenuPopup,
19 MenuRadioGroup,
20 MenuRadioItem,
21 MenuSeparator,
22 MenuTrigger,
23} from "../ui/menu";
24import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
25import type { resolvePullRequestPrimaryControl } from "./pullRequestDetail.logic";
26import { PullRequestGlyph } from "./pullRequestIcons";
27import type { resolvePullRequestState } from "./pullRequestPresentation";
28import type { PickableEnvironment } from "./pullRequestProjectAssignment.logic";
29import { useInlineActionConfirm } from "./useInlineActionConfirm";
30​
31export type PerformPullRequestAction = (
32 action: PullRequestAction,
33 method?: PullRequestMergeMethod,
34 updateMethod?: PullRequestUpdateMethod,
35) => Promise<boolean>;
36​
37export function ActOnEnvironmentPicker({
38 environments,
39 value,
40 onChange,
41 disabled,
42}: {
43 environments: ReadonlyArray<PickableEnvironment>;
44 value: EnvironmentId;
45 onChange: (environmentId: EnvironmentId) => void;
46 disabled: boolean;
47}) {
48 return (
49 <>
50 <MenuSeparator />
51 <MenuRadioGroup
52 value={value}
53 onValueChange={(environmentId) => onChange(environmentId as EnvironmentId)}
54 >
55 {environments.map((environment) => (
56 <MenuRadioItem
57 key={environment.environmentId}
58 value={environment.environmentId}
59 disabled={disabled}
60 >
61 <span className="flex min-w-0 items-center gap-2">
62 <EnvironmentMachineIcon
63 kind={environment.machine ?? "server"}
64 className="size-3.5 shrink-0"
65 />
66 <span className="truncate">{environment.label}</span>
67 </span>
68 </MenuRadioItem>
69 ))}
70 </MenuRadioGroup>
71 </>
72 );
74​
75export function PullRequestCheckoutMenu({
76 handoff,
77 checkoutRoot,
78 canPrepareWorktree,
79 canCheckoutHere,
80 onCheckout,
81 pickableEnvironments,
82 actingEnvironmentId,
83 onActingEnvironmentChange,
84 term,
85}: {
86 handoff: string | null;
87 checkoutRoot: string | null;
88 canPrepareWorktree: boolean;
89 canCheckoutHere: boolean;
90 onCheckout: (mode: "worktree" | "local") => void;
91 pickableEnvironments: ReadonlyArray<PickableEnvironment>;
92 actingEnvironmentId: EnvironmentId;
93 onActingEnvironmentChange: (environmentId: EnvironmentId) => void;
94 /** The host's word for a change request, as in "pull request". */
95 term: string;
96}) {
97 return (
98 <Menu>
99 <Tooltip>
100 <TooltipTrigger
101 render={
102 <MenuTrigger
103 disabled={handoff !== null || checkoutRoot === null}
104 render={
105 <Button
106 size="xs"
107 variant="outline"
108 aria-label={handoff?.startsWith("checkout") ? "Checking out..." : "Check out"}
109 >
110 <GitBranchIcon aria-hidden className="size-3.5" />
111 <span className="@max-[35rem]/pr-header:hidden">
112 {handoff?.startsWith("checkout") ? "Checking out..." : "Check out"}
113 </span>
114 <ChevronDownIcon aria-hidden className="size-3.5 text-muted-foreground" />
115 </Button>
116 }
117 />
118 }
119 />
120 <TooltipPopup>Check out this {term}</TooltipPopup>
121 </Tooltip>
122 <MenuPopup align="end" side="bottom">
123 <MenuItem disabled={!canPrepareWorktree} onClick={() => onCheckout("worktree")}>
124 <GitBranchIcon className="mt-1 size-3.5 shrink-0 self-start" />
125 <span className="flex min-w-0 flex-col">
126 <span>In a separate worktree</span>
127 <span className="text-xs text-muted-foreground">
128 Its own folder and thread. Nothing you have open moves.
129 </span>
130 </span>
131 </MenuItem>
132 <MenuItem disabled={!canCheckoutHere} onClick={() => onCheckout("local")}>
133 <FolderGit2Icon className="mt-1 size-3.5 shrink-0 self-start" />
134 <span className="flex min-w-0 flex-col">
135 <span>In this repository</span>
136 <span className="text-xs text-muted-foreground">
137 Switches the branch you are working in.
138 </span>
139 </span>
140 </MenuItem>
141 {pickableEnvironments.length > 0 ? (
142 <ActOnEnvironmentPicker
143 environments={pickableEnvironments}
144 value={actingEnvironmentId}
145 onChange={onActingEnvironmentChange}
146 disabled={handoff !== null}
147 />
148 ) : null}
149 </MenuPopup>
150 </Menu>
151 );
153​
154export function PullRequestResolveConflictsButton({
155 handoff,
156 unavailable,
157 onResolve,
158}: {
159 handoff: string | null;
160 unavailable: boolean;
161 onResolve: () => void;
162}) {
163 return (
164 <Tooltip>
165 <TooltipTrigger
166 render={
167 <span className="inline-flex shrink-0">
168 <Button
169 size="xs"
170 variant="destructive-outline"
171 disabled={handoff !== null || unavailable}
172 onClick={onResolve}
173 aria-label={handoff === "conflicts" ? "Preparing..." : "Resolve conflicts"}
174 >
175 <PullRequestGlyph.conflicting aria-hidden className="size-3.5" />
176 <span className="@max-[30rem]/pr-header:hidden">
177 {handoff === "conflicts" ? "Preparing..." : "Resolve conflicts"}
178 </span>
179 </Button>
180 </span>
181 }
182 />
183 <TooltipPopup side="top">
184 {handoff === "conflicts" ? "Preparing..." : "Resolve conflicts"}
185 </TooltipPopup>
186 </Tooltip>
187 );
189​
190/** The header's one prominent control, which depends on where the pull request stands. */
191export function PullRequestPrimaryAction({
192 pullRequestKey,
193 primaryAction,
194 mergeBlockedReason,
195 autoMergeArmed,
196 armedAutoMergeLabel,
197 pendingAutoMergeLabel,
198 selectedMergeMethod,
199 selectedMergeMethodLabel,
200 statePresentation,
201 pendingAction,
202 perform,
203 resolveConflicts,
204}: {
205 pullRequestKey: string;
206 primaryAction: ReturnType<typeof resolvePullRequestPrimaryControl> | null;
207 mergeBlockedReason: string | null;
208 autoMergeArmed: boolean;
209 armedAutoMergeLabel: string;
210 pendingAutoMergeLabel: string;
211 selectedMergeMethod: PullRequestMergeMethod;
212 selectedMergeMethodLabel: string;
213 statePresentation: ReturnType<typeof resolvePullRequestState> | null;
214 pendingAction: PullRequestAction | null;
215 perform: PerformPullRequestAction;
216 resolveConflicts: ReactNode;
217}) {
218 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
219 const actionPending = pendingAction !== null;
220 return (
221 <>
222 {autoMergeArmed && primaryAction !== "auto-merge-armed" ? (
223 <Tooltip>
224 <TooltipTrigger
225 render={
226 <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}>
227 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
228 <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span>
229 </Badge>
230 }
231 />
232 <TooltipPopup side="top">
233 {armedAutoMergeLabel}: the host will merge this on its own once its requirements are met
234 </TooltipPopup>
235 </Tooltip>
236 ) : null}
237 {primaryAction === "resolve" ? (
238 resolveConflicts
239 ) : primaryAction === "ready" ? (
240 <Tooltip>
241 <TooltipTrigger
242 render={
243 <span className="inline-flex shrink-0">
244 <Button
245 size="xs"
246 variant="default"
247 disabled={actionPending}
248 onClick={() => void perform("ready")}
249 aria-label="Ready for review"
250 >
251 <PullRequestGlyph.pullRequest aria-hidden className="size-3.5" />
252 <span className="@max-[30rem]/pr-header:hidden">Ready for review</span>
253 </Button>
254 </span>
255 }
256 />
257 <TooltipPopup side="top">Ready for review</TooltipPopup>
258 </Tooltip>
259 ) : primaryAction === "enable-auto-merge" ? (
260 <Tooltip>
261 <TooltipTrigger
262 render={
263 <span className="inline-flex shrink-0">
264 <Button
265 size="xs"
266 variant="default"
267 disabled={actionPending}
268 aria-disabled={mergeBlockedReason !== null || undefined}
269 aria-description={mergeBlockedReason ?? undefined}
270 {...(mergeBlockedReason === null
271 ? inlineConfirm.props(
272 "enable-auto-merge",
273 () => void perform("enable-auto-merge", selectedMergeMethod),
274 )
275 : {})}
276 aria-label={
277 pendingAction === "enable-auto-merge"
278 ? "Enabling..."
279 : inlineConfirm.isArmed("enable-auto-merge")
280 ? "Confirm auto-merge?"
281 : pendingAutoMergeLabel
282 }
283 >
284 {inlineConfirm.isArmed("enable-auto-merge") ? (
285 <CheckIcon aria-hidden className="size-3.5" />
286 ) : (
287 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
288 )}
289 <AnimatedWidth className="@max-[30rem]/pr-header:hidden">
290 <span className="grid">
291 <span aria-hidden className="invisible col-start-1 row-start-1">
292 {pendingAutoMergeLabel}
293 </span>
294 <span className="col-start-1 row-start-1">
295 {pendingAction === "enable-auto-merge"
296 ? "Enabling..."
297 : inlineConfirm.isArmed("enable-auto-merge")
298 ? "Confirm auto-merge?"
299 : pendingAutoMergeLabel}
300 </span>
301 </span>
302 </AnimatedWidth>
303 </Button>
304 </span>
305 }
306 />
307 <TooltipPopup side="top">
308 {mergeBlockedReason ??
309 (pendingAction === "enable-auto-merge"
310 ? "Enabling..."
311 : inlineConfirm.isArmed("enable-auto-merge")
312 ? "Confirm auto-merge?"
313 : pendingAutoMergeLabel)}
314 </TooltipPopup>
315 </Tooltip>
316 ) : primaryAction === "auto-merge-armed" ? (
317 <Tooltip>
318 <TooltipTrigger
319 render={
320 <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}>
321 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
322 <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span>
323 </Badge>
324 }
325 />
326 <TooltipPopup side="top">
327 {armedAutoMergeLabel}: the host will merge this on its own once its requirements are met
328 </TooltipPopup>
329 </Tooltip>
330 ) : primaryAction === "merge" ? (
331 <Tooltip>
332 <TooltipTrigger
333 render={
334 <span className="inline-flex shrink-0">
335 <Button
336 size="xs"
337 variant="default"
338 disabled={actionPending}
339 aria-disabled={mergeBlockedReason !== null || undefined}
340 aria-description={mergeBlockedReason ?? undefined}
341 {...(mergeBlockedReason === null
342 ? inlineConfirm.props("merge", () => void perform("merge", selectedMergeMethod))
343 : {})}
344 aria-label={
345 pendingAction === "merge"
346 ? "Merging..."
347 : inlineConfirm.isArmed("merge")
348 ? "Confirm merge?"
349 : selectedMergeMethodLabel
350 }
351 >
352 {inlineConfirm.isArmed("merge") ? (
353 <CheckIcon aria-hidden className="size-3.5" />
354 ) : (
355 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
356 )}
357 <AnimatedWidth className="@max-[30rem]/pr-header:hidden">
358 <span className="grid">
359 <span aria-hidden className="invisible col-start-1 row-start-1">
360 {selectedMergeMethodLabel}
361 </span>
362 <span className="col-start-1 row-start-1">
363 {pendingAction === "merge"
364 ? "Merging..."
365 : inlineConfirm.isArmed("merge")
366 ? "Confirm merge?"
367 : selectedMergeMethodLabel}
368 </span>
369 </span>
370 </AnimatedWidth>
371 </Button>
372 </span>
373 }
374 />
375 <TooltipPopup side="top">
376 {mergeBlockedReason ??
377 (pendingAction === "merge"
378 ? "Merging..."
379 : inlineConfirm.isArmed("merge")
380 ? "Confirm merge?"
381 : selectedMergeMethodLabel)}
382 </TooltipPopup>
383 </Tooltip>
384 ) : (primaryAction === "merged" || primaryAction === "closed") &&
385 statePresentation !== null ? (
386 <Badge size="control" variant="outline">
387 <span className={cn("flex items-center gap-1", statePresentation.toneClassName)}>
388 <statePresentation.Icon className="size-3.5" />
389 {statePresentation.label}
390 </span>
391 </Badge>
392 ) : null}
393 </>
394 );
396​
397export function PullRequestApproveWorkflowsButton({
398 pullRequestKey,
399 pendingAction,
400 perform,
401}: {
402 pullRequestKey: string;
403 pendingAction: PullRequestAction | null;
404 perform: PerformPullRequestAction;
405}) {
406 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
407 const actionPending = pendingAction !== null;
408 return (
409 <Tooltip>
410 <TooltipTrigger
411 render={
412 <span className="ml-auto inline-flex shrink-0">
413 <Button
414 size="xs"
415 variant="warning-outline"
416 disabled={actionPending}
417 {...inlineConfirm.props("approve-workflows", () => void perform("approve-workflows"))}
418 aria-label={
419 pendingAction === "approve-workflows"
420 ? "Approving..."
421 : inlineConfirm.isArmed("approve-workflows")
422 ? "Confirm approve?"
423 : "Approve workflows to run"
424 }
425 >
426 <PlayIcon aria-hidden className="size-3.5" />
427 <span className="inline-grid">
428 <span aria-hidden className="invisible col-start-1 row-start-1">
429 Approve workflows to run
430 </span>
431 <span aria-hidden className="invisible col-start-1 row-start-1">
432 Confirm approve?
433 </span>
434 <span className="col-start-1 row-start-1">
435 {pendingAction === "approve-workflows"
436 ? "Approving..."
437 : inlineConfirm.isArmed("approve-workflows")
438 ? "Confirm approve?"
439 : "Approve workflows to run"}
440 </span>
441 </span>
442 </Button>
443 </span>
444 }
445 />
446 <TooltipPopup side="top">
447 {pendingAction === "approve-workflows"
448 ? "Approving..."
449 : inlineConfirm.isArmed("approve-workflows")
450 ? "Confirm approve?"
451 : "Approve workflows to run"}
452 </TooltipPopup>
453 </Tooltip>
454 );