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