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

PullRequestDetailActions.tsx

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