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.

5.1 KB
Raw Blame
import { useCallback, type MutableRefObject } from 'react'
import { translate } from '../i18n/mobile-i18n'
import { triggerError, triggerSuccess } from '../platform/haptics'
import type { LoadStatusOptions } from './mobile-source-control-screen-state'
import type {
  MobileCommitFailureRecovery,
  RecordMobileCommitFailure
} from './mobile-commit-failure-recovery'

type GitStep = { method: string; params?: Record<string, unknown> }
type SendGitRequest = <T>(method: string, params?: Record<string, unknown>) => Promise<T>
type RunGitWorkflow = (
  actionId: string,
  runner: () => Promise<void>,
  options?: { clearCommitMessage?: boolean }
) => Promise<boolean>

type Params = {
  commitMessage: string
  stagedEntries: MobileCommitFailureRecovery['stagedEntries']
  sendGitRequest: SendGitRequest
  sendCommitRequest: (message: string) => Promise<unknown>
  cancelGenerateCommitMessage: () => void
  runGitSyncSteps: () => Promise<void>
  runGitWorkflow: RunGitWorkflow
  loadStatus: (options?: LoadStatusOptions) => Promise<boolean>
  mountedRef: MutableRefObject<boolean>
  busyActionRef: MutableRefObject<string | null>
  setBusyAction: (next: string | null) => void
  setActionError: (next: string | null) => void
  setCommitMessage: (next: string) => void
  recordCommitFailure: RecordMobileCommitFailure
}

export function useMobileSourceControlCommitRunners(params: Params) {
  const {
    commitMessage,
    stagedEntries,
    sendGitRequest,
    sendCommitRequest: sendCommitRequestOnly,
    cancelGenerateCommitMessage,
    runGitSyncSteps,
    runGitWorkflow,
    loadStatus,
    mountedRef,
    busyActionRef,
    setBusyAction,
    setActionError,
    setCommitMessage,
    recordCommitFailure
  } = params
  // Cancel generation before committing so its late reply cannot refill the cleared message box.
  const sendCommitRequest = useCallback(
    (message: string) => {
      cancelGenerateCommitMessage()
      return sendCommitRequestOnly(message)
    },
    [cancelGenerateCommitMessage, sendCommitRequestOnly]
  )

  const commit = useCallback(async () => {
    const message = commitMessage.trim()
    if (!message) {
      return false
    }
    return await runGitWorkflow(
      'commit',
      async () => {
        try {
          await sendCommitRequest(message)
        } catch (err) {
          recordCommitFailure({
            error:
              err instanceof Error
                ? err.message
                : translate('sourceControl.commitRunners.commitFailed', 'Commit failed'),
            commitMessage: message,
            stagedEntries
          })
          throw err
        }
      },
      { clearCommitMessage: true }
    )
  }, [commitMessage, recordCommitFailure, runGitWorkflow, sendCommitRequest, stagedEntries])

  const runCommitFollowUps = useCallback(
    async (actionId: string, afterCommit: () => Promise<void>) => {
      const message = commitMessage.trim()
      if (!message) {
        return false
      }
      if (busyActionRef.current) {
        return false
      }
      busyActionRef.current = actionId
      setBusyAction(actionId)
      setActionError(null)
      recordCommitFailure(null)
      let didCommit = false
      try {
        await sendCommitRequest(message)
        didCommit = true
        await afterCommit()
        if (!mountedRef.current) {
          return false
        }
        setCommitMessage('')
        triggerSuccess()
        await loadStatus({ preserveReadyOnFailure: true, force: true })
        return true
      } catch (err) {
        if (!mountedRef.current) {
          return false
        }
        triggerError()
        const errorMessage =
          err instanceof Error
            ? err.message
            : translate('sourceControl.commitRunners.actionFailed', 'Source control action failed')
        if (!didCommit) {
          recordCommitFailure({ error: errorMessage, commitMessage: message, stagedEntries })
        }
        if (didCommit) {
          setCommitMessage('')
          await loadStatus({
            preserveReadyOnFailure: true,
            clearActionErrorOnSuccess: false,
            force: true
          })
        }
        setActionError(errorMessage)
        return false
      } finally {
        if (busyActionRef.current === actionId) {
          busyActionRef.current = null
          if (mountedRef.current) {
            setBusyAction(null)
          }
        }
      }
    },
    [
      busyActionRef,
      commitMessage,
      loadStatus,
      mountedRef,
      recordCommitFailure,
      sendCommitRequest,
      setActionError,
      setBusyAction,
      setCommitMessage,
      stagedEntries
    ]
  )

  const runCommitSequence = useCallback(
    async (actionId: string, afterCommit: GitStep[]) => {
      return await runCommitFollowUps(actionId, async () => {
        for (const step of afterCommit) {
          await sendGitRequest<unknown>(step.method, step.params)
        }
      })
    },
    [runCommitFollowUps, sendGitRequest]
  )

  const runCommitSyncSequence = useCallback(async () => {
    return await runCommitFollowUps('commit-sync', runGitSyncSteps)
  }, [runCommitFollowUps, runGitSyncSteps])

  return { commit, runCommitSequence, runCommitSyncSequence }
}