Files
orca/mobile/src/session/use-mobile-structured-selection-controller.ts
T
Neil 19043a3d76 refactor(mobile): organize pairing, session, RPC and host owners (#39846)
* refactor(mobile): give relay and encryption concrete owners

* refactor(mobile): organize session and review feature owners

* refactor(mobile): organize RPC and host ownership

* test(mobile): point render fixtures at concrete owners

* refactor(mobile): use explicit type-only owner imports
2026-11-08 07:19:05 -08:00

59 lines
1.9 KiB
TypeScript

import { useEffect, useMemo, useSyncExternalStore } from 'react'
import { createStructuredSessionSelectionController } from '../../../src/shared/agent-session/options/structured-agent-session-selection-operations'
import { persistMobileStructuredOptionPicks } from './native-chat/input/mobile-native-chat-session-option-persistence'
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
/** Temporary operations belong to one phone authority, above its queried host answer. */
export function useMobileStructuredSelectionController(args: {
agent: string | null
client: RpcClient | null
sessionId: string | null
onRevalidate: () => void
}) {
const { agent, client, sessionId, onRevalidate } = args
const clientGeneration = useSyncExternalStore(client?.onStateChange ?? (() => () => {}), () =>
client?.getGeneration?.()
)
const scope = useMemo(() => {
let attached = true
const controller = createStructuredSessionSelectionController(
(picks) => {
if (agent) {
void persistMobileStructuredOptionPicks({ client, agent, picks })
}
},
() => attached
)
return {
controller,
attach: () => {
attached = true
},
detach: () => {
attached = false
controller.abandon()
}
}
}, [agent, client, sessionId])
const { controller } = scope
useEffect(() => {
scope.attach()
return scope.detach
}, [scope])
const selectionRevision = useSyncExternalStore(controller.subscribe, controller.snapshot)
const terminal = controller.terminalSnapshot()
useEffect(() => {
if (terminal) {
onRevalidate()
}
}, [terminal, onRevalidate])
useEffect(() => {
return client?.onStateChange((state) => {
if (state === 'connected') {
onRevalidate()
}
})
}, [client, controller, onRevalidate])
return { controller, selectionRevision, clientGeneration }
}