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