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

PullRequestSpeedActions.tsx

131 lines4.7 KB

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 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;
25​
26/** No detail or stack reads until a merge is clicked, even on a long list. */
27export function PullRequestSpeedActions<Entry extends PullRequestSpeedActionEntry>({
28 entry,
29 visible,
30 onActed,
31 closing = false,
32 sweeping = false,
33 onCloseSweepStart,
34}: {
35 entry: Entry;
36 visible: boolean;
37 onActed?: (result: PullRequestSpeedActionResult<Entry>) => void;
38 closing?: boolean;
39 sweeping?: boolean;
40 onCloseSweepStart?: (entry: Entry, event: PointerEvent) => void;
41}) {
42 const canWrite = useAtomValue(
43 pullRequestEnvironment.runAction.permissionAtom(entry.environmentId),
44 );
45 const resolveProjectDefault = usePullRequestDefaultMergeMethodResolver(
46 entry.environmentId,
47 entry.projectId,
48 );
49 const reference = {
50 projectId: entry.projectId,
51 host: entry.host,
52 repository: entry.repository,
53 number: entry.number,
54 };
55 const { actionPending, perform } = usePullRequestActionRunner({
56 environmentId: entry.environmentId,
57 reference,
58 onSuccess: (action) => onActed?.({ entry, action }),
59 resolveMergeMethod: (detail) => {
60 const allowed = detail.capabilities.mergeMethods.filter(
61 (method) => detail.mergeCapabilities[method],
62 );
63 if (allowed.length === 0)
64 throw new Error("No merge method is available for this repository.");
65 return resolvePullRequestMergeMethod(
66 allowed,
67 null,
68 resolveProjectDefault(),
69 useUiStateStore.getState().pullRequestMergeMethod,
70 );
71 },
72 });
73 const actions =
74 entry.state === "closed"
75 ? (["reopen"] as const)
76 : entry.isDraft
77 ? (["close", "ready"] as const)
78 : (["close", "merge"] as const);
79 const busy = actionPending || closing || sweeping;
80 return (
81 <div
82 className="shrink-0 items-center gap-1 pr-3"
83 style={{ display: visible || busy ? "flex" : "none" }}
84 role="group"
85 aria-label={`Quick actions for pull request #${entry.number}`}
86 data-pull-request-action-pending={actionPending || closing}
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 pull request 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 </div>
123 );
125​
126const ACTIONS = {
127 close: { label: "Close", Icon: PullRequestGlyph.closed },
128 merge: { label: "Merge", Icon: PullRequestGlyph.merged },
129 ready: { label: "Ready for review", Icon: PullRequestGlyph.pullRequest },
130 reopen: { label: "Reopen", Icon: PullRequestGlyph.reopen },
131} as const;