Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { PullRequestAction, PullRequestSummary } from "@t3tools/contracts";
3import { createContext, use, type ReactNode } from "react";
4import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
5
6import { useShortcutModifierState } from "~/shortcutModifierState";
7import { pullRequestEnvironment } from "~/state/pullRequests";
8import { useUiStateStore } from "~/uiStateStore";
9import { Button } from "../ui/button";
10import { Spinner } from "../ui/spinner";
11import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
12import { resolvePullRequestMergeMethod } from "./pullRequestDetail.logic";
13import { PullRequestGlyph } from "./pullRequestIcons";
14import { pullRequestQuickActions, type EnvironmentPullRequestEntry } from "./pullRequestList.logic";
15import {
16 usePullRequestActionRunner,
17 usePullRequestDefaultMergeMethodResolver,
18} from "./usePullRequestActions";
19
20type PullRequestSpeedActionEntry = Pick<
21 EnvironmentPullRequestEntry,
22 "environmentId" | "projectId" | "host" | "repository" | "number" | "state" | "isDraft"
23> & { readonly stack?: object | undefined };
24
25export interface PullRequestSpeedActionResult<Entry = EnvironmentPullRequestEntry> {
26 readonly entry: Entry;
27 readonly action: PullRequestAction;
28 readonly reply?: PullRequestSummary | undefined;
29}
30
31export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntry>({
32 entry,
33 hostActions,
34 terminology,
35 onActed,
36 closing = false,
37 sweeping = false,
38 onCloseSweepStart,
39}: {
40 entry: Entry;
41 hostActions: ReadonlySet<PullRequestAction>;
42 terminology: ChangeRequestTerminology;
43 onActed?: (result: PullRequestSpeedActionResult<Entry>) => void;
44 closing?: boolean;
45 sweeping?: boolean;
46 onCloseSweepStart?: (entry: Entry, event: PointerEvent) => void;
47}) {
48 const canWrite = useAtomValue(
49 pullRequestEnvironment.runAction.permissionAtom(entry.environmentId),
50 );
51 const resolveProjectDefault = usePullRequestDefaultMergeMethodResolver(
52 entry.environmentId,
53 entry.projectId,
54 );
55 const reference = {
56 projectId: entry.projectId,
57 host: entry.host,
58 repository: entry.repository,
59 number: entry.number,
60 };
61 const { actionPending, perform } = usePullRequestActionRunner({
62 environmentId: entry.environmentId,
63 reference,
64 terminology,
65 onSuccess: (action, reply) => onActed?.({ entry, action, reply }),
66 resolveMergeMethod: (detail) => {
67 const allowed = detail.capabilities.mergeMethods.filter(
68 (method) => detail.mergeCapabilities[method],
69 );
70 if (allowed.length === 0)
71 throw new Error("No merge method is available for this repository.");
72 return resolvePullRequestMergeMethod(
73 allowed,
74 null,
75 resolveProjectDefault(),
76 useUiStateStore.getState().pullRequestMergeMethod,
77 );
78 },
79 });
80 const actions = pullRequestQuickActions(entry, hostActions);
81 const busy = actionPending || closing || sweeping;
82 if (actions.length === 0) return null;
83 return (
84 <SpeedModeGroup
85 busy={busy}
86 pending={actionPending || closing}
87 label={`Quick actions for ${terminology.singular} #${entry.number}`}
88 >
89 {actions.map((action) => {
90 const label = ACTIONS[action].label;
91 const Icon = ACTIONS[action].Icon;
92 return (
93 <Tooltip key={action}>
94 <TooltipTrigger
95 render={
96 <Button
97 variant={action === "close" ? "destructive-outline" : "outline"}
98 size="xs"
99 disabled={!canWrite || busy || (action === "merge" && entry.stack !== undefined)}
100 aria-label={`${label} #${entry.number}`}
101 onClick={() => void perform(action)}
102 onPointerDown={(event) => {
103 if (action !== "close" || !event.isPrimary || event.button !== 0) return;
104 event.stopPropagation();
105 onCloseSweepStart?.(entry, event.nativeEvent);
106 }}
107 />
108 }
109 >
110 {busy ? <Spinner size="xs" /> : <Icon aria-hidden className="size-3" />}
111 {label}
112 </TooltipTrigger>
113 <TooltipPopup>
114 {action === "merge" && entry.stack
115 ? `Open this ${terminology.singular} to merge its stack`
116 : action === "close" && onCloseSweepStart
117 ? "Close immediately, or drag across rows to close several"
118 : `${label} immediately`}
119 </TooltipPopup>
120 </Tooltip>
121 );
122 })}
123 </SpeedModeGroup>
124 );
125}
126
127/**
128 * Whether Shift is held, as context rather than a row prop: a Shift press then re-renders each
129 * row's action group, not the rows or their buttons, in one commit.
130 */
131export const PullRequestSpeedModeContext = createContext(false);
132
133/** Tracks Shift for the quick actions below it. */
134export function PullRequestSpeedModeProvider({ children }: { children: ReactNode }) {
135 const modifiers = useShortcutModifierState(true);
136 const speedMode =
137 modifiers.shiftKey && !modifiers.metaKey && !modifiers.ctrlKey && !modifiers.altKey;
138 return <PullRequestSpeedModeContext value={speedMode}>{children}</PullRequestSpeedModeContext>;
139}
140
141function SpeedModeGroup({
142 busy,
143 pending,
144 label,
145 children,
146}: {
147 busy: boolean;
148 pending: boolean;
149 label: string;
150 children: ReactNode;
151}) {
152 const speedMode = use(PullRequestSpeedModeContext);
153 return (
154 <div
155 className="shrink-0 items-center gap-1 pr-3"
156 style={{ display: speedMode || busy ? "flex" : "none" }}
157 role="group"
158 aria-label={label}
159 data-pull-request-action-pending={pending}
160 >
161 {children}
162 </div>
163 );
164}
165
166const ACTIONS = {
167 close: { label: "Close", Icon: PullRequestGlyph.closed },
168 merge: { label: "Merge", Icon: PullRequestGlyph.merged },
169 ready: { label: "Ready for review", Icon: PullRequestGlyph.pullRequest },
170 reopen: { label: "Reopen", Icon: PullRequestGlyph.reopen },
171} as const;