| 1 | import { RegistryContext, useAtomValue } from "@effect/atom-react"; |
| 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 5 | import { useContext, useMemo } from "react"; |
| 6 | |
| 7 | import { agentSessionScan } from "../state/agentSessions"; |
| 8 | import { formatEnvironmentQueryError } from "../state/query"; |
| 9 | |
| 10 | /** Subscribe to each selected computer without coupling their failures or refreshes. */ |
| 11 | export function useProjectScans(environmentIds: readonly EnvironmentId[]) { |
| 12 | const registry = useContext(RegistryContext); |
| 13 | const scansAtom = useMemo( |
| 14 | () => |
| 15 | Atom.make((get) => |
| 16 | environmentIds.map((environmentId) => { |
| 17 | const atom = agentSessionScan({ environmentId, input: {} }); |
| 18 | const result = get(atom); |
| 19 | return { |
| 20 | environmentId, |
| 21 | data: Option.getOrNull(AsyncResult.value(result)), |
| 22 | error: result._tag === "Failure" ? formatEnvironmentQueryError(result.cause) : null, |
| 23 | isPending: result.waiting || result._tag === "Initial", |
| 24 | refresh: () => registry.refresh(atom), |
| 25 | }; |
| 26 | }), |
| 27 | ), |
| 28 | [environmentIds, registry], |
| 29 | ); |
| 30 | return useAtomValue(scansAtom); |
| 31 | } |