| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | connectionCatalogDisplayUrl, |
| 4 | hasRelayRoute, |
| 5 | type EnvironmentPresentation as BaseEnvironmentPresentation, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 8 | import { useMemo } from "react"; |
| 9 | |
| 10 | import { environmentCatalog } from "../connection/catalog"; |
| 11 | import { environmentPresentations } from "./presentation"; |
| 12 | import { useEnvironmentQuery } from "./query"; |
| 13 | |
| 14 | export interface EnvironmentPresentation extends BaseEnvironmentPresentation { |
| 15 | readonly environmentId: EnvironmentId; |
| 16 | readonly label: string; |
| 17 | readonly displayUrl: string | null; |
| 18 | readonly relayManaged: boolean; |
| 19 | } |
| 20 | |
| 21 | export function projectEnvironmentPresentation( |
| 22 | environmentId: EnvironmentId, |
| 23 | presentation: BaseEnvironmentPresentation, |
| 24 | ): EnvironmentPresentation { |
| 25 | return { |
| 26 | ...presentation, |
| 27 | environmentId, |
| 28 | label: presentation.entry.target.label, |
| 29 | displayUrl: connectionCatalogDisplayUrl(presentation.entry), |
| 30 | relayManaged: hasRelayRoute(presentation.entry), |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export function useEnvironments() { |
| 35 | const catalog = useAtomValue(environmentCatalog.catalogValueAtom); |
| 36 | const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom); |
| 37 | const presentationById = useAtomValue(environmentPresentations.presentationsAtom); |
| 38 | |
| 39 | const environments = useMemo( |
| 40 | () => |
| 41 | [...presentationById.entries()].map(([environmentId, presentation]) => |
| 42 | projectEnvironmentPresentation(environmentId, presentation), |
| 43 | ), |
| 44 | [presentationById], |
| 45 | ); |
| 46 | |
| 47 | return { |
| 48 | isReady: catalog.isReady, |
| 49 | networkStatus, |
| 50 | environments, |
| 51 | presentationById, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | export function useEnvironmentConnectionState(environmentId: EnvironmentId) { |
| 56 | return useEnvironmentQuery(environmentCatalog.stateAtom(environmentId)); |
| 57 | } |