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.

1.6 KB
Raw Blame
import { useCallback, useEffect, useRef, type RefObject } from 'react'
import { AppState } from 'react-native'
import { translate } from '../i18n/mobile-i18n'

// Silent startup/bridge failures may emit no error; a native watchdog must turn the blank pane into a reported failure.
const WEB_READY_WATCHDOG_MS = 15000

export function useTerminalWebReadyWatchdog(
  isWebReadyRef: RefObject<boolean>,
  reportEngineError: (message: string, fatal: boolean) => void
) {
  const watchdogRef = useRef<ReturnType<typeof setTimeout> | null>(null)

  const clearWebReadyWatchdog = useCallback(() => {
    if (watchdogRef.current) {
      clearTimeout(watchdogRef.current)
      watchdogRef.current = null
    }
  }, [])

  const armWebReadyWatchdog = useCallback(() => {
    clearWebReadyWatchdog()
    const fire = () => {
      watchdogRef.current = null
      if (isWebReadyRef.current) {
        return
      }
      if (AppState.currentState !== 'active') {
        // Background WebViews may legitimately stall; judge readiness only in foreground.
        watchdogRef.current = setTimeout(fire, WEB_READY_WATCHDOG_MS)
        return
      }
      reportEngineError(
        translate(
          'terminal.webviewReadyWatchdog.initializeFailed',
          'Terminal did not initialize - no ready signal from the terminal view'
        ),
        true
      )
    }
    watchdogRef.current = setTimeout(fire, WEB_READY_WATCHDOG_MS)
  }, [clearWebReadyWatchdog, isWebReadyRef, reportEngineError])

  useEffect(() => {
    armWebReadyWatchdog()
    return clearWebReadyWatchdog
  }, [armWebReadyWatchdog, clearWebReadyWatchdog])

  return { armWebReadyWatchdog, clearWebReadyWatchdog }
}