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 { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import * as Dialog from "../../components/ui/Dialog"; |
| 3 | import { useAtomRefresh, useAtomValue } from "@effect/atom-react"; |
| 4 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 5 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 6 | import { changeRequestTermKind } from "../../lib/changeRequestTerm"; |
| 7 | import type { PullRequestDetail, PullRequestMergeMethod } from "@t3tools/contracts"; |
| 8 | import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; |
| 9 | import { AsyncResult } from "effect/reactivity"; |
| 10 | import { useGT, useMessages } from "gt-react-native"; |
| 11 | import { useRef, useState } from "react"; |
| 12 | import { View } from "react-native"; |
| 13 | import { AppText as Text } from "../../components/ui/AppText"; |
| 14 | import { Button } from "../../components/ui/Button"; |
| 15 | import { SplitButton } from "../../components/ui/SplitButton"; |
| 16 | import { useEnvironmentServerConfig } from "../../state/entities"; |
| 17 | import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences"; |
| 18 | import { composerPullRequests } from "../../state/pull-requests"; |
| 19 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 20 | import { |
| 21 | allowedMergeMethods, |
| 22 | canPerformPullRequestAction, |
| 23 | mergeUnavailableReason, |
| 24 | MERGE_METHODS, |
| 25 | mergeMethodKind, |
| 26 | PR_ACTION_ICONS, |
| 27 | pullRequestActionLabel, |
| 28 | resolveMergeMethod, |
| 29 | } from "./pullRequestActions"; |
| 30 | import { pullRequestStackMerge, singlePullRequestMergeAllowed } from "./pullRequestStack"; |
| 31 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 32 | import { usePullRequestAction } from "./usePullRequestAction"; |
| 33 | import { usePullRequestPermissions } from "./usePullRequestPermissions"; |
| 34 | import { PullRequestGlyph } from "./pullRequestGlyphs"; |
| 35 | import type { AppSymbolName } from "../../components/ui/AppSymbol"; |
| 36 | import type { PullRequestStackState } from "./usePullRequestStack"; |
| 37 | import { m as resolveMessage } from "../../i18n/messages"; |
| 38 | |
| 39 | export function PullRequestMergeControls(props: { |
| 40 | readonly pr: PullRequestDetail; |
| 41 | readonly params: PullRequestParams; |
| 42 | readonly stack: PullRequestStackState; |
| 43 | }) { |
| 44 | const { pr, stack } = props; |
| 45 | const gt = useGT(); |
| 46 | const m = useMessages(); |
| 47 | const terminology = sourceControlClients.get(pr.provider).changeRequest; |
| 48 | const target = pullRequestTarget(props.params); |
| 49 | const permissions = usePullRequestPermissions(props.params, pr); |
| 50 | const stateAction = usePullRequestAction(props.params, pr); |
| 51 | const preferences = useAtomValue(mobilePreferencesAtom); |
| 52 | const serverSettings = useEnvironmentServerConfig(target.environmentId)?.settings; |
| 53 | const savePreferences = useAtomCommand(saveMobilePreferences); |
| 54 | const runAction = useAtomCommand(composerPullRequests.runAction, { reportFailure: false }); |
| 55 | const refreshDetail = useAtomRefresh(composerPullRequests.detail(target)); |
| 56 | const refreshActivity = useAtomRefresh(composerPullRequests.activity(target)); |
| 57 | const [selectedMethod, setSelectedMethod] = useState<PullRequestMergeMethod | null>(null); |
| 58 | const [busy, setBusy] = useState(false); |
| 59 | const pending = useRef(false); |
| 60 | const methods = allowedMergeMethods(pr); |
| 61 | const method = resolveMergeMethod( |
| 62 | pr, |
| 63 | selectedMethod, |
| 64 | serverSettings |
| 65 | ? resolveProjectSettings(serverSettings, target.input.projectId).settings |
| 66 | .pullRequestMergeMethod |
| 67 | : null, |
| 68 | AsyncResult.isSuccess(preferences) ? preferences.value.pullRequestMergeMethod : undefined, |
| 69 | ); |
| 70 | const scope = stack.stack ? pullRequestStackMerge(stack.stack, pr.number, terminology, m) : null; |
| 71 | const canMergeSingle = singlePullRequestMergeAllowed({ |
| 72 | supportsStackActions: stack.supportsStackActions, |
| 73 | hasStack: stack.stack !== null, |
| 74 | isFresh: stack.isFresh, |
| 75 | }); |
| 76 | const checkingStack = |
| 77 | permissions.connected && stack.supportsStackActions && !stack.isFresh && !stack.error; |
| 78 | const reason = |
| 79 | (!permissions.connected |
| 80 | ? gt("Reconnect to merge.") |
| 81 | : mergeUnavailableReason(pr, permissions.canWrite, terminology, m)) ?? |
| 82 | (stack.stack |
| 83 | ? !stack.supportsStackActions |
| 84 | ? gt("This environment can display stacks but cannot merge them.") |
| 85 | : !stack.isFresh |
| 86 | ? gt("Refresh the stack before merging.") |
| 87 | : scope?.reason |
| 88 | : !canMergeSingle |
| 89 | ? stack.error |
| 90 | ? gt("Couldn't check stack membership. Refresh before merging.") |
| 91 | : gt("Checking stack membership…") |
| 92 | : null); |
| 93 | const canMerge = !busy && method !== undefined && !reason; |
| 94 | const label = method |
| 95 | ? scope && scope.layers.length > 1 |
| 96 | ? gt( |
| 97 | "{kind, select, squash {Squash and merge stack ({count})} merge {Create a merge commit stack ({count})} rebase {Rebase and merge stack ({count})} other {Fast-forward merge stack ({count})}}", |
| 98 | { kind: mergeMethodKind(method), count: scope.layers.length }, |
| 99 | ) |
| 100 | : m(MERGE_METHODS[method].title) |
| 101 | : gt("Merge unavailable"); |
| 102 | const nextAction = |
| 103 | pr.state === "closed" ? "reopen" : pr.state === "open" && pr.isDraft ? "ready" : null; |
| 104 | // States the button itself names, instead of a reason line under it. Draft and |
| 105 | // closed offer their way forward when the viewer can take it. |
| 106 | const status: { |
| 107 | label: string; |
| 108 | icon: AppSymbolName; |
| 109 | final?: boolean; |
| 110 | action?: "ready" | "reopen"; |
| 111 | } | null = |
| 112 | nextAction && stateAction.available(nextAction) |
| 113 | ? { |
| 114 | label: pullRequestActionLabel(nextAction, terminology, m), |
| 115 | icon: PR_ACTION_ICONS[nextAction], |
| 116 | final: nextAction === "reopen", |
| 117 | action: nextAction, |
| 118 | } |
| 119 | : pr.state === "merged" |
| 120 | ? { |
| 121 | label: gt("Merged", { $context: "pull request state" }), |
| 122 | icon: PullRequestGlyph.merged, |
| 123 | final: true, |
| 124 | } |
| 125 | : pr.state === "closed" |
| 126 | ? { |
| 127 | label: gt("Closed", { $context: "pull request state" }), |
| 128 | icon: PullRequestGlyph.closed, |
| 129 | final: true, |
| 130 | } |
| 131 | : pr.isDraft |
| 132 | ? { |
| 133 | label: gt("Draft", { $context: "pull request state" }), |
| 134 | icon: PullRequestGlyph.draft, |
| 135 | } |
| 136 | : pr.mergeability === "conflicting" |
| 137 | ? { label: gt("Resolve conflicts to merge"), icon: PullRequestGlyph.conflicting } |
| 138 | : pr.autoMergeEnabled |
| 139 | ? { |
| 140 | label: pr.autoMergeMethod |
| 141 | ? gt( |
| 142 | "Auto-merge pending ({kind, select, squash {squash} merge {merge} rebase {rebase} fastForward {fast-forward} other {{method}}})", |
| 143 | { |
| 144 | kind: mergeMethodKind(pr.autoMergeMethod), |
| 145 | method: pr.autoMergeMethod, |
| 146 | }, |
| 147 | ) |
| 148 | : gt("Auto-merge pending"), |
| 149 | icon: "clock", |
| 150 | } |
| 151 | : checkingStack && !busy |
| 152 | ? { label: gt("Checking stack…"), icon: PullRequestGlyph.merged } |
| 153 | : null; |
| 154 | const autoAction = pr.autoMergeEnabled ? "disable-auto-merge" : "enable-auto-merge"; |
| 155 | const canAutoMerge = |
| 156 | !busy && |
| 157 | (autoAction === "disable-auto-merge" || (!stack.stack && canMergeSingle)) && |
| 158 | permissions.canWrite && |
| 159 | canPerformPullRequestAction(pr, autoAction); |
| 160 | |
| 161 | const perform = async (action: "merge" | "enable-auto-merge" | "disable-auto-merge") => { |
| 162 | if ( |
| 163 | pending.current || |
| 164 | (action !== "disable-auto-merge" && !method) || |
| 165 | (action === "merge" ? !canMerge : !canAutoMerge) |
| 166 | ) |
| 167 | return; |
| 168 | pending.current = true; |
| 169 | setBusy(true); |
| 170 | const response = await runAction({ |
| 171 | ...target, |
| 172 | input: { |
| 173 | ...target.input, |
| 174 | action, |
| 175 | ...(action === "merge" && method && stack.stack && scope |
| 176 | ? { |
| 177 | stackNumber: stack.stack.number, |
| 178 | expectedStackHeads: scope.heads, |
| 179 | mergeMethod: method, |
| 180 | } |
| 181 | : action === "merge" && method |
| 182 | ? { |
| 183 | resolveMergeMethod: (fresh) => { |
| 184 | if ( |
| 185 | !canPerformPullRequestAction(fresh, "merge") || |
| 186 | !allowedMergeMethods(fresh).includes(method) |
| 187 | ) |
| 188 | throw new Error( |
| 189 | gt( |
| 190 | "The {term, select, mr {merge request} cr {change request} other {pull request}} changed. Refresh before merging.", |
| 191 | { |
| 192 | term: changeRequestTermKind(terminology), |
| 193 | }, |
| 194 | ), |
| 195 | ); |
| 196 | return method; |
| 197 | }, |
| 198 | } |
| 199 | : action === "enable-auto-merge" && method |
| 200 | ? { mergeMethod: method } |
| 201 | : {}), |
| 202 | }, |
| 203 | }); |
| 204 | // A stack can partly merge before the provider reports a failure. |
| 205 | if (!AsyncResult.isSuccess(response)) refreshDetail(); |
| 206 | refreshActivity(); |
| 207 | stack.refresh(); |
| 208 | pending.current = false; |
| 209 | setBusy(false); |
| 210 | if (AsyncResult.isFailure(response)) { |
| 211 | const cause = squashAtomCommandFailure(response); |
| 212 | Dialog.alert( |
| 213 | gt("Couldn't complete the merge action"), |
| 214 | cause instanceof Error |
| 215 | ? (errorDisplayMessage(cause, resolveMessage) ?? cause.message) |
| 216 | : gt("Try again."), |
| 217 | ); |
| 218 | } |
| 219 | }; |
| 220 | const confirm = (action: "merge" | "enable-auto-merge" | "disable-auto-merge") => { |
| 221 | const title = |
| 222 | action === "merge" |
| 223 | ? scope && scope.layers.length > 1 |
| 224 | ? gt( |
| 225 | "{count, plural, one {Merge {count} stack layers?} other {Merge {count} stack layers?}}", |
| 226 | { count: scope.layers.length }, |
| 227 | ) |
| 228 | : gt( |
| 229 | "Merge {term, select, mr {merge request} cr {change request} other {pull request}}?", |
| 230 | { term: changeRequestTermKind(terminology) }, |
| 231 | ) |
| 232 | : action === "enable-auto-merge" |
| 233 | ? gt("Enable auto-merge?") |
| 234 | : gt("Disable auto-merge?"); |
| 235 | const message = |
| 236 | action === "merge" && scope |
| 237 | ? gt( |
| 238 | "Merge {layers} into {base} using {kind, select, squash {squash} merge {merge} rebase {rebase} fastForward {fast-forward} other {{method}}}. Layers above #{number} stay open.", |
| 239 | { |
| 240 | layers: scope.layers.map((layer) => `#${layer.number}`).join(" → "), |
| 241 | base: stack.stack?.base ?? "", |
| 242 | kind: mergeMethodKind(method), |
| 243 | method: method ?? "", |
| 244 | number: pr.number, |
| 245 | }, |
| 246 | ) |
| 247 | : action === "enable-auto-merge" |
| 248 | ? gt( |
| 249 | "Merge #{number} using {kind, select, squash {squash} merge {merge} rebase {rebase} fastForward {fast-forward} other {{method}}} when its requirements are met. This may happen immediately.", |
| 250 | { number: pr.number, kind: mergeMethodKind(method), method: method ?? "" }, |
| 251 | ) |
| 252 | : action === "disable-auto-merge" |
| 253 | ? gt( |
| 254 | "Stop the provider from automatically merging this {term, select, mr {merge request} cr {change request} other {pull request}}.", |
| 255 | { |
| 256 | term: changeRequestTermKind(terminology), |
| 257 | }, |
| 258 | ) |
| 259 | : gt( |
| 260 | "Merge #{number} into {branch} using {kind, select, squash {squash} merge {merge} rebase {rebase} fastForward {fast-forward} other {{method}}}.", |
| 261 | { |
| 262 | number: pr.number, |
| 263 | branch: pr.baseBranch, |
| 264 | kind: mergeMethodKind(method), |
| 265 | method: method ?? "", |
| 266 | }, |
| 267 | ); |
| 268 | Dialog.alert(title, message, [ |
| 269 | { text: gt("Cancel"), style: "cancel" }, |
| 270 | { |
| 271 | text: |
| 272 | action === "merge" |
| 273 | ? label |
| 274 | : action === "enable-auto-merge" |
| 275 | ? gt("Enable auto-merge") |
| 276 | : gt("Disable auto-merge"), |
| 277 | onPress: () => void perform(action), |
| 278 | }, |
| 279 | ]); |
| 280 | }; |
| 281 | return ( |
| 282 | <View className="gap-2 border-t border-border-subtle p-3"> |
| 283 | <SplitButton |
| 284 | label={ |
| 285 | busy || stateAction.busy |
| 286 | ? gt("Working…") |
| 287 | : !permissions.connected |
| 288 | ? gt("Reconnect to merge") |
| 289 | : (status?.label ?? label) |
| 290 | } |
| 291 | accessibilityLabel={ |
| 292 | status |
| 293 | ? status.label |
| 294 | : scope |
| 295 | ? gt( |
| 296 | "{count, plural, one {{label}, {count} stack layers through '#'{number}} other {{label}, {count} stack layers through '#'{number}}}", |
| 297 | { |
| 298 | label, |
| 299 | count: scope.layers.length, |
| 300 | number: pr.number, |
| 301 | }, |
| 302 | ) |
| 303 | : label |
| 304 | } |
| 305 | icon={status?.icon ?? PullRequestGlyph.merged} |
| 306 | disabled={status?.action ? stateAction.busy : !canMerge || status !== null} |
| 307 | loading={busy || stateAction.busy || (!status?.action && checkingStack)} |
| 308 | onPress={() => |
| 309 | status?.action ? void stateAction.perform(status.action) : confirm("merge") |
| 310 | } |
| 311 | menuTitle={gt("Merge options")} |
| 312 | actions={ |
| 313 | status?.final |
| 314 | ? [] |
| 315 | : [ |
| 316 | ...methods.map((id) => ({ |
| 317 | id, |
| 318 | title: m(MERGE_METHODS[id].title), |
| 319 | state: id === method ? ("on" as const) : ("off" as const), |
| 320 | attributes: { disabled: !canMerge && !canAutoMerge }, |
| 321 | })), |
| 322 | ...(canAutoMerge |
| 323 | ? [ |
| 324 | { |
| 325 | id: autoAction, |
| 326 | title: pr.autoMergeEnabled |
| 327 | ? gt("Disable auto-merge") |
| 328 | : gt("Enable auto-merge"), |
| 329 | image: "clock", |
| 330 | }, |
| 331 | ] |
| 332 | : []), |
| 333 | ] |
| 334 | } |
| 335 | onPressAction={(id) => { |
| 336 | const selected = methods.find((method) => method === id); |
| 337 | if (selected) { |
| 338 | setSelectedMethod(selected); |
| 339 | void savePreferences({ pullRequestMergeMethod: selected }); |
| 340 | } else if (id === autoAction && canAutoMerge) confirm(autoAction); |
| 341 | }} |
| 342 | /> |
| 343 | {permissions.connected && !status && reason ? ( |
| 344 | <Text className="px-1 text-xs text-foreground-muted">{reason}</Text> |
| 345 | ) : null} |
| 346 | {stack.error && stack.supportsStackActions ? ( |
| 347 | <Button label={gt("Retry stack lookup")} tone="text" onPress={stack.refresh} /> |
| 348 | ) : null} |
| 349 | </View> |
| 350 | ); |
| 351 | } |