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