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 { AsyncResult, Atom } from "effect/unstable/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(
25 AsyncResult.success<EnvironmentShellState>({
26 snapshot: Option.none(),
27 status: "empty",
28 error: Option.none(),
29 }),
30).pipe(Atom.withLabel("mobile-environment-shell:empty"));
31
32/** Reads one environment's shell projection without waiting on other environments. */
33export function useEnvironmentShellState(environmentId: EnvironmentId | null) {
34 const result = useAtomValue(
35 environmentId === null
36 ? EMPTY_ENVIRONMENT_SHELL_STATE_ATOM
37 : environmentShell.stateAtom(environmentId),
38 );
39 return Option.getOrElse(AsyncResult.value(result), () => ({
40 snapshot: Option.none(),
41 status: "empty" as const,
42 error: Option.none(),
43 }));
44}