Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.8 KB
Raw Blame
import { useCallback, useEffect, useRef, type MutableRefObject } from 'react'
import type { RpcClient } from '../transport/rpc-client'
import { triggerError } from '../platform/haptics'
import { translate } from '../i18n/mobile-i18n'
import type { MobileGitStatusResult } from './mobile-git-status'
import type { LoadStatusOptions } from './mobile-source-control-screen-state'
import {
  getMobileCommitFailureStagedEntries,
  type MobileCommitFailureRecovery,
  type RecordMobileCommitFailure
} from './mobile-commit-failure-recovery'
import {
  mobileHostedReviewCreateIntentProgressMessage,
  type MobileHostedReviewCreateIntentProgress
} from './mobile-hosted-review-create-intent'
import {
  isMobileHostedReviewCommitFailure,
  runMobileHostedReviewCreateIntent,
  type MobileHostedReviewCreateIntentRunOutcome
} from './mobile-hosted-review-create-intent-runner'

type RunGitWorkflow = (actionId: string, runner: () => Promise<void>) => Promise<boolean>
type LoadStatus = (options?: LoadStatusOptions) => Promise<boolean>

type Params = {
  client: RpcClient | null
  worktreeId: string
  status: MobileGitStatusResult | null
  branchLabel: string
  commitMessage: string
  stagedEntries: MobileCommitFailureRecovery['stagedEntries']
  mountedRef: MutableRefObject<boolean>
  runGitWorkflow: RunGitWorkflow
  loadStatus: LoadStatus
  cancelGenerateCommitMessage: () => void
  setActionError: (next: string | null) => void
  setCommitMessage: (next: string) => void
  setShowActionSheet: (next: boolean) => void
  setCreatedPrUrl: (next: string | null) => void
  setCreatedPrWarning: (next: string | null) => void
  recordCommitFailure: RecordMobileCommitFailure
}

export function useMobileCreatePrRunner({
  client,
  worktreeId,
  status,
  branchLabel,
  commitMessage,
  stagedEntries,
  mountedRef,
  runGitWorkflow,
  loadStatus,
  cancelGenerateCommitMessage,
  setActionError,
  setCommitMessage,
  setShowActionSheet,
  setCreatedPrUrl,
  setCreatedPrWarning,
  recordCommitFailure
}: Params) {
  const commitMessageRef = useRef(commitMessage)
  useEffect(() => {
    commitMessageRef.current = commitMessage
  }, [commitMessage])
  return useCallback(
    async (pushFirst: boolean) => {
      setShowActionSheet(false)
      const branch = status?.branch
      if (!client || !branch) {
        triggerError()
        setActionError(
          translate(
            'sourceControl.useMobileCreatePrRunner.branchRequired',
            'Check out a branch before creating a pull request.'
          )
        )
        return
      }
      const created: { current: MobileHostedReviewCreateIntentRunOutcome | null } = {
        current: null
      }
      let progress: MobileHostedReviewCreateIntentProgress | null = null
      const ran = await runGitWorkflow(pushFirst ? 'push-create-pr' : 'create-pr', async () => {
        // Committing the current message supersedes any generation still in flight.
        cancelGenerateCommitMessage()
        created.current = await runMobileHostedReviewCreateIntent(client, worktreeId, {
          branch,
          title: branchLabel,
          status,
          commitMessage,
          readCommitMessage: () => commitMessageRef.current,
          onProgress: (nextProgress: MobileHostedReviewCreateIntentProgress) => {
            progress = nextProgress
            setActionError(mobileHostedReviewCreateIntentProgressMessage(nextProgress))
          }
        })
        if (!created.current.ok) {
          throw new Error(created.current.error)
        }
      })
      const outcome = created.current
      if (outcome?.committed && mountedRef.current) {
        setCommitMessage('')
      }
      if (!ran && outcome?.status !== undefined && mountedRef.current) {
        await loadStatus({
          preserveReadyOnFailure: true,
          clearActionErrorOnSuccess: false,
          force: true
        })
      }
      if (!ran || !mountedRef.current || !outcome || !outcome.ok) {
        if (!ran && outcome && isMobileHostedReviewCommitFailure(outcome, progress)) {
          const outcomeStagedEntries = getMobileCommitFailureStagedEntries(outcome.status?.entries)
          recordCommitFailure({
            error: outcome.error,
            commitMessage: outcome.commitMessage ?? commitMessage.trim(),
            stagedEntries: outcomeStagedEntries.length > 0 ? outcomeStagedEntries : stagedEntries
          })
        }
        return
      }
      setActionError(null)
      setCreatedPrUrl(outcome.url)
      setCreatedPrWarning(outcome.warning ?? null)
    },
    [
      branchLabel,
      cancelGenerateCommitMessage,
      client,
      commitMessage,
      loadStatus,
      mountedRef,
      recordCommitFailure,
      runGitWorkflow,
      setActionError,
      setCommitMessage,
      setCreatedPrUrl,
      setCreatedPrWarning,
      setShowActionSheet,
      stagedEntries,
      status,
      worktreeId
    ]
  )
}