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

PullRequestActionsMenu.tsx

386 lines14.0 KB

1import { useAtomValue } from "@effect/atom-react";
2import type { ChangeRequestTerminology } from "@t3tools/client-runtime/source-control-clients";
3import type {
4 EnvironmentId,
5 PullRequestAction,
6 PullRequestDetail,
7 PullRequestMergeMethod,
8 PullRequestRef,
9 ScopedThreadRef,
10} from "@t3tools/contracts";
11import {
12 ArrowUpRightIcon,
13 BookOpenIcon,
14 CopyIcon,
15 HammerIcon,
16 LinkIcon,
17 MessageCircleQuestionIcon,
18 MoreHorizontalIcon,
19 RotateCcwIcon,
20 SparklesIcon,
21 Trash2Icon,
22} from "lucide-react";
23​
24import { shortcutLabelForCommand } from "~/keybindings";
25import { readLocalApi } from "~/localApi";
26import { primaryServerKeybindingsAtom } from "~/state/server";
27import { Button } from "../ui/button";
28import {
29 Menu,
30 MenuItem,
31 MenuPopup,
32 MenuRadioGroup,
33 MenuRadioItem,
34 MenuSeparator,
35 MenuShortcut,
36 MenuTrigger,
37} from "../ui/menu";
38import { RefreshIcon } from "../ui/refresh-icon";
39import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
40import { ActOnEnvironmentPicker, type PerformPullRequestAction } from "./PullRequestDetailActions";
41import {
42 PULL_REQUEST_MERGE_METHOD_LABELS,
43 pullRequestActionMenuHasGroup,
44} from "./pullRequestDetail.logic";
45import { PullRequestGlyph } from "./pullRequestIcons";
46import type { PickableEnvironment } from "./pullRequestProjectAssignment.logic";
47import { PullRequestThreadLinks } from "./PullRequestThreadLinks";
48import { useInlineActionConfirm } from "./useInlineActionConfirm";
49​
50/** Everything else the reader can do with the pull request, behind the header's overflow button. */
51export function PullRequestActionsMenu({
52 environmentId,
53 reference,
54 pullRequestKey,
55 detail,
56 terminology,
57 linkThreadRef,
58 onThreadPickerOpenChange,
59 refreshing,
60 refreshFromHost,
61 handoff,
62 attachesToComposer,
63 askAboutPullRequest,
64 explainPullRequest,
65 canFixFindings,
66 startFixFindings,
67 handoffLabels,
68 canWriteGuide,
69 guideRunning,
70 guideWritten,
71 writeGuide,
72 pickableEnvironments,
73 actingEnvironmentId,
74 onActingEnvironmentChange,
75 can,
76 autoMergeArmed,
77 showsDraftToggle,
78 showsMergeNow,
79 showsAutoMerge,
80 showsMergeMethods,
81 allowedMergeMethods,
82 selectedMergeMethod,
83 onSelectMergeMethod,
84 showsDeleteBranch,
85 environmentConnected,
86 confirmDeleteBranch,
87 pendingAction,
88 perform,
89 openOnHostText,
90 copyReference,
91}: {
92 environmentId: EnvironmentId;
93 reference: PullRequestRef;
94 pullRequestKey: string;
95 detail: Pick<PullRequestDetail, "url" | "state" | "isDraft" | "headBranch">;
96 terminology: ChangeRequestTerminology;
97 linkThreadRef: ScopedThreadRef | null;
98 onThreadPickerOpenChange: (open: boolean) => void;
99 refreshing: boolean;
100 refreshFromHost: () => Promise<void>;
101 handoff: string | null;
102 attachesToComposer: boolean;
103 askAboutPullRequest: () => void;
104 explainPullRequest: () => void;
105 canFixFindings: boolean;
106 startFixFindings: () => void;
107 handoffLabels: { readonly fixFindings: string };
108 canWriteGuide: boolean;
109 guideRunning: boolean;
110 guideWritten: boolean;
111 writeGuide: () => void;
112 pickableEnvironments: ReadonlyArray<PickableEnvironment>;
113 actingEnvironmentId: EnvironmentId;
114 onActingEnvironmentChange: (environmentId: EnvironmentId) => void;
115 can: (action: PullRequestAction) => boolean;
116 autoMergeArmed: boolean;
117 showsDraftToggle: boolean;
118 showsMergeNow: boolean;
119 showsAutoMerge: boolean;
120 showsMergeMethods: boolean;
121 allowedMergeMethods: ReadonlyArray<PullRequestMergeMethod>;
122 selectedMergeMethod: PullRequestMergeMethod;
123 onSelectMergeMethod: (method: PullRequestMergeMethod) => void;
124 showsDeleteBranch: boolean;
125 environmentConnected: boolean;
126 confirmDeleteBranch: () => Promise<void>;
127 pendingAction: PullRequestAction | null;
128 perform: PerformPullRequestAction;
129 openOnHostText: string;
130 copyReference: (value: string, label: string) => void;
131}) {
132 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
133 const inlineConfirm = useInlineActionConfirm(pullRequestKey);
134 const actionPending = pendingAction !== null;
135 return (
136 <Menu>
137 <Tooltip>
138 <TooltipTrigger
139 render={
140 <MenuTrigger
141 render={
142 <Button
143 aria-label={
144 refreshing
145 ? `Refreshing ${terminology.singular}`
146 : `More ${terminology.singular} actions`
147 }
148 size="icon-xs"
149 variant="ghost-muted"
150 />
151 }
152 >
153 {refreshing ? (
154 <RefreshIcon refreshing size="md" />
155 ) : (
156 <MoreHorizontalIcon className="size-4" />
157 )}
158 </MenuTrigger>
159 }
160 />
161 <TooltipPopup>
162 {refreshing
163 ? `Refreshing ${terminology.singular}`
164 : `More ${terminology.singular} actions`}
165 </TooltipPopup>
166 </Tooltip>
167 <MenuPopup align="end" side="bottom">
168 <PullRequestThreadLinks
169 display="menu-item"
170 environmentId={environmentId}
171 reference={reference}
172 url={detail.url}
173 threadRef={linkThreadRef}
174 onPickerOpenChange={onThreadPickerOpenChange}
175 />
176 <MenuItem disabled={refreshing} onClick={() => void refreshFromHost()}>
177 <RefreshIcon size="sm" refreshing={refreshing} />
178 Refresh
179 </MenuItem>
180 <MenuItem disabled={handoff !== null} onClick={askAboutPullRequest}>
181 <MessageCircleQuestionIcon className="mt-1 size-3.5 shrink-0 self-start" />
182 <span className="flex min-w-0 flex-col">
183 <span>{handoff === "ask" ? "Opening..." : "Ask a question"}</span>
184 <span className="text-xs text-muted-foreground">
185 {attachesToComposer
186 ? `Adds the ${terminology.singular} to this thread's composer.`
187 : `Opens a thread that knows which ${terminology.singular} you mean.`}
188 </span>
189 </span>
190 </MenuItem>
191 <MenuItem disabled={handoff !== null} onClick={explainPullRequest}>
192 <BookOpenIcon className="mt-1 size-3.5 shrink-0 self-start" />
193 <span className="flex min-w-0 flex-col">
194 <span>
195 {handoff === "explain" ? "Opening..." : `Explain this ${terminology.shortLabel}`}
196 </span>
197 <span className="text-xs text-muted-foreground">
198 A walk through the diff and what to read closely.
199 </span>
200 </span>
201 </MenuItem>
202 <MenuItem disabled={handoff !== null || !canFixFindings} onClick={startFixFindings}>
203 <HammerIcon className="size-3.5" />
204 {handoff === "findings" ? "Preparing..." : handoffLabels.fixFindings}
205 </MenuItem>
206 {canWriteGuide ? (
207 <MenuItem disabled={guideRunning} onClick={writeGuide}>
208 <SparklesIcon className="size-3.5" />
209 {guideRunning ? "Writing guide..." : !guideWritten ? "Write guide" : "Rewrite guide"}
210 </MenuItem>
211 ) : null}
212 {pickableEnvironments.length > 0 ? (
213 <ActOnEnvironmentPicker
214 environments={pickableEnvironments}
215 value={actingEnvironmentId}
216 onChange={onActingEnvironmentChange}
217 disabled={handoff !== null}
218 />
219 ) : null}
220 <MenuSeparator />
221 {detail.state === "open" ? (
222 <>
223 {showsDraftToggle ? (
224 <MenuItem
225 disabled={actionPending}
226 onClick={() => void perform(detail.isDraft ? "ready" : "draft")}
227 >
228 {detail.isDraft ? (
229 <PullRequestGlyph.pullRequest className="size-3.5" />
230 ) : (
231 <PullRequestGlyph.draft className="size-3.5" />
232 )}
233 {detail.isDraft ? "Ready for review" : "Convert to draft"}
234 </MenuItem>
235 ) : null}
236 {showsMergeNow ? (
237 <MenuItem
238 closeOnClick={inlineConfirm.isArmed("merge")}
239 aria-label={inlineConfirm.isArmed("merge") ? "Confirm merge?" : "Merge now"}
240 disabled={actionPending}
241 {...inlineConfirm.props("merge", () => void perform("merge", selectedMergeMethod))}
242 >
243 <PullRequestGlyph.merged className="size-3.5" />
244 {inlineConfirm.isArmed("merge") ? "Confirm merge?" : "Merge now"}
245 </MenuItem>
246 ) : null}
247 {autoMergeArmed && can("disable-auto-merge") ? (
248 <MenuItem disabled={actionPending} onClick={() => void perform("disable-auto-merge")}>
249 <PullRequestGlyph.merged className="size-3.5" />
250 Disable auto-merge
251 </MenuItem>
252 ) : showsAutoMerge ? (
253 <MenuItem
254 closeOnClick={inlineConfirm.isArmed("enable-auto-merge")}
255 aria-label={
256 inlineConfirm.isArmed("enable-auto-merge")
257 ? "Confirm auto-merge?"
258 : "Enable auto-merge"
259 }
260 disabled={actionPending}
261 {...inlineConfirm.props(
262 "enable-auto-merge",
263 () => void perform("enable-auto-merge", selectedMergeMethod),
264 )}
265 >
266 <PullRequestGlyph.merged className="size-3.5" />
267 {inlineConfirm.isArmed("enable-auto-merge")
268 ? "Confirm auto-merge?"
269 : "Enable auto-merge"}
270 </MenuItem>
271 ) : null}
272 {showsMergeMethods ? (
273 <>
274 {showsDraftToggle || showsMergeNow || showsAutoMerge ? <MenuSeparator /> : null}
275 <MenuRadioGroup
276 value={selectedMergeMethod}
277 onValueChange={(method) => onSelectMergeMethod(method as PullRequestMergeMethod)}
278 >
279 {allowedMergeMethods.map((method) => (
280 <MenuRadioItem
281 key={method}
282 value={method}
283 disabled={actionPending}
284 closeOnClick
285 >
286 <span className="flex min-w-0 items-center gap-2">
287 <PullRequestGlyph.merged className="size-3.5" />
288 <span>{PULL_REQUEST_MERGE_METHOD_LABELS[method]}</span>
289 </span>
290 </MenuRadioItem>
291 ))}
292 </MenuRadioGroup>
293 </>
294 ) : null}
295 {pullRequestActionMenuHasGroup(
296 showsDraftToggle,
297 showsAutoMerge || showsMergeNow,
298 showsMergeMethods,
299 ) ? (
300 <MenuSeparator />
301 ) : null}
302 </>
303 ) : null}
304 <MenuItem onClick={() => void readLocalApi()?.shell.openExternal(detail.url)}>
305 <ArrowUpRightIcon className="size-3.5" />
306 {openOnHostText}
307 </MenuItem>
308 <MenuItem onClick={() => copyReference(detail.url, `${terminology.shortLabel} link`)}>
309 <LinkIcon className="size-3.5" />
310 Copy link
311 <MenuShortcut>
312 {shortcutLabelForCommand(keybindings, "thread.copyReference")}
313 </MenuShortcut>
314 </MenuItem>
315 <MenuItem
316 onClick={() => copyReference(`#${reference.number}`, `${terminology.shortLabel} number`)}
317 >
318 <CopyIcon className="size-3.5" />
319 Copy {terminology.shortLabel} number
320 <MenuShortcut>
321 {shortcutLabelForCommand(keybindings, "pullRequest.copyNumber")}
322 </MenuShortcut>
323 </MenuItem>
324 {detail.state === "open" && can("close") ? (
325 <>
326 <MenuSeparator />
327 <MenuItem
328 closeOnClick={inlineConfirm.isArmed("close")}
329 aria-label={
330 inlineConfirm.isArmed("close") ? "Confirm close?" : `Close ${terminology.singular}`
331 }
332 variant="destructive"
333 disabled={actionPending}
334 {...inlineConfirm.props("close", () => void perform("close"))}
335 >
336 <PullRequestGlyph.closed className="size-3.5" />
337 {inlineConfirm.isArmed("close") ? "Confirm close?" : `Close ${terminology.singular}`}
338 </MenuItem>
339 </>
340 ) : detail.state === "closed" && can("reopen") ? (
341 <>
342 <MenuSeparator />
343 <MenuItem disabled={actionPending} onClick={() => void perform("reopen")}>
344 <PullRequestGlyph.reopen className="size-3.5" />
345 Reopen {terminology.singular}
346 </MenuItem>
347 </>
348 ) : detail.state === "merged" && can("revert") ? (
349 <>
350 <MenuSeparator />
351 <MenuItem
352 closeOnClick={inlineConfirm.isArmed("revert")}
353 aria-label={inlineConfirm.isArmed("revert") ? "Confirm revert?" : "Revert changes"}
354 variant={inlineConfirm.isArmed("revert") ? "destructive" : "default"}
355 disabled={actionPending}
356 {...inlineConfirm.props("revert", () => void perform("revert"))}
357 >
358 <RotateCcwIcon className="size-3.5" />
359 {inlineConfirm.isArmed("revert") ? "Confirm revert?" : "Revert changes"}
360 </MenuItem>
361 </>
362 ) : null}
363 {showsDeleteBranch ? (
364 <>
365 <MenuSeparator />
366 <MenuItem
367 variant="destructive"
368 disabled={actionPending || !environmentConnected}
369 onClick={() => void confirmDeleteBranch()}
370 >
371 <Trash2Icon className="mt-1 size-3.5 shrink-0 self-start" />
372 <span className="flex min-w-0 flex-col">
373 <span>
374 {pendingAction === "delete-branch" ? "Deleting branch..." : "Delete branch"}
375 </span>
376 <span className="truncate text-xs text-muted-foreground">
377 {environmentConnected ? detail.headBranch : "Reconnect to delete this branch"}
378 </span>
379 </span>
380 </MenuItem>
381 </>
382 ) : null}
383 </MenuPopup>
384 </Menu>
385 );