1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentPresentation } from "@t3tools/client-runtime/connection";
3import {
4 createEnvironmentPresentationAtoms,
5 createEnvironmentSummaryAtoms,
6} from "@t3tools/client-runtime/state/presentation";
7import type { EnvironmentId } from "@t3tools/contracts";
8import { Atom } from "effect/reactivity";
9
10import { environmentCatalog } from "../connection/catalog";
11import { serverEnvironment } from "./server";
12
13export const environmentPresentations = createEnvironmentPresentationAtoms({
14 catalogValueAtom: environmentCatalog.catalogValueAtom,
15 stateAtom: environmentCatalog.stateAtom,
16 serverConfigValueAtom: serverEnvironment.configValueAtom,
17});
18
19export const environmentSummaries = createEnvironmentSummaryAtoms({
20 catalogValueAtom: environmentCatalog.catalogValueAtom,
21 presentationAtom: environmentPresentations.presentationAtom,
22});
23
24const EMPTY_ENVIRONMENT_PRESENTATION_ATOM = Atom.make<EnvironmentPresentation | null>(null).pipe(
25 Atom.withLabel("web-environment-presentation:empty"),
26);
27
28export function useEnvironmentPresentation(environmentId: EnvironmentId | null) {
29 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
30 const presentation = useAtomValue(
31 environmentId === null
32 ? EMPTY_ENVIRONMENT_PRESENTATION_ATOM
33 : environmentPresentations.presentationAtom(environmentId),
34 );
35 return {
36 isReady: catalog.isReady,
37 presentation,
38 };
39}