Open History to see the last commit
1import type { PullRequestAction } from "@t3tools/contracts";
2import { useUiStateStore } from "~/uiStateStore";
3import { Button } from "../ui/button";
4import { Spinner } from "../ui/spinner";
5import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
6import { resolvePullRequestMergeMethod } from "./pullRequestDetail.logic";
7import { PullRequestGlyph } from "./pullRequestIcons";
8import type { EnvironmentPullRequestEntry } from "./pullRequestList.logic";
9import {
10 usePullRequestActionRunner,
11 usePullRequestDefaultMergeMethodResolver,
12} from "./usePullRequestActions";
13
14export interface PullRequestSpeedActionResult {
15 readonly entry: EnvironmentPullRequestEntry;
16 readonly action: PullRequestAction;
17}
18
19/** No detail or stack reads until a merge is clicked, even on a long list. */
20export function PullRequestSpeedActions({
21 entry,
22 visible,
23 onActed,
24 closing = false,
25 sweeping = false,
26 onCloseSweepStart,
27}: {
28 entry: EnvironmentPullRequestEntry;
29 visible: boolean;
30 onActed: (result: PullRequestSpeedActionResult) => void;
31 closing?: boolean;
32 sweeping?: boolean;
33 onCloseSweepStart?: (entry: EnvironmentPullRequestEntry, event: PointerEvent) => void;
34}) {
35 const resolveProjectDefault = usePullRequestDefaultMergeMethodResolver(
36 entry.environmentId,
37 entry.projectId,
38 );
39 const reference = {
40 projectId: entry.projectId,
41 host: entry.host,
42 repository: entry.repository,
43 number: entry.number,
44 };
45 const { actionPending, perform } = usePullRequestActionRunner({
46 environmentId: entry.environmentId,
47 reference,
48 onSuccess: (action) => onActed({ entry, action }),
49 resolveMergeMethod: (detail) => {
50 const allowed = detail.capabilities.mergeMethods.filter(
51 (method) => detail.mergeCapabilities[method],
52 );
53 if (allowed.length === 0)
54 throw new Error("No merge method is available for this repository.");
55 return resolvePullRequestMergeMethod(
56 allowed,
57 null,
58 resolveProjectDefault(),
59 useUiStateStore.getState().pullRequestMergeMethod,
60 );
61 },
62 });
63 const actions =
64 entry.state === "closed"
65 ? (["reopen"] as const)
66 : entry.isDraft
67 ? (["close", "ready"] as const)
68 : (["close", "merge"] as const);
69 const busy = actionPending || closing || sweeping;
70 return (
71 <div
72 className="shrink-0 items-center gap-1 pr-3"
73 style={{ display: visible || busy ? "flex" : "none" }}
74 role="group"
75 aria-label={`Quick actions for pull request #${entry.number}`}
76 data-pull-request-action-pending={actionPending || closing}
77 >
78 {actions.map((action) => {
79 const label = ACTIONS[action].label;
80 const Icon = ACTIONS[action].Icon;
81 return (
82 <Tooltip key={action}>
83 <TooltipTrigger
84 render={
85 <Button
86 variant={action === "close" ? "destructive-outline" : "outline"}
87 size="xs"
88 disabled={busy || (action === "merge" && entry.stack !== undefined)}
89 aria-label={`${label} #${entry.number}`}
90 onClick={() => void perform(action)}
91 onPointerDown={(event) => {
92 if (action !== "close" || !event.isPrimary || event.button !== 0) return;
93 event.stopPropagation();
94 onCloseSweepStart?.(entry, event.nativeEvent);
95 }}
96 />
97 }
98 >
99 {busy ? <Spinner size="xs" /> : <Icon aria-hidden className="size-3" />}
100 {label}
101 </TooltipTrigger>
102 <TooltipPopup>
103 {action === "merge" && entry.stack
104 ? "Open this pull request to merge its stack"
105 : action === "close"
106 ? "Close immediately, or drag across rows to close several"
107 : `${label} immediately`}
108 </TooltipPopup>
109 </Tooltip>
110 );
111 })}
112 </div>
113 );
114}
115
116const ACTIONS = {
117 close: { label: "Close", Icon: PullRequestGlyph.closed },
118 merge: { label: "Merge", Icon: PullRequestGlyph.merged },
119 ready: { label: "Ready for review", Icon: PullRequestGlyph.pullRequest },
120 reopen: { label: "Reopen", Icon: PullRequestGlyph.reopen },
121} as const;