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