The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | EnvironmentId, |
| 3 | PullRequestAction, |
| 4 | PullRequestMergeMethod, |
| 5 | PullRequestUpdateMethod, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { CheckIcon, ChevronDownIcon, FolderGit2Icon, GitBranchIcon, PlayIcon } from "lucide-react"; |
| 8 | import { Branch, T, useGT } from "gt-react"; |
| 9 | import type { ReactNode } from "react"; |
| 10 | |
| 11 | import { AnimatedWidth } from "~/components/AnimatedWidth"; |
| 12 | import { cn } from "~/lib/utils"; |
| 13 | import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon"; |
| 14 | import { Badge } from "../ui/badge"; |
| 15 | import { Button } from "../ui/button"; |
| 16 | import { |
| 17 | Menu, |
| 18 | MenuItem, |
| 19 | MenuPopup, |
| 20 | MenuRadioGroup, |
| 21 | MenuRadioItem, |
| 22 | MenuSeparator, |
| 23 | MenuTrigger, |
| 24 | } from "../ui/menu"; |
| 25 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 26 | import type { resolvePullRequestPrimaryControl } from "./pullRequestDetail.logic"; |
| 27 | import { PullRequestGlyph } from "./pullRequestIcons"; |
| 28 | import type { resolvePullRequestState } from "./pullRequestPresentation"; |
| 29 | import type { PickableEnvironment } from "./pullRequestProjectAssignment.logic"; |
| 30 | import { useInlineActionConfirm } from "./useInlineActionConfirm"; |
| 31 | |
| 32 | import type { ChangeRequestTermKind } from "./changeRequestTerm"; |
| 33 | |
| 34 | export type PerformPullRequestAction = ( |
| 35 | action: PullRequestAction, |
| 36 | method?: PullRequestMergeMethod, |
| 37 | updateMethod?: PullRequestUpdateMethod, |
| 38 | ) => Promise<boolean>; |
| 39 | |
| 40 | export function ActOnEnvironmentPicker({ |
| 41 | environments, |
| 42 | value, |
| 43 | onChange, |
| 44 | disabled, |
| 45 | }: { |
| 46 | environments: ReadonlyArray<PickableEnvironment>; |
| 47 | value: EnvironmentId; |
| 48 | onChange: (environmentId: EnvironmentId) => void; |
| 49 | disabled: boolean; |
| 50 | }) { |
| 51 | return ( |
| 52 | <> |
| 53 | <MenuSeparator /> |
| 54 | <MenuRadioGroup |
| 55 | value={value} |
| 56 | onValueChange={(environmentId) => onChange(environmentId as EnvironmentId)} |
| 57 | > |
| 58 | {environments.map((environment) => ( |
| 59 | <MenuRadioItem |
| 60 | key={environment.environmentId} |
| 61 | value={environment.environmentId} |
| 62 | disabled={disabled} |
| 63 | > |
| 64 | <span className="flex min-w-0 items-center gap-2"> |
| 65 | <EnvironmentMachineIcon |
| 66 | kind={environment.machine ?? "server"} |
| 67 | className="size-3.5 shrink-0" |
| 68 | /> |
| 69 | <span className="truncate">{environment.label}</span> |
| 70 | </span> |
| 71 | </MenuRadioItem> |
| 72 | ))} |
| 73 | </MenuRadioGroup> |
| 74 | </> |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | export function PullRequestCheckoutMenu({ |
| 79 | handoff, |
| 80 | checkoutRoot, |
| 81 | canPrepareWorktree, |
| 82 | canCheckoutHere, |
| 83 | onCheckout, |
| 84 | pickableEnvironments, |
| 85 | actingEnvironmentId, |
| 86 | onActingEnvironmentChange, |
| 87 | term, |
| 88 | }: { |
| 89 | handoff: string | null; |
| 90 | checkoutRoot: string | null; |
| 91 | canPrepareWorktree: boolean; |
| 92 | canCheckoutHere: boolean; |
| 93 | onCheckout: (mode: "worktree" | "local") => void; |
| 94 | pickableEnvironments: ReadonlyArray<PickableEnvironment>; |
| 95 | actingEnvironmentId: EnvironmentId; |
| 96 | onActingEnvironmentChange: (environmentId: EnvironmentId) => void; |
| 97 | term: ChangeRequestTermKind; |
| 98 | }) { |
| 99 | const gt = useGT(); |
| 100 | return ( |
| 101 | <Menu> |
| 102 | <Tooltip> |
| 103 | <TooltipTrigger |
| 104 | render={ |
| 105 | <MenuTrigger |
| 106 | disabled={handoff !== null || checkoutRoot === null} |
| 107 | render={ |
| 108 | <Button |
| 109 | size="xs" |
| 110 | variant="outline" |
| 111 | aria-label={ |
| 112 | handoff?.startsWith("checkout") ? gt("Checking out...") : gt("Check out") |
| 113 | } |
| 114 | > |
| 115 | <GitBranchIcon aria-hidden className="size-3.5" /> |
| 116 | <span className="@max-[35rem]/pr-header:hidden"> |
| 117 | {handoff?.startsWith("checkout") ? gt("Checking out...") : gt("Check out")} |
| 118 | </span> |
| 119 | <ChevronDownIcon aria-hidden className="size-3.5 text-muted-foreground" /> |
| 120 | </Button> |
| 121 | } |
| 122 | /> |
| 123 | } |
| 124 | /> |
| 125 | <TooltipPopup> |
| 126 | <T> |
| 127 | Check out this{" "} |
| 128 | <Branch branch={term} mr="merge request" cr="change request"> |
| 129 | pull request |
| 130 | </Branch> |
| 131 | </T> |
| 132 | </TooltipPopup> |
| 133 | </Tooltip> |
| 134 | <MenuPopup align="end" side="bottom"> |
| 135 | <MenuItem disabled={!canPrepareWorktree} onClick={() => onCheckout("worktree")}> |
| 136 | <GitBranchIcon className="mt-1 size-3.5 shrink-0 self-start" /> |
| 137 | <span className="flex min-w-0 flex-col"> |
| 138 | <span> |
| 139 | <T>In a separate worktree</T> |
| 140 | </span> |
| 141 | <span className="text-xs text-muted-foreground"> |
| 142 | <T>Its own folder and thread. Nothing you have open moves.</T> |
| 143 | </span> |
| 144 | </span> |
| 145 | </MenuItem> |
| 146 | <MenuItem disabled={!canCheckoutHere} onClick={() => onCheckout("local")}> |
| 147 | <FolderGit2Icon className="mt-1 size-3.5 shrink-0 self-start" /> |
| 148 | <span className="flex min-w-0 flex-col"> |
| 149 | <span> |
| 150 | <T>In this repository</T> |
| 151 | </span> |
| 152 | <span className="text-xs text-muted-foreground"> |
| 153 | <T>Switches the branch you are working in.</T> |
| 154 | </span> |
| 155 | </span> |
| 156 | </MenuItem> |
| 157 | {pickableEnvironments.length > 0 ? ( |
| 158 | <ActOnEnvironmentPicker |
| 159 | environments={pickableEnvironments} |
| 160 | value={actingEnvironmentId} |
| 161 | onChange={onActingEnvironmentChange} |
| 162 | disabled={handoff !== null} |
| 163 | /> |
| 164 | ) : null} |
| 165 | </MenuPopup> |
| 166 | </Menu> |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | export function PullRequestResolveConflictsButton({ |
| 171 | handoff, |
| 172 | unavailable, |
| 173 | onResolve, |
| 174 | }: { |
| 175 | handoff: string | null; |
| 176 | unavailable: boolean; |
| 177 | onResolve: () => void; |
| 178 | }) { |
| 179 | const gt = useGT(); |
| 180 | return ( |
| 181 | <Tooltip> |
| 182 | <TooltipTrigger |
| 183 | render={ |
| 184 | <span className="inline-flex shrink-0"> |
| 185 | <Button |
| 186 | size="xs" |
| 187 | variant="destructive-outline" |
| 188 | disabled={handoff !== null || unavailable} |
| 189 | onClick={onResolve} |
| 190 | aria-label={handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")} |
| 191 | > |
| 192 | <PullRequestGlyph.conflicting aria-hidden className="size-3.5" /> |
| 193 | <span className="@max-[30rem]/pr-header:hidden"> |
| 194 | {handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")} |
| 195 | </span> |
| 196 | </Button> |
| 197 | </span> |
| 198 | } |
| 199 | /> |
| 200 | <TooltipPopup side="top"> |
| 201 | {handoff === "conflicts" ? gt("Preparing...") : gt("Resolve conflicts")} |
| 202 | </TooltipPopup> |
| 203 | </Tooltip> |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | /** The header's one prominent control, which depends on where the pull request stands. */ |
| 208 | export function PullRequestPrimaryAction({ |
| 209 | pullRequestKey, |
| 210 | primaryAction, |
| 211 | mergeBlockedReason, |
| 212 | autoMergeArmed, |
| 213 | armedAutoMergeLabel, |
| 214 | pendingAutoMergeLabel, |
| 215 | selectedMergeMethod, |
| 216 | selectedMergeMethodLabel, |
| 217 | statePresentation, |
| 218 | pendingAction, |
| 219 | perform, |
| 220 | resolveConflicts, |
| 221 | }: { |
| 222 | pullRequestKey: string; |
| 223 | primaryAction: ReturnType<typeof resolvePullRequestPrimaryControl> | null; |
| 224 | mergeBlockedReason: string | null; |
| 225 | autoMergeArmed: boolean; |
| 226 | armedAutoMergeLabel: string; |
| 227 | pendingAutoMergeLabel: string; |
| 228 | selectedMergeMethod: PullRequestMergeMethod; |
| 229 | selectedMergeMethodLabel: string; |
| 230 | statePresentation: ReturnType<typeof resolvePullRequestState> | null; |
| 231 | pendingAction: PullRequestAction | null; |
| 232 | perform: PerformPullRequestAction; |
| 233 | resolveConflicts: ReactNode; |
| 234 | }) { |
| 235 | const gt = useGT(); |
| 236 | const inlineConfirm = useInlineActionConfirm(pullRequestKey); |
| 237 | const actionPending = pendingAction !== null; |
| 238 | return ( |
| 239 | <> |
| 240 | {autoMergeArmed && primaryAction !== "auto-merge-armed" ? ( |
| 241 | <Tooltip> |
| 242 | <TooltipTrigger |
| 243 | render={ |
| 244 | <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}> |
| 245 | <PullRequestGlyph.merged aria-hidden className="size-3.5" /> |
| 246 | <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span> |
| 247 | </Badge> |
| 248 | } |
| 249 | /> |
| 250 | <TooltipPopup side="top"> |
| 251 | {gt("{label}: the host will merge this on its own once its requirements are met", { |
| 252 | label: armedAutoMergeLabel, |
| 253 | })} |
| 254 | </TooltipPopup> |
| 255 | </Tooltip> |
| 256 | ) : null} |
| 257 | {primaryAction === "resolve" ? ( |
| 258 | resolveConflicts |
| 259 | ) : primaryAction === "ready" ? ( |
| 260 | <Tooltip> |
| 261 | <TooltipTrigger |
| 262 | render={ |
| 263 | <span className="inline-flex shrink-0"> |
| 264 | <Button |
| 265 | size="xs" |
| 266 | variant="default" |
| 267 | disabled={actionPending} |
| 268 | onClick={() => void perform("ready")} |
| 269 | aria-label={gt("Ready for review")} |
| 270 | > |
| 271 | <PullRequestGlyph.pullRequest aria-hidden className="size-3.5" /> |
| 272 | <span className="@max-[30rem]/pr-header:hidden"> |
| 273 | <T>Ready for review</T> |
| 274 | </span> |
| 275 | </Button> |
| 276 | </span> |
| 277 | } |
| 278 | /> |
| 279 | <TooltipPopup side="top"> |
| 280 | <T>Ready for review</T> |
| 281 | </TooltipPopup> |
| 282 | </Tooltip> |
| 283 | ) : primaryAction === "enable-auto-merge" ? ( |
| 284 | <Tooltip> |
| 285 | <TooltipTrigger |
| 286 | render={ |
| 287 | <span className="inline-flex shrink-0"> |
| 288 | <Button |
| 289 | size="xs" |
| 290 | variant="default" |
| 291 | disabled={actionPending} |
| 292 | aria-disabled={mergeBlockedReason !== null || undefined} |
| 293 | aria-description={mergeBlockedReason ?? undefined} |
| 294 | {...(mergeBlockedReason === null |
| 295 | ? inlineConfirm.props( |
| 296 | "enable-auto-merge", |
| 297 | () => void perform("enable-auto-merge", selectedMergeMethod), |
| 298 | ) |
| 299 | : {})} |
| 300 | aria-label={ |
| 301 | pendingAction === "enable-auto-merge" |
| 302 | ? gt("Enabling...") |
| 303 | : inlineConfirm.isArmed("enable-auto-merge") |
| 304 | ? gt("Confirm auto-merge?") |
| 305 | : pendingAutoMergeLabel |
| 306 | } |
| 307 | > |
| 308 | {inlineConfirm.isArmed("enable-auto-merge") ? ( |
| 309 | <CheckIcon aria-hidden className="size-3.5" /> |
| 310 | ) : ( |
| 311 | <PullRequestGlyph.merged aria-hidden className="size-3.5" /> |
| 312 | )} |
| 313 | <AnimatedWidth className="@max-[30rem]/pr-header:hidden"> |
| 314 | <span className="grid"> |
| 315 | <span aria-hidden className="invisible col-start-1 row-start-1"> |
| 316 | {pendingAutoMergeLabel} |
| 317 | </span> |
| 318 | <span className="col-start-1 row-start-1"> |
| 319 | {pendingAction === "enable-auto-merge" |
| 320 | ? gt("Enabling...") |
| 321 | : inlineConfirm.isArmed("enable-auto-merge") |
| 322 | ? gt("Confirm auto-merge?") |
| 323 | : pendingAutoMergeLabel} |
| 324 | </span> |
| 325 | </span> |
| 326 | </AnimatedWidth> |
| 327 | </Button> |
| 328 | </span> |
| 329 | } |
| 330 | /> |
| 331 | <TooltipPopup side="top"> |
| 332 | {mergeBlockedReason ?? |
| 333 | (pendingAction === "enable-auto-merge" |
| 334 | ? gt("Enabling...") |
| 335 | : inlineConfirm.isArmed("enable-auto-merge") |
| 336 | ? gt("Confirm auto-merge?") |
| 337 | : pendingAutoMergeLabel)} |
| 338 | </TooltipPopup> |
| 339 | </Tooltip> |
| 340 | ) : primaryAction === "auto-merge-armed" ? ( |
| 341 | <Tooltip> |
| 342 | <TooltipTrigger |
| 343 | render={ |
| 344 | <Badge size="control" variant="info" role="img" aria-label={armedAutoMergeLabel}> |
| 345 | <PullRequestGlyph.merged aria-hidden className="size-3.5" /> |
| 346 | <span className="@max-[30rem]/pr-header:hidden">{armedAutoMergeLabel}</span> |
| 347 | </Badge> |
| 348 | } |
| 349 | /> |
| 350 | <TooltipPopup side="top"> |
| 351 | {gt("{label}: the host will merge this on its own once its requirements are met", { |
| 352 | label: armedAutoMergeLabel, |
| 353 | })} |
| 354 | </TooltipPopup> |
| 355 | </Tooltip> |
| 356 | ) : primaryAction === "merge" ? ( |
| 357 | <Tooltip> |
| 358 | <TooltipTrigger |
| 359 | render={ |
| 360 | <span className="inline-flex shrink-0"> |
| 361 | <Button |
| 362 | size="xs" |
| 363 | variant="default" |
| 364 | disabled={actionPending} |
| 365 | aria-disabled={mergeBlockedReason !== null || undefined} |
| 366 | aria-description={mergeBlockedReason ?? undefined} |
| 367 | {...(mergeBlockedReason === null |
| 368 | ? inlineConfirm.props("merge", () => void perform("merge", selectedMergeMethod)) |
| 369 | : {})} |
| 370 | aria-label={ |
| 371 | pendingAction === "merge" |
| 372 | ? gt("Merging...") |
| 373 | : inlineConfirm.isArmed("merge") |
| 374 | ? gt("Confirm merge?") |
| 375 | : selectedMergeMethodLabel |
| 376 | } |
| 377 | > |
| 378 | {inlineConfirm.isArmed("merge") ? ( |
| 379 | <CheckIcon aria-hidden className="size-3.5" /> |
| 380 | ) : ( |
| 381 | <PullRequestGlyph.merged aria-hidden className="size-3.5" /> |
| 382 | )} |
| 383 | <AnimatedWidth className="@max-[30rem]/pr-header:hidden"> |
| 384 | <span className="grid"> |
| 385 | <span aria-hidden className="invisible col-start-1 row-start-1"> |
| 386 | {selectedMergeMethodLabel} |
| 387 | </span> |
| 388 | <span className="col-start-1 row-start-1"> |
| 389 | {pendingAction === "merge" |
| 390 | ? gt("Merging...") |
| 391 | : inlineConfirm.isArmed("merge") |
| 392 | ? gt("Confirm merge?") |
| 393 | : selectedMergeMethodLabel} |
| 394 | </span> |
| 395 | </span> |
| 396 | </AnimatedWidth> |
| 397 | </Button> |
| 398 | </span> |
| 399 | } |
| 400 | /> |
| 401 | <TooltipPopup side="top"> |
| 402 | {mergeBlockedReason ?? |
| 403 | (pendingAction === "merge" |
| 404 | ? gt("Merging...") |
| 405 | : inlineConfirm.isArmed("merge") |
| 406 | ? gt("Confirm merge?") |
| 407 | : selectedMergeMethodLabel)} |
| 408 | </TooltipPopup> |
| 409 | </Tooltip> |
| 410 | ) : (primaryAction === "merged" || primaryAction === "closed") && |
| 411 | statePresentation !== null ? ( |
| 412 | <Badge size="control" variant="outline"> |
| 413 | <span className={cn("flex items-center gap-1", statePresentation.toneClassName)}> |
| 414 | <statePresentation.Icon className="size-3.5" /> |
| 415 | {statePresentation.label} |
| 416 | </span> |
| 417 | </Badge> |
| 418 | ) : null} |
| 419 | </> |
| 420 | ); |
| 421 | } |
| 422 | |
| 423 | export function PullRequestApproveWorkflowsButton({ |
| 424 | pullRequestKey, |
| 425 | pendingAction, |
| 426 | perform, |
| 427 | }: { |
| 428 | pullRequestKey: string; |
| 429 | pendingAction: PullRequestAction | null; |
| 430 | perform: PerformPullRequestAction; |
| 431 | }) { |
| 432 | const gt = useGT(); |
| 433 | const inlineConfirm = useInlineActionConfirm(pullRequestKey); |
| 434 | const actionPending = pendingAction !== null; |
| 435 | return ( |
| 436 | <Tooltip> |
| 437 | <TooltipTrigger |
| 438 | render={ |
| 439 | <span className="ml-auto inline-flex shrink-0"> |
| 440 | <Button |
| 441 | size="xs" |
| 442 | variant="warning-outline" |
| 443 | disabled={actionPending} |
| 444 | {...inlineConfirm.props("approve-workflows", () => void perform("approve-workflows"))} |
| 445 | aria-label={ |
| 446 | pendingAction === "approve-workflows" |
| 447 | ? gt("Approving...") |
| 448 | : inlineConfirm.isArmed("approve-workflows") |
| 449 | ? gt("Confirm approve?") |
| 450 | : gt("Approve workflows to run") |
| 451 | } |
| 452 | > |
| 453 | <PlayIcon aria-hidden className="size-3.5" /> |
| 454 | <span className="inline-grid"> |
| 455 | <span aria-hidden className="invisible col-start-1 row-start-1"> |
| 456 | <T>Approve workflows to run</T> |
| 457 | </span> |
| 458 | <span aria-hidden className="invisible col-start-1 row-start-1"> |
| 459 | <T>Confirm approve?</T> |
| 460 | </span> |
| 461 | <span className="col-start-1 row-start-1"> |
| 462 | {pendingAction === "approve-workflows" |
| 463 | ? gt("Approving...") |
| 464 | : inlineConfirm.isArmed("approve-workflows") |
| 465 | ? gt("Confirm approve?") |
| 466 | : gt("Approve workflows to run")} |
| 467 | </span> |
| 468 | </span> |
| 469 | </Button> |
| 470 | </span> |
| 471 | } |
| 472 | /> |
| 473 | <TooltipPopup side="top"> |
| 474 | {pendingAction === "approve-workflows" |
| 475 | ? gt("Approving...") |
| 476 | : inlineConfirm.isArmed("approve-workflows") |
| 477 | ? gt("Confirm approve?") |
| 478 | : gt("Approve workflows to run")} |
| 479 | </TooltipPopup> |
| 480 | </Tooltip> |
| 481 | ); |
| 482 | } |