The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { RegistryContext, useAtomValue } from "@effect/atom-react"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import { AsyncResult } from "effect/reactivity"; |
| 4 | import { useMessages } from "gt-react"; |
| 5 | import { useContext } from "react"; |
| 6 | |
| 7 | import { agentSessionScan } from "../state/agentSessions"; |
| 8 | import { environmentAtomList, environmentIdsKey } from "../state/environmentAtomList"; |
| 9 | import { formatEnvironmentQueryError } from "../state/query"; |
| 10 | |
| 11 | const scanAtom = (environmentId: EnvironmentId) => agentSessionScan({ environmentId, input: {} }); |
| 12 | const projectScansAtom = environmentAtomList("web-project-scans", scanAtom); |
| 13 | |
| 14 | export function useProjectScans(environmentIds: readonly EnvironmentId[]) { |
| 15 | const resolve = useMessages(); |
| 16 | const registry = useContext(RegistryContext); |
| 17 | const results = useAtomValue(projectScansAtom(environmentIdsKey(environmentIds))); |
| 18 | return environmentIds.map((environmentId, index) => { |
| 19 | const result = results[index]!; |
| 20 | return { |
| 21 | environmentId, |
| 22 | data: AsyncResult.getOrElse(result, () => null), |
| 23 | error: result._tag === "Failure" ? formatEnvironmentQueryError(result.cause, resolve) : null, |
| 24 | isPending: result.waiting || result._tag === "Initial", |
| 25 | refresh: () => registry.refresh(scanAtom(environmentId)), |
| 26 | }; |
| 27 | }); |
| 28 | } |