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