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

PullRequestSpeedActions.tsx

166 lines5.7 KB

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