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

environments.ts

86 lines2.9 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 connectionCatalogDisplayUrl,
4 type EnvironmentPresentation as BaseEnvironmentPresentation,
5} from "@t3tools/client-runtime/connection";
6import { Discovery } from "@t3tools/client-runtime/relay";
7import type { EnvironmentId } from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import { useMemo } from "react";
10​
11import { environmentCatalog } from "../connection/catalog";
12import { environmentPresentations, useEnvironmentPresentation } from "./presentation";
13import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
14import { relayEnvironmentDiscovery } from "./relay";
15import { usePreparedConnection } from "./session";
16​
17export interface EnvironmentPresentation extends BaseEnvironmentPresentation {
18 readonly environmentId: EnvironmentId;
19 readonly label: string;
20 readonly displayUrl: string | null;
21 readonly relayManaged: boolean;
23​
24function projectEnvironmentPresentation(
25 environmentId: EnvironmentId,
26 presentation: BaseEnvironmentPresentation,
27): EnvironmentPresentation {
28 return {
29 ...presentation,
30 environmentId,
31 label: presentation.entry.target.label,
32 displayUrl: connectionCatalogDisplayUrl(presentation.entry),
33 relayManaged: presentation.entry.target._tag === "RelayConnectionTarget",
34 };
36​
37export function useEnvironments() {
38 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
39 const networkStatus = useAtomValue(environmentCatalog.networkStatusValueAtom);
40 const presentationById = useAtomValue(environmentPresentations.presentationsAtom);
41​
42 const environments = useMemo(
43 () =>
44 [...presentationById.entries()].map(([environmentId, presentation]) =>
45 projectEnvironmentPresentation(environmentId, presentation),
46 ),
47 [presentationById],
48 );
49​
50 return {
51 isReady: catalog.isReady,
52 networkStatus,
53 environments,
54 presentationById,
55 };
57​
58export function usePrimaryEnvironmentId(): EnvironmentId | null {
59 return useAtomValue(primaryEnvironmentIdAtom);
61​
62export function useEnvironment(
63 environmentId: EnvironmentId | null,
64): EnvironmentPresentation | null {
65 const { presentation } = useEnvironmentPresentation(environmentId);
66 return useMemo(
67 () =>
68 environmentId === null || presentation === null
69 ? null
70 : projectEnvironmentPresentation(environmentId, presentation),
71 [environmentId, presentation],
72 );
74​
75export function usePrimaryEnvironment(): EnvironmentPresentation | null {
76 return useEnvironment(usePrimaryEnvironmentId());
78​
79export function useEnvironmentHttpBaseUrl(environmentId: EnvironmentId | null): string | null {
80 const prepared = usePreparedConnection(environmentId);
81 return Option.isSome(prepared) ? prepared.value.httpBaseUrl : null;
83​
84export function useRelayEnvironmentDiscovery(): Discovery.RelayEnvironmentDiscoveryState {
85 return useAtomValue(relayEnvironmentDiscovery.stateValueAtom);