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.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { useCallback, type Dispatch, type MutableRefObject, type SetStateAction } from 'react'
import { translate } from '../i18n/mobile-i18n'
import type { RouteHandoff } from '../navigation/route-handoff'
import type { RpcClient } from '../transport/rpc-client'
import { triggerError, triggerSuccess } from '../platform/haptics'
import { useMobileCommitMessageGeneration } from './use-mobile-commit-message-generation'
import { useMobileSourceControlCommitRunners } from './use-mobile-source-control-commit-runners'
import { useMobileSourceControlActionSheetRunners } from './use-mobile-source-control-action-sheet-runners'
import { useMobileCreatePrRunner } from './use-mobile-create-pr-runner'
import { useMobileStageAllRunner } from './use-mobile-stage-all-runner'
import { useMobileConflictRunners } from './use-mobile-conflict-runners'
import type { RuntimeGitLocalBranches } from '../../../src/shared/runtime-types'
import type { MobileGitStatusResult } from './mobile-git-status'
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 Params = {
client: RpcClient | null
hostId: string
worktreeId: string
status: MobileGitStatusResult | null
branchLabel: string
commitMessage: string
stagedEntries: MobileCommitFailureRecovery['stagedEntries']
generatingMessage: boolean
stageablePaths: string[]
unstageablePaths: string[]
router: RouteHandoff
sendGitRequest: SendGitRequest
sendCommitRequest: (message: string) => Promise<unknown>
runGitSyncSteps: () => Promise<void>
loadStatus: (options?: LoadStatusOptions) => Promise<boolean>
mountedRef: MutableRefObject<boolean>
busyActionRef: MutableRefObject<string | null>
setBusyAction: (next: string | null) => void
setActionError: (next: string | null) => void
setCommitMessage: Dispatch<SetStateAction<string>>
setGeneratingMessage: (next: boolean) => void
setShowActionSheet: (next: boolean) => void
setLocalBranches: (next: RuntimeGitLocalBranches | null) => void
setShowBranchPicker: (next: boolean) => void
setCreatedPrUrl: (next: string | null) => void
setCreatedPrWarning: (next: string | null) => void
recordCommitFailure: RecordMobileCommitFailure
onOpenHistory?: () => void
}
export function useMobileSourceControlRunners(params: Params) {
const {
client,
hostId,
worktreeId,
status,
branchLabel,
commitMessage,
stagedEntries,
generatingMessage,
stageablePaths,
unstageablePaths,
router,
sendGitRequest,
sendCommitRequest,
runGitSyncSteps,
loadStatus,
mountedRef,
busyActionRef,
setBusyAction,
setActionError,
setCommitMessage,
setGeneratingMessage,
setShowActionSheet,
setLocalBranches,
setShowBranchPicker,
setCreatedPrUrl,
setCreatedPrWarning,
recordCommitFailure,
onOpenHistory
} = params
const runGitWorkflow = useCallback(
async (
actionId: string,
runner: () => Promise<void>,
options?: { clearCommitMessage?: boolean }
) => {
if (busyActionRef.current) {
return false
}
busyActionRef.current = actionId
setBusyAction(actionId)
setActionError(null)
recordCommitFailure(null)
try {
await runner()
if (!mountedRef.current) {
return false
}
if (options?.clearCommitMessage) {
setCommitMessage('')
}
triggerSuccess()
await loadStatus({ preserveReadyOnFailure: true, force: true })
return true
} catch (err) {
if (!mountedRef.current) {
return false
}
triggerError()
const text = translate('sourceControl.runners.actionFailed', 'Source control action failed')
setActionError(err instanceof Error ? err.message : text)
return false
} finally {
if (busyActionRef.current === actionId) {
busyActionRef.current = null
if (mountedRef.current) {
setBusyAction(null)
}
}
}
},
[
busyActionRef,
loadStatus,
mountedRef,
recordCommitFailure,
setActionError,
setBusyAction,
setCommitMessage
]
)
const runGitAction = useCallback(
async (actionId: string, method: string, p: Record<string, unknown>) => {
return await runGitWorkflow(actionId, async () => {
await sendGitRequest<unknown>(method, p)
})
},
[runGitWorkflow, sendGitRequest]
)
const runGitSequence = useCallback(
async (actionId: string, steps: GitStep[], options?: { clearCommitMessage?: boolean }) => {
return await runGitWorkflow(
actionId,
async () => {
for (const step of steps) {
await sendGitRequest<unknown>(step.method, step.params)
}
},
options
)
},
[runGitWorkflow, sendGitRequest]
)
const runGitSync = useCallback(
async (actionId: string) => await runGitWorkflow(actionId, runGitSyncSteps),
[runGitSyncSteps, runGitWorkflow]
)
const stageAll = useMobileStageAllRunner({
status,
stageablePaths,
runGitWorkflow,
sendGitRequest
})
const unstageAll = useCallback(async () => {
if (unstageablePaths.length === 0) {
return
}
await runGitAction('unstage-all', 'git.bulkUnstage', { filePaths: unstageablePaths })
}, [runGitAction, unstageablePaths])
const { generateCommitMessage, cancelGenerateCommitMessage } = useMobileCommitMessageGeneration({
client,
worktreeId,
generatingMessage,
mountedRef,
busyActionRef,
setGeneratingMessage,
setCommitMessage,
setActionError
})
const { commit, runCommitSequence, runCommitSyncSequence } = useMobileSourceControlCommitRunners({
commitMessage,
stagedEntries,
sendGitRequest,
sendCommitRequest,
cancelGenerateCommitMessage,
runGitSyncSteps,
runGitWorkflow,
loadStatus,
mountedRef,
busyActionRef,
setBusyAction,
setActionError,
setCommitMessage,
recordCommitFailure
})
const createPr = useMobileCreatePrRunner({
client,
worktreeId,
status,
branchLabel,
commitMessage,
stagedEntries,
mountedRef,
runGitWorkflow,
loadStatus,
cancelGenerateCommitMessage,
setActionError,
setCommitMessage,
setShowActionSheet,
setCreatedPrUrl,
setCreatedPrWarning,
recordCommitFailure
})
const openBranchPicker = useCallback(() => {
setShowActionSheet(false)
setLocalBranches(null)
setShowBranchPicker(true)
if (client) {
void sendGitRequest<RuntimeGitLocalBranches>('git.localBranches')
.then((result) => {
if (mountedRef.current) {
setLocalBranches(result)
}
})
.catch(() => {
if (mountedRef.current) {
setLocalBranches({ current: null, branches: [] })
}
})
}
}, [
client,
mountedRef,
sendGitRequest,
setLocalBranches,
setShowActionSheet,
setShowBranchPicker
])
const openHistory = useCallback(() => {
setShowActionSheet(false)
// History is a local hub segment; fallback navigation targets the hub directly to avoid a redirect hop.
if (onOpenHistory) {
onOpenHistory()
return
}
if (hostId && worktreeId) {
router.push({
pathname: '/h/[hostId]/source-control/[worktreeId]',
params: {
hostId,
worktreeId,
tab: 'history'
}
} as Parameters<typeof router.push>[0])
}
}, [hostId, onOpenHistory, router, setShowActionSheet, worktreeId])
const checkoutBranch = useCallback(
async (branch: string) => {
setShowBranchPicker(false)
await runGitAction('checkout', 'git.checkout', { branch })
},
[runGitAction, setShowBranchPicker]
)
const actionSheetRunners = useMobileSourceControlActionSheetRunners({
client,
sendGitRequest,
runGitWorkflow,
runGitSequence,
runGitSync,
commit,
runCommitSequence,
runCommitSyncSequence,
setShowActionSheet
})
const conflictRunners = useMobileConflictRunners({ sendGitRequest, runGitAction, runGitWorkflow })
return {
runGitAction,
stageAll,
unstageAll,
commit,
generateCommitMessage,
cancelGenerateCommitMessage,
createPr,
openBranchPicker,
openHistory,
checkoutBranch,
...conflictRunners,
...actionSheetRunners
}
}