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

shell.ts

42 lines1.8 KB

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