Files
orca/mobile/src/source-control/use-mobile-source-control-loaders.ts
T
Jinjing 026516f323 the-sandbox-flag-is-2 (#8236)
* Fix mobile source control drawer overflow and branch-compare state loss

- Render BottomDrawer in a native Modal so it covers the full viewport
  even when mounted inside a ScrollView.
- Move the conflict/Abort row onto its own line so it never overflows
  the branch card, and enlarge the Abort hit target.
- Show the committed-on-branch footer even when the changed-files
  SectionList has no sections, since RN skips ListFooterComponent for
  empty sections.
- Stop branch-compare state from collapsing to idle/error on transient
  base-ref resolution failures when a ready result should be preserved.

* Add mobile source control drawer reload screenshot

Attaches an evidence screenshot for the mobile source control drawer overflow / branch-compare state loss fix.

* Remove stray temp screenshot file

Accidentally committed debug artifact from mobile source control drawer work; not needed in the repo.
2026-07-10 23:04:38 -07:00

288 lines
10 KiB
TypeScript

import { useCallback, useEffect, useRef, useState, type MutableRefObject } from 'react'
import { View } from 'react-native'
import type { RpcClient } from '../transport/rpc-client'
import type { ConnectionState, RpcSuccess } from '../transport/types'
import { resolveMobileBranchCompareBaseRef } from './mobile-branch-base-ref'
import {
isMobileGitUnavailable,
isMobileGitTransientRefreshError,
type MobileGitStatusResult
} from './mobile-git-status'
import type { MobileGitBranchCompareResult } from './mobile-branch-compare'
import {
SELECTOR_RETRY_COUNT,
SELECTOR_RETRY_DELAY_MS,
wait,
type LoadStatusOptions,
type MobileBranchCompareState,
type ScreenState,
type StatusLoadInFlight
} from './mobile-source-control-screen-state'
type Params = {
client: RpcClient | null
connState: ConnectionState
statusIdentityKey: string
worktreeId: string
setActionError: (message: string | null) => void
onStatusLoadSuccess?: () => void
}
export type MobileSourceControlLoaders = {
screenState: ScreenState
setScreenState: (next: ScreenState | ((prev: ScreenState) => ScreenState)) => void
branchCompareState: MobileBranchCompareState
setBranchCompareState: (
next: MobileBranchCompareState | ((prev: MobileBranchCompareState) => MobileBranchCompareState)
) => void
mountedRef: MutableRefObject<boolean>
setRootRef: (node: View | null) => void
loadStatus: (options?: LoadStatusOptions) => Promise<boolean>
}
// Owns git.status / git.branchCompare loading, the load-generation guards, and
// the mount ref so the giant state hook stays under the line limit.
export function useMobileSourceControlLoaders(params: Params): MobileSourceControlLoaders {
const { client, connState, statusIdentityKey, worktreeId, setActionError, onStatusLoadSuccess } =
params
const [screenState, setScreenState] = useState<ScreenState>({ kind: 'loading' })
const [branchCompareState, setBranchCompareState] = useState<MobileBranchCompareState>({
kind: 'idle'
})
const currentStatusIdentityRef = useRef('')
const currentBranchCompareIdentityRef = useRef('')
const loadGenerationRef = useRef(0)
const branchCompareGenerationRef = useRef(0)
const mountedRef = useRef(true)
const statusLoadInFlightRef = useRef<StatusLoadInFlight | null>(null)
// Why: the same route can be reused for another worktree/host (identity change);
// a kept-on-failure `ready` state would otherwise show the previous worktree's
// data until the fresh load resolves. Reset to loading in the render phase (the
// React "adjust state on prop change" pattern) before the new load runs.
const lastResetIdentityRef = useRef(statusIdentityKey)
if (lastResetIdentityRef.current !== statusIdentityKey) {
lastResetIdentityRef.current = statusIdentityKey
setScreenState({ kind: 'loading' })
setBranchCompareState({ kind: 'idle' })
}
currentStatusIdentityRef.current = statusIdentityKey
currentBranchCompareIdentityRef.current = statusIdentityKey
const setRootRef = useCallback((node: View | null): void => {
if (node !== null) {
mountedRef.current = true
return
}
// Why: source-control RPC loads can outlive the route; invalidate pending
// writes when the screen detaches without a passive cleanup-only Effect.
mountedRef.current = false
loadGenerationRef.current += 1
branchCompareGenerationRef.current += 1
}, [])
const loadBranchCompare = useCallback(
async (options?: { preserveReadyOnFailure?: boolean }) => {
const loadKey = statusIdentityKey
const generation = branchCompareGenerationRef.current + 1
branchCompareGenerationRef.current = generation
const isCurrentLoad = () =>
mountedRef.current &&
branchCompareGenerationRef.current === generation &&
currentBranchCompareIdentityRef.current === loadKey
if (!worktreeId || !client || connState !== 'connected') {
if (isCurrentLoad()) {
setBranchCompareState({ kind: 'idle' })
}
return false
}
setBranchCompareState((prev) => (prev.kind === 'ready' ? prev : { kind: 'loading' }))
try {
const baseRef = await resolveMobileBranchCompareBaseRef(client, worktreeId)
if (!isCurrentLoad()) {
return false
}
if (!baseRef) {
// Why: wiping a prior ready compare to idle makes Changes say "No
// Changes" even when commits still exist (e.g. after abort-merge refresh).
setBranchCompareState((prev) => {
if (options?.preserveReadyOnFailure && prev.kind === 'ready') {
return prev
}
return {
kind: 'error',
message: 'Unable to resolve the base branch for comparison.'
}
})
return false
}
const response = await client.sendRequest('git.branchCompare', {
worktree: `id:${worktreeId}`,
baseRef
})
if (!isCurrentLoad()) {
return false
}
if (!response.ok) {
if (isMobileGitUnavailable(response.error?.code, response.error?.message)) {
setBranchCompareState((prev) => {
if (options?.preserveReadyOnFailure && prev.kind === 'ready') {
return prev
}
return { kind: 'idle' }
})
return false
}
throw new Error(response.error?.message || 'Unable to load committed changes')
}
setBranchCompareState({
kind: 'ready',
result: (response as RpcSuccess).result as MobileGitBranchCompareResult
})
return true
} catch (err) {
if (!isCurrentLoad()) {
return false
}
const message = err instanceof Error ? err.message : 'Unable to load committed changes'
setBranchCompareState((prev) => {
if (options?.preserveReadyOnFailure && prev.kind === 'ready') {
return prev
}
return { kind: 'error', message }
})
return false
}
},
[client, connState, statusIdentityKey, worktreeId]
)
const loadStatus = useCallback(
async (options?: LoadStatusOptions) => {
const loadKey = statusIdentityKey
const inFlight = statusLoadInFlightRef.current
if (inFlight && !options?.force && inFlight.key === loadKey && inFlight.client === client) {
return await inFlight.promise
}
const loadPromise = (async () => {
const generation = loadGenerationRef.current + 1
loadGenerationRef.current = generation
const isCurrentLoad = () =>
mountedRef.current &&
loadGenerationRef.current === generation &&
currentStatusIdentityRef.current === loadKey
if (!worktreeId) {
if (isCurrentLoad()) {
setScreenState({ kind: 'loading' })
}
return false
}
if (!client || connState !== 'connected') {
if (isCurrentLoad()) {
setScreenState({
kind: 'error',
message:
connState === 'connected' ? 'Connecting to desktop...' : 'Waiting for desktop...'
})
}
return false
}
if (!isCurrentLoad()) {
return false
}
setScreenState((prev) => (prev.kind === 'ready' ? prev : { kind: 'loading' }))
try {
for (let attempt = 0; attempt <= SELECTOR_RETRY_COUNT; attempt += 1) {
const response = await client.sendRequest('git.status', {
worktree: `id:${worktreeId}`
})
if (!isCurrentLoad()) {
return false
}
if (response.ok) {
const result = (response as RpcSuccess).result as MobileGitStatusResult
setScreenState({ kind: 'ready', status: result })
void loadBranchCompare({ preserveReadyOnFailure: true })
if (options?.clearActionErrorOnSuccess !== false) {
setActionError(null)
}
// Why: recovery prompts are based on a specific failed commit
// snapshot; a fresh status means that snapshot may be stale.
onStatusLoadSuccess?.()
return true
}
if (isMobileGitUnavailable(response.error?.code, response.error?.message)) {
setScreenState({
kind: 'unavailable',
message: 'Update Orca desktop to use Source Control on mobile.'
})
return false
}
const shouldRetry =
response.error?.code === 'selector_not_found' ||
isMobileGitTransientRefreshError(response.error?.code, response.error?.message)
if (shouldRetry && attempt < SELECTOR_RETRY_COUNT) {
await wait(SELECTOR_RETRY_DELAY_MS)
if (!isCurrentLoad()) {
return false
}
continue
}
throw new Error(response.error?.message || 'Unable to load source control')
}
} catch (err) {
if (!isCurrentLoad()) {
return false
}
const message = err instanceof Error ? err.message : 'Unable to load source control'
setScreenState((prev) => {
// Why: git mutations can succeed while the immediate status refresh
// races a desktop abort; keep the last good screen instead of flashing
// a full-screen error that Retry fixes a moment later.
if (options?.preserveReadyOnFailure && prev.kind === 'ready') {
return prev
}
return { kind: 'error', message }
})
return false
}
return false
})()
statusLoadInFlightRef.current = { key: loadKey, client, promise: loadPromise }
try {
return await loadPromise
} finally {
if (statusLoadInFlightRef.current?.promise === loadPromise) {
statusLoadInFlightRef.current = null
}
}
},
[
client,
connState,
loadBranchCompare,
onStatusLoadSuccess,
statusIdentityKey,
worktreeId,
setActionError
]
)
useEffect(() => {
void loadStatus()
}, [loadStatus])
return {
screenState,
setScreenState,
branchCompareState,
setBranchCompareState,
mountedRef,
setRootRef,
loadStatus
}
}