Open History to see the last commit
1import {
2 createEnvironmentShellAtoms,
3 createEnvironmentShellSummaryAtom,
4 createEnvironmentSnapshotAtom,
5 createShellEnvironmentAtoms,
6 type EnvironmentShellState,
7} from "@t3tools/client-runtime/state/shell";
8import { useAtomValue } from "@effect/atom-react";
9import type { EnvironmentId } from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import { Atom } from "effect/reactivity";
12
13import { environmentCatalog } from "../connection/catalog";
14import { connectionAtomRuntime } from "../connection/runtime";
15
16export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
17export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
18export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
19export const environmentShellSummaryAtom = createEnvironmentShellSummaryAtom({
20 catalogValueAtom: environmentCatalog.catalogValueAtom,
21 shellStateValueAtom: environmentShell.stateValueAtom,
22});
23
24const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make<EnvironmentShellState>({
25 snapshot: Option.none(),
26 status: "empty",
27 error: Option.none(),
28});
29
30const shellStatus = (state: EnvironmentShellState) => state.status;
31const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.error);
32
33export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
34 const atom =
35 environmentId === null
36 ? EMPTY_ENVIRONMENT_SHELL_STATE_ATOM
37 : environmentShell.stateValueAtom(environmentId);
38 return {
39 status: useAtomValue(atom, shellStatus),
40 hasError: useAtomValue(atom, shellHasError),
41 };
42}