mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
* 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
135 lines
4.9 KiB
TypeScript
135 lines
4.9 KiB
TypeScript
import { useMobileTasksItemSelection } from './use-mobile-tasks-item-selection'
|
|
import { useCallback, useLayoutEffect, useRef, type SetStateAction } from 'react'
|
|
import type { ActionableTaskItem } from './mobile-tasks-project-workspace-types'
|
|
import type { DetailPayload, GitHubWorkItem } from './mobile-tasks-provider-detail-types'
|
|
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
|
|
import type { ConnectionState } from '../transport/connection/connection-state'
|
|
import {
|
|
githubWorkItemTargetKey,
|
|
resolveGitHubWorkItemTarget,
|
|
matchesGitHubWorkItemTarget
|
|
} from '../../../src/shared/github/work-item-target'
|
|
import type { TaskItem } from './mobile-tasks-legacy-foundation'
|
|
|
|
export function githubTaskKey(source: GitHubWorkItem): string {
|
|
const target = resolveGitHubWorkItemTarget(source)
|
|
return [source.repoId, target ? githubWorkItemTargetKey(target) : source.url].join('\0')
|
|
}
|
|
|
|
export function sameGitHubTask(
|
|
current: TaskItem | null,
|
|
item: Extract<TaskItem, { provider: 'github' }>
|
|
): current is Extract<TaskItem, { provider: 'github' }> {
|
|
return (
|
|
current?.provider === 'github' && githubTaskKey(current.source) === githubTaskKey(item.source)
|
|
)
|
|
}
|
|
|
|
export function requireGitHubTaskTarget(source: GitHubWorkItem) {
|
|
const target = resolveGitHubWorkItemTarget(source)
|
|
if (!target || !matchesGitHubWorkItemTarget(source, target)) {
|
|
throw new Error('Unable to identify this GitHub item. Open it again from Tasks.')
|
|
}
|
|
return target
|
|
}
|
|
|
|
export function useMobileTasksSelection(
|
|
client: RpcClient | null,
|
|
readDetails: () => DetailPayload | null,
|
|
// The shared file owner's retire/recover step, run from this listener for each real connection change.
|
|
onConnectionChange: (connected: boolean) => void
|
|
) {
|
|
const {
|
|
actionItem,
|
|
detailRequestItem,
|
|
setActionItem: publishItem,
|
|
hydrateActionItem: publishHydration
|
|
} = useMobileTasksItemSelection()
|
|
const owner = useRef({ item: actionItem, visit: {}, client, revision: 0, live: true })
|
|
useLayoutEffect(() => {
|
|
const current = owner.current
|
|
if (current.client !== client) {
|
|
current.revision += 1
|
|
current.client = client
|
|
}
|
|
}, [client])
|
|
useLayoutEffect(() => {
|
|
owner.current.live = true
|
|
let observed: { state: ConnectionState; generation: number | undefined } | null = null
|
|
// One owner sees every raw (state, generation) change, including a down/up pair in one render.
|
|
const reconcile = (state: ConnectionState) => {
|
|
const generation = client?.getGeneration?.()
|
|
if (observed?.state === state && Object.is(observed.generation, generation)) {
|
|
return
|
|
}
|
|
const first = observed === null
|
|
observed = { state, generation }
|
|
if (first) {
|
|
return
|
|
}
|
|
if (state !== 'connected') {
|
|
owner.current.revision += 1
|
|
}
|
|
onConnectionChange(state === 'connected')
|
|
}
|
|
const stop = client?.onStateChange(reconcile)
|
|
if (client) {
|
|
reconcile(client.getState())
|
|
}
|
|
return () => {
|
|
stop?.()
|
|
owner.current.live = false
|
|
owner.current.revision += 1
|
|
}
|
|
}, [client, onConnectionChange])
|
|
const setActionItem = useCallback(
|
|
(update: SetStateAction<ActionableTaskItem | null>) => {
|
|
const current = owner.current
|
|
const next = typeof update === 'function' ? update(current.item) : update
|
|
const key = (item: ActionableTaskItem | null) =>
|
|
item?.provider === 'github' ? githubTaskKey(item.source) : (item?.key ?? null)
|
|
if (key(next) !== key(current.item)) {
|
|
current.visit = {}
|
|
}
|
|
current.item = next
|
|
publishItem(next)
|
|
},
|
|
[publishItem]
|
|
)
|
|
const hydrateActionItem = useCallback(
|
|
(update: SetStateAction<ActionableTaskItem | null>) => {
|
|
const current = owner.current
|
|
const next = typeof update === 'function' ? update(current.item) : update
|
|
current.item = next
|
|
publishHydration(next)
|
|
},
|
|
[publishHydration]
|
|
)
|
|
const claimPublication = useCallback(
|
|
(item: TaskItem | null, accepted?: DetailPayload | null) => {
|
|
const current = owner.current
|
|
const { visit, client: selected, revision } = current
|
|
const generation = selected?.getGeneration?.()
|
|
return () => {
|
|
const details = readDetails()
|
|
return (
|
|
current.live &&
|
|
current.visit === visit &&
|
|
current.client === selected &&
|
|
current.revision === revision &&
|
|
selected?.getState() === 'connected' &&
|
|
selected.getGeneration?.() === generation &&
|
|
(item?.provider !== 'github' || sameGitHubTask(current.item, item)) &&
|
|
(accepted?.provider !== 'github' ||
|
|
(details?.provider === 'github' &&
|
|
details.targetKey === accepted.targetKey &&
|
|
details.headSha === accepted.headSha &&
|
|
details.baseSha === accepted.baseSha))
|
|
)
|
|
}
|
|
},
|
|
[readDetails]
|
|
)
|
|
return { actionItem, detailRequestItem, setActionItem, hydrateActionItem, claimPublication }
|
|
}
|