Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

shell.ts

40 lines1.7 KB

1import {
2 createEnvironmentShellAtoms,
3 createEnvironmentShellSummaryAtom,
4 createEnvironmentSnapshotAtom,
5 EMPTY_SHELL_STATE,
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 environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
17export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
18export const environmentShellSummaryAtom = createEnvironmentShellSummaryAtom({
19 catalogValueAtom: environmentCatalog.catalogValueAtom,
20 shellStateValueAtom: environmentShell.stateValueAtom,
21});
22​
23const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make<EnvironmentShellState>(EMPTY_SHELL_STATE);
24​
25const shellStatus = (state: EnvironmentShellState) => state.status;
26const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.error);
27// The empty placeholder stands until the environment has read its cached shell.
28const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE;
29​
30export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
31 const atom =
32 environmentId === null
33 ? EMPTY_ENVIRONMENT_SHELL_STATE_ATOM
34 : environmentShell.stateValueAtom(environmentId);
35 return {
36 status: useAtomValue(atom, shellStatus),
37 hasError: useAtomValue(atom, shellHasError),
38 cacheRead: useAtomValue(atom, shellCacheRead),
39 };