Files
orca/mobile/src/browser/use-mobile-browser-request.ts
Neil 692ff95dd3 Split mobile browser pane modules (#17141)
* Split speech session lifecycle

* Split terminal output scheduler pipeline

* Split mobile browser pane modules

* Fix F3-speech for #17123

* Fix F1-cycle for #17131
2026-08-29 20:04:45 -07:00

68 lines
2.0 KiB
TypeScript

import { useCallback, type Dispatch, type SetStateAction } from 'react'
import type { RpcClient } from '../transport/rpc-client'
import type { RpcFailure, RpcSuccess } from '../transport/types'
import { browserErrorMessage, shouldSurfaceBrowserError } from './mobile-browser-frame-state'
type BrowserRequestArgs = {
busyRef: { current: boolean }
client: RpcClient | null
pageId: string | null
setBusy: Dispatch<SetStateAction<boolean>>
setError: Dispatch<SetStateAction<string | null>>
worktreeId: string
}
export function useMobileBrowserRequest(args: BrowserRequestArgs) {
const { busyRef, client, pageId, setBusy, setError, worktreeId } = args
const pageParams = useCallback(() => {
if (!pageId) {
return null
}
return {
worktree: `id:${worktreeId}`,
page: pageId
}
}, [pageId, worktreeId])
const sendBrowserRequest = useCallback(
async (
method: string,
params: Record<string, unknown> = {},
opts: { showBusy?: boolean; suppressError?: boolean; timeoutMs?: number } = {}
): Promise<unknown | null> => {
const base = pageParams()
if (!client || !base) {
return null
}
if (opts.showBusy) {
busyRef.current = true
setBusy(true)
}
try {
const response = await client.sendRequest(
method,
{ ...base, ...params },
{ timeoutMs: opts.timeoutMs ?? 15_000 }
)
if (!response.ok) {
throw new Error((response as RpcFailure).error.message)
}
setError(null)
return (response as RpcSuccess).result
} catch (err) {
const message = browserErrorMessage(err, 'Browser command failed')
if (!opts.suppressError && shouldSurfaceBrowserError(message)) {
setError(message)
}
return null
} finally {
if (opts.showBusy) {
busyRef.current = false
setBusy(false)
}
}
},
[client, pageParams]
)
return { pageParams, sendBrowserRequest }
}