Files
archived-orca/src/shared/source-control-dropdown-action-context.ts
T
Kelvin Amoaba c903d30283 fix(mobile): build the Source Control action sheet from desktop's menu rows (#34047)
* fix(mobile): build the Source Control action sheet from desktop's menu rows

Adds Force Push and desktop's order, enable rules and hints.

* fix(mobile): rebase from desktop's effective base and keep the Force Push warning in the action sheet

* fix(mobile): keep the rebase row's base when the branch compare fails

* fix(mobile): keep upstream compares working when the default base lookup fails

* fix(mobile): keep the rebase target apart from the branch compare so failed or unsupported compares keep it

* fix(mobile): leave force pushes to desktop when the linked review has its own push target

* refactor(mobile): rebase onto the shown base and share the force-push block with the primary action
2026-10-27 13:13:25 +00:00

138 lines
4.9 KiB
TypeScript

// Derive shared branch/review facts once so dropdown labels and disabled states cannot disagree.
import type { SourceControlPrimaryActionDecisionInputs } from './source-control-primary-action-decision-types'
import { canSubmitCommit, resolveCommitDisabledReason } from './source-control-commit-eligibility'
import type { GitConflictOperation } from './git-status-types'
import { shouldForcePushWithLeaseForUpstream } from './git-upstream-status'
import type { DropdownActionInputs } from './source-control-dropdown-item-types'
import { formatForcePushTitle } from './source-control-dropdown-labels'
export type DropdownActionContext = {
upstreamStatus: SourceControlPrimaryActionDecisionInputs['upstreamStatus']
hostedReviewCreation: SourceControlPrimaryActionDecisionInputs['hostedReviewCreation']
branchCommitsAhead: number | undefined
conflictOperation: GitConflictOperation
isPullRequestOperationActive: boolean
canPushLinkedReviewWithoutUpstream: boolean
rebaseBaseRef: string | null | undefined
hasDirtyLocalChanges: boolean
upstreamLoading: boolean
hasUpstream: boolean
hasOpenHostedReview: boolean
canPushUntrackedHostedReview: boolean
pushBlockedByOpenHostedReviewTarget: boolean
publishBlockedByMergedPR: boolean
publishBlockedByPRLoading: boolean
publishBlockedByOpenHostedReview: boolean
publishBlockedByDetachedHead: boolean
ahead: number
behind: number
shouldForcePushWithLease: boolean
pushLabelCount: number
forcePushTitle: string
globalBusy: boolean
commitDisabledReason: ReturnType<typeof resolveCommitDisabledReason>
canCommit: boolean
}
export function deriveDropdownActionContext(inputs: DropdownActionInputs): DropdownActionContext {
const {
stagedCount,
hasPartiallyStagedChanges,
hasMessage,
hasUnresolvedConflicts,
isCommitting,
isRemoteOperationActive,
upstreamStatus,
prState,
isPRStateLoading,
hostedReviewCreation,
conflictOperation = 'unknown',
branchCommitsAhead,
hasCurrentBranch = true,
canPushLinkedReviewWithoutUpstream = false,
rebaseBaseRef,
isPullRequestOperationActive = false
} = inputs
const hasStaged = stagedCount > 0
const hasDirtyLocalChanges = hasStaged || inputs.hasUnstagedChanges
// Undefined means loading, not unpublished; treating it as false could republish and clobber the upstream.
const upstreamLoading = upstreamStatus === undefined
const hasUpstream = upstreamStatus?.hasUpstream ?? false
const hasOpenHostedReview = prState === 'open' || prState === 'draft'
const canPushUntrackedHostedReview =
!hasUpstream &&
hasOpenHostedReview &&
hasCurrentBranch &&
branchCommitsAhead !== 0 &&
canPushLinkedReviewWithoutUpstream
// Only missing review heads block Push; zero ahead commits still means the target is known.
const pushBlockedByOpenHostedReviewTarget =
!hasUpstream && hasOpenHostedReview && !canPushLinkedReviewWithoutUpstream
const publishBlockedByMergedPR = !hasUpstream && prState === 'merged'
const publishBlockedByPRLoading = !hasUpstream && !!isPRStateLoading
const publishBlockedByOpenHostedReview = !hasUpstream && hasOpenHostedReview
const publishBlockedByDetachedHead = !hasUpstream && !hasCurrentBranch
const ahead = upstreamStatus?.ahead ?? 0
const behind = upstreamStatus?.behind ?? 0
const shouldForcePushWithLease = shouldForcePushWithLeaseForUpstream(upstreamStatus)
// Use branch-compare counts; upstream ahead can be zero while loading or inflated by patch-equivalent rewrites.
const pushLabelCount =
branchCommitsAhead !== undefined &&
branchCommitsAhead > 0 &&
(shouldForcePushWithLease || !hasUpstream)
? branchCommitsAhead
: ahead
const forcePushTitle = formatForcePushTitle(branchCommitsAhead, upstreamStatus?.upstreamName)
// Lock all rows during in-flight work to prevent queued actions against stale snapshots.
const globalBusy = isCommitting || isRemoteOperationActive || isPullRequestOperationActive
const commitDisabledReason = resolveCommitDisabledReason({
stagedCount,
hasPartiallyStagedChanges,
hasMessage,
hasUnresolvedConflicts
})
const canCommit =
!globalBusy &&
canSubmitCommit({
stagedCount,
hasPartiallyStagedChanges,
hasMessage,
hasUnresolvedConflicts,
isCommitting,
isRemoteOperationActive,
isPullRequestOperationActive
})
return {
upstreamStatus,
hostedReviewCreation,
branchCommitsAhead,
conflictOperation,
isPullRequestOperationActive,
canPushLinkedReviewWithoutUpstream,
rebaseBaseRef,
hasDirtyLocalChanges,
upstreamLoading,
hasUpstream,
hasOpenHostedReview,
canPushUntrackedHostedReview,
pushBlockedByOpenHostedReviewTarget,
publishBlockedByMergedPR,
publishBlockedByPRLoading,
publishBlockedByOpenHostedReview,
publishBlockedByDetachedHead,
ahead,
behind,
shouldForcePushWithLease,
pushLabelCount,
forcePushTitle,
globalBusy,
commitDisabledReason,
canCommit
}
}