Open History to see the last commit
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, HostPressure, ServerConfig } from "@t3tools/contracts";
8import * as Option from "effect/Option";
9import { Atom } from "effect/reactivity";
10import { shallow } from "zustand/shallow";
11
12import { environmentCatalog } from "../connection/catalog";
13import { environmentServerConfigsAtom, serverEnvironment } from "./server";
14import { expectsPrimaryEnvironment } from "./shell";
15
16const presentationConfigAtom = Atom.family((environmentId: EnvironmentId) =>
17 Atom.make((get) => {
18 const config = get(serverEnvironment.configValueAtom(environmentId));
19 if (config === null) return null;
20 const { hostPressure: _hostPressure, ...presentation } = config;
21 const previous = Option.getOrNull(get.self<Omit<ServerConfig, "hostPressure"> | null>());
22 return previous !== null && shallow(previous, presentation) ? previous : presentation;
23 }).pipe(Atom.withLabel(`web-environment-presentation-config:${environmentId}`)),
24);
25
26export const environmentPresentations = createEnvironmentPresentationAtoms({
27 catalogValueAtom: environmentCatalog.catalogValueAtom,
28 stateAtom: environmentCatalog.stateAtom,
29 serverConfigValueAtom: presentationConfigAtom,
30});
31
32// Host telemetry replaces the config on every sample. Only pressure widgets read the samples.
33export const environmentHostPressureAtom = Atom.make((get) => {
34 const next = new Map<EnvironmentId, HostPressure>();
35 for (const [environmentId, config] of get(environmentServerConfigsAtom)) {
36 if (config.hostPressure !== undefined) next.set(environmentId, config.hostPressure);
37 }
38 const previous = Option.getOrNull(get.self<ReadonlyMap<EnvironmentId, HostPressure>>());
39 return previous !== null && shallow(previous, next) ? previous : next;
40}).pipe(Atom.withLabel("web-environment-host-pressure"));
41
42export const environmentSummaries = createEnvironmentSummaryAtoms({
43 catalogValueAtom: environmentCatalog.catalogValueAtom,
44 presentationAtom: environmentPresentations.presentationAtom,
45});
46
47const EMPTY_ENVIRONMENT_PRESENTATION_ATOM = Atom.make<EnvironmentPresentation | null>(null).pipe(
48 Atom.withLabel("web-environment-presentation:empty"),
49);
50
51/** The catalog has loaded without a usable connection to the environment (as `enabledEnvironmentIds` counts them). */
52export const environmentUnreachableAtom = Atom.family((environmentId: EnvironmentId | null) =>
53 Atom.make((get) => {
54 if (environmentId === null) return false;
55 const catalog = get(environmentCatalog.catalogValueAtom);
56 if (!catalog.isReady) return false;
57 // The catalog is ready before primary discovery lands; until then it cannot prove absence.
58 if (
59 expectsPrimaryEnvironment() &&
60 !Array.from(catalog.entries.values()).some(
61 (entry) => entry.target._tag === "PrimaryConnectionTarget",
62 )
63 ) {
64 return false;
65 }
66 const entry = catalog.entries.get(environmentId);
67 return !entry?.enabled || entry.unsupportedReason !== undefined;
68 }),
69);
70
71export function useEnvironmentPresentation(environmentId: EnvironmentId | null) {
72 const catalog = useAtomValue(environmentCatalog.catalogValueAtom);
73 const presentation = useAtomValue(
74 environmentId === null
75 ? EMPTY_ENVIRONMENT_PRESENTATION_ATOM
76 : environmentPresentations.presentationAtom(environmentId),
77 );
78 return {
79 isReady: catalog.isReady,
80 presentation,
81 };
82}