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

PullRequestDetailActions.tsx

464 lines15.8 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 { Branch, 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={
111 handoff?.startsWith("checkout") ? gt("Checking out...") : gt("Check out")
112 }
113 >
114 <GitBranchIcon aria-hidden className="size-3.5" />
115 <span className="@max-[35rem]/pr-header:hidden">
116 {handoff?.startsWith("checkout") ? gt("Checking out...") : gt("Check out")}
117 </span>
118 <ChevronDownIcon aria-hidden className="size-3.5 text-muted-foreground" />
119 </Button>
120 }
121 />
122 }
123 label={
124 <>
125 <T>
126 Check out this{" "}
127 <Branch branch={term} mr="merge request" cr="change request">
128 pull request
129 </Branch>
130 </T>
131 </>
132 }
133 />
134 <MenuPopup align="end" side="bottom">
135 <MenuItem disabled={!canPrepareWorktree} onClick={() => onCheckout("worktree")}>
136 <GitBranchIcon className="mt-1 size-3.5 shrink-0 self-start" />
137 <span className="flex min-w-0 flex-col">
138 <span>
139 <T>In a separate worktree</T>
140 </span>
141 <span className="text-xs text-muted-foreground">
142 <T>Its own folder and thread. Nothing you have open moves.</T>
143 </span>
144 </span>
145 </MenuItem>
146 <MenuItem disabled={!canCheckoutHere} onClick={() => onCheckout("local")}>
147 <FolderGit2Icon className="mt-1 size-3.5 shrink-0 self-start" />
148 <span className="flex min-w-0 flex-col">
149 <span>
150 <T>In this repository</T>
151 </span>
152 <span className="text-xs text-muted-foreground">
153 <T>Switches the branch you are working in.</T>
154 </span>
155 </span>
156 </MenuItem>
157 {pickableEnvironments.length > 0 ? (
158 <ActOnEnvironmentPicker
159 environments={pickableEnvironments}
160 value={actingEnvironmentId}
161 onChange={onActingEnvironmentChange}
162 disabled={handoff !== null}
163 />
164 ) : null}
165 </MenuPopup>
166 </Menu>
167 );
169​
170export function PullRequestResolveConflictsButton({
171 handoff,
172 unavailable,
173 onResolve,
174}: {
175 handoff: string | null;
176 unavailable: boolean;
177 onResolve: () => void;
178}) {
179 const gt = useGT();
180 return (
181 <SharedTooltipTrigger
182 render={
183 <span className="inline-flex shrink-0">
184 <Button
185 size="xs"
186 variant="destructive-outline"
187 disabled={handoff !== null || unavailable}
188 onClick={onResolve}
189 aria-label={handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")}
190 >
191 <PullRequestGlyph.conflicting aria-hidden className="size-3.5" />
192 <span className="@max-[30rem]/pr-header:hidden">
193 {handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")}
194 </span>
195 </Button>
196 </span>
197 }
198 label={handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")}
199 />
200 );
202​
203/** The header's one prominent control, which depends on where the pull request stands. */
204export function PullRequestPrimaryAction({
205 pullRequestKey,
206 primaryAction,
207 mergeBlockedReason,
208 autoMergeArmed,
209 armedAutoMergeLabel,
210 pendingAutoMergeLabel,
211 selectedMergeMethod,
212 selectedMergeMethodLabel,
213 statePresentation,
214 pendingAction,
215 perform,
216 resolveConflicts,
217}: {
218 pullRequestKey: string;
219 primaryAction: ReturnType<typeof resolvePullRequestPrimaryControl> | null;
220 mergeBlockedReason: string | null;
221 autoMergeArmed: boolean;
222 armedAutoMergeLabel: string;
223 pendingAutoMergeLabel: string;
224 selectedMergeMethod: PullRequestMergeMethod;
225 selectedMergeMethodLabel: string;
226 statePresentation: ReturnType<typeof resolvePullRequestState> | null;
227 pendingAction: PullRequestAction | null;
228 perform: PerformPullRequestAction;
229 resolveConflicts: ReactNode;
230}) {
231 const gt = useGT();
232 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
233 const actionPending = pendingAction !== null;
234 return (
235 <>
236 {autoMergeArmed && primaryAction !== "auto-merge-armed" ? (
237 <SharedTooltipTrigger
238 render={
239 <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}>
240 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
241 <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span>
242 </Badge>
243 }
244 label={gt("{label}: the host will merge this on its own once its requirements are met", {
245 label: armedAutoMergeLabel,
246 })}
247 />
248 ) : null}
249 {primaryAction === "resolve" ? (
250 resolveConflicts
251 ) : primaryAction === "ready" ? (
252 <SharedTooltipTrigger
253 render={
254 <span className="inline-flex shrink-0">
255 <Button
256 size="xs"
257 variant="default"
258 disabled={actionPending}
259 onClick={() => void perform("ready")}
260 aria-label={gt("Ready for review")}
261 >
262 <PullRequestGlyph.pullRequest aria-hidden className="size-3.5" />
263 <span className="@max-[30rem]/pr-header:hidden">
264 <T>Ready for review</T>
265 </span>
266 </Button>
267 </span>
268 }
269 label={
270 <>
271 <T>Ready for review</T>
272 </>
273 }
274 />
275 ) : primaryAction === "enable-auto-merge" ? (
276 <SharedTooltipTrigger
277 render={
278 <span className="inline-flex shrink-0">
279 <Button
280 size="xs"
281 variant="default"
282 disabled={actionPending}
283 aria-disabled={mergeBlockedReason !== null || undefined}
284 aria-description={mergeBlockedReason ?? undefined}
285 {...(mergeBlockedReason === null
286 ? inlineConfirm.props(
287 "enable-auto-merge",
288 () => void perform("enable-auto-merge", selectedMergeMethod),
289 )
290 : {})}
291 aria-label={
292 pendingAction === "enable-auto-merge"
293 ? gt("Enabling...")
294 : inlineConfirm.isArmed("enable-auto-merge")
295 ? gt("Confirm auto-merge?")
296 : pendingAutoMergeLabel
297 }
298 >
299 {inlineConfirm.isArmed("enable-auto-merge") ? (
300 <CheckIcon aria-hidden className="size-3.5" />
301 ) : (
302 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
303 )}
304 <AnimatedWidth className="@max-[30rem]/pr-header:hidden">
305 <span className="grid">
306 <span aria-hidden className="invisible col-start-1 row-start-1">
307 {pendingAutoMergeLabel}
308 </span>
309 <span className="col-start-1 row-start-1">
310 {pendingAction === "enable-auto-merge"
311 ? gt("Enabling...")
312 : inlineConfirm.isArmed("enable-auto-merge")
313 ? gt("Confirm auto-merge?")
314 : pendingAutoMergeLabel}
315 </span>
316 </span>
317 </AnimatedWidth>
318 </Button>
319 </span>
320 }
321 label={
322 mergeBlockedReason ??
323 (pendingAction === "enable-auto-merge"
324 ? gt("Enabling...")
325 : inlineConfirm.isArmed("enable-auto-merge")
326 ? gt("Confirm auto-merge?")
327 : pendingAutoMergeLabel)
328 }
329 />
330 ) : primaryAction === "auto-merge-armed" ? (
331 <SharedTooltipTrigger
332 render={
333 <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}>
334 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
335 <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span>
336 </Badge>
337 }
338 label={gt("{label}: the host will merge this on its own once its requirements are met", {
339 label: armedAutoMergeLabel,
340 })}
341 />
342 ) : primaryAction === "merge" ? (
343 <SharedTooltipTrigger
344 render={
345 <span className="inline-flex shrink-0">
346 <Button
347 size="xs"
348 variant="default"
349 disabled={actionPending}
350 aria-disabled={mergeBlockedReason !== null || undefined}
351 aria-description={mergeBlockedReason ?? undefined}
352 {...(mergeBlockedReason === null
353 ? inlineConfirm.props("merge", () => void perform("merge", selectedMergeMethod))
354 : {})}
355 aria-label={
356 pendingAction === "merge"
357 ? gt("Merging...")
358 : inlineConfirm.isArmed("merge")
359 ? gt("Confirm merge?")
360 : selectedMergeMethodLabel
361 }
362 >
363 {inlineConfirm.isArmed("merge") ? (
364 <CheckIcon aria-hidden className="size-3.5" />
365 ) : (
366 <PullRequestGlyph.merged aria-hidden className="size-3.5" />
367 )}
368 <AnimatedWidth className="@max-[30rem]/pr-header:hidden">
369 <span className="grid">
370 <span aria-hidden className="invisible col-start-1 row-start-1">
371 {selectedMergeMethodLabel}
372 </span>
373 <span className="col-start-1 row-start-1">
374 {pendingAction === "merge"
375 ? gt("Merging...")
376 : inlineConfirm.isArmed("merge")
377 ? gt("Confirm merge?")
378 : selectedMergeMethodLabel}
379 </span>
380 </span>
381 </AnimatedWidth>
382 </Button>
383 </span>
384 }
385 label={
386 mergeBlockedReason ??
387 (pendingAction === "merge"
388 ? gt("Merging...")
389 : inlineConfirm.isArmed("merge")
390 ? gt("Confirm merge?")
391 : selectedMergeMethodLabel)
392 }
393 />
394 ) : (primaryAction === "merged" || primaryAction === "closed") &&
395 statePresentation !== null ? (
396 <Badge size="control" variant="outline">
397 <span className={cn("flex items-center gap-1", statePresentation.toneClassName)}>
398 <statePresentation.Icon className="size-3.5" />
399 {statePresentation.label}
400 </span>
401 </Badge>
402 ) : null}
403 </>
404 );
406​
407export function PullRequestApproveWorkflowsButton({
408 pullRequestKey,
409 pendingAction,
410 perform,
411}: {
412 pullRequestKey: string;
413 pendingAction: PullRequestAction | null;
414 perform: PerformPullRequestAction;
415}) {
416 const gt = useGT();
417 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
418 const actionPending = pendingAction !== null;
419 return (
420 <SharedTooltipTrigger
421 render={
422 <span className="ml-auto inline-flex shrink-0">
423 <Button
424 size="xs"
425 variant="warning-outline"
426 disabled={actionPending}
427 {...inlineConfirm.props("approve-workflows", () => void perform("approve-workflows"))}
428 aria-label={
429 pendingAction === "approve-workflows"
430 ? gt("Approving...")
431 : inlineConfirm.isArmed("approve-workflows")
432 ? gt("Confirm approve?")
433 : gt("Approve workflows to run")
434 }
435 >
436 <PlayIcon aria-hidden className="size-3.5" />
437 <span className="inline-grid">
438 <span aria-hidden className="invisible col-start-1 row-start-1">
439 <T>Approve workflows to run</T>
440 </span>
441 <span aria-hidden className="invisible col-start-1 row-start-1">
442 <T>Confirm approve?</T>
443 </span>
444 <span className="col-start-1 row-start-1">
445 {pendingAction === "approve-workflows"
446 ? gt("Approving...")
447 : inlineConfirm.isArmed("approve-workflows")
448 ? gt("Confirm approve?")
449 : gt("Approve workflows to run")}
450 </span>
451 </span>
452 </Button>
453 </span>
454 }
455 label={
456 pendingAction === "approve-workflows"
457 ? gt("Approving...")
458 : inlineConfirm.isArmed("approve-workflows")
459 ? gt("Confirm approve?")
460 : gt("Approve workflows to run")
461 }
462 />
463 );