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