Open History to see the last commit
1import {
2 resolveEnvironmentMachineKind,
3 type EnvironmentMachineKind,
4 type EnvironmentId,
5 type ServerConfig,
6} from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import { AsyncResult, Atom } from "effect/reactivity";
9
10import { AVAILABLE_CONNECTION_STATE, type SupervisorConnectionState } from "../connection/model.ts";
11import {
12 connectionCatalogDisplayUrl,
13 presentEnvironmentConnection,
14 type EnvironmentConnectionPhase,
15 type EnvironmentPresentation,
16} from "../connection/presentation.ts";
17import type { EnvironmentCatalogState } from "./connections.ts";
18import { hasRelayRoute } from "../connection/routes.ts";
19
20function mapsEqual<K, V>(left: ReadonlyMap<K, V>, right: ReadonlyMap<K, V>): boolean {
21 if (left.size !== right.size) {
22 return false;
23 }
24 for (const [key, value] of left) {
25 if (right.get(key) !== value) {
26 return false;
27 }
28 }
29 return true;
30}
31
32export function createEnvironmentPresentationAtoms<E>(input: {
33 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
34 readonly stateAtom: (
35 environmentId: EnvironmentId,
36 ) => Atom.Atom<AsyncResult.AsyncResult<SupervisorConnectionState, E>>;
37 readonly serverConfigValueAtom: (environmentId: EnvironmentId) => Atom.Atom<ServerConfig | null>;
38}) {
39 const presentationAtom = Atom.family((environmentId: EnvironmentId) =>
40 Atom.make((get) => {
41 const entry = get(input.catalogValueAtom).entries.get(environmentId);
42 if (entry === undefined) {
43 return null;
44 }
45 const state = Option.getOrElse(
46 AsyncResult.value(get(input.stateAtom(environmentId))),
47 () => AVAILABLE_CONNECTION_STATE,
48 );
49 return {
50 entry,
51 connection:
52 entry.unsupportedReason === undefined
53 ? presentEnvironmentConnection(state)
54 : { phase: "unsupported", error: entry.unsupportedReason, traceId: null },
55 serverConfig: get(input.serverConfigValueAtom(environmentId)),
56 } satisfies EnvironmentPresentation;
57 }).pipe(Atom.withLabel(`environment-presentation:${environmentId}`)),
58 );
59
60 let previous: ReadonlyMap<EnvironmentId, EnvironmentPresentation> = new Map();
61 const presentationsAtom = Atom.make((get) => {
62 const next = new Map<EnvironmentId, EnvironmentPresentation>();
63 for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
64 const presentation = get(presentationAtom(environmentId));
65 if (presentation !== null) {
66 next.set(environmentId, presentation);
67 }
68 }
69 if (mapsEqual(previous, next)) {
70 return previous;
71 }
72 previous = next;
73 return previous;
74 }).pipe(Atom.withLabel("environment-presentations"));
75
76 return {
77 presentationAtom,
78 presentationsAtom,
79 };
80}
81
82export interface EnvironmentConnectionSummary {
83 readonly environmentId: EnvironmentId;
84 readonly environmentLabel: string;
85 readonly displayUrl: string;
86 readonly isRelayManaged: boolean;
87 readonly isEnabled: boolean;
88 readonly connectionState: EnvironmentConnectionPhase;
89 readonly connectionError: string | null;
90 readonly connectionErrorTraceId: string | null;
91}
92
93export function projectEnvironmentConnectionSummary(
94 environmentId: EnvironmentId,
95 environment: EnvironmentPresentation,
96): EnvironmentConnectionSummary {
97 return {
98 environmentId,
99 environmentLabel: environment.entry.target.label,
100 displayUrl: connectionCatalogDisplayUrl(environment.entry) ?? "",
101 isRelayManaged: hasRelayRoute(environment.entry),
102 isEnabled: environment.entry.enabled,
103 connectionState: environment.connection.phase,
104 connectionError: environment.connection.error,
105 connectionErrorTraceId: environment.connection.traceId,
106 };
107}
108
109export function createEnvironmentSummaryAtoms(input: {
110 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
111 readonly presentationAtom: (
112 environmentId: EnvironmentId,
113 ) => Atom.Atom<EnvironmentPresentation | null>;
114}) {
115 const environmentIdsAtom = Atom.make((get) => {
116 const next = [...get(input.catalogValueAtom).entries.keys()];
117 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>());
118 return previous !== null &&
119 previous.length === next.length &&
120 next.every((id, index) => id === previous[index])
121 ? previous
122 : next;
123 });
124 const identitiesAtom = Atom.make((get) => {
125 const next = [...get(input.catalogValueAtom).entries].map(([environmentId, entry]) => ({
126 environmentId,
127 label: entry.target.label,
128 }));
129 const previous = Option.getOrNull(
130 get.self<ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }>>(),
131 );
132 return previous !== null &&
133 previous.length === next.length &&
134 next.every(
135 (value, index) =>
136 value.environmentId === previous[index]?.environmentId &&
137 value.label === previous[index]?.label,
138 )
139 ? previous
140 : next;
141 });
142 const environmentAtom = Atom.family((environmentId: EnvironmentId) =>
143 Atom.make((get) => {
144 const presentation = get(input.presentationAtom(environmentId));
145 if (presentation === null) return null;
146 const next = projectEnvironmentConnectionSummary(environmentId, presentation);
147 const previous = Option.getOrNull(get.self<EnvironmentConnectionSummary | null>());
148 if (
149 previous !== null &&
150 previous.environmentId === next.environmentId &&
151 previous.environmentLabel === next.environmentLabel &&
152 previous.displayUrl === next.displayUrl &&
153 previous.isRelayManaged === next.isRelayManaged &&
154 previous.isEnabled === next.isEnabled &&
155 previous.connectionState === next.connectionState &&
156 previous.connectionError === next.connectionError &&
157 previous.connectionErrorTraceId === next.connectionErrorTraceId
158 )
159 return previous;
160 return next;
161 }),
162 );
163 const environmentsAtom = Atom.make((get) => {
164 const next: Array<EnvironmentConnectionSummary> = [];
165 for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
166 const environment = get(environmentAtom(environmentId));
167 if (environment !== null) next.push(environment);
168 }
169 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentConnectionSummary>>());
170 return previous !== null &&
171 previous.length === next.length &&
172 next.every((value, index) => value === previous[index])
173 ? previous
174 : next;
175 }).pipe(Atom.withLabel("environment-connection-summaries"));
176 const connectedEnvironmentIdsAtom = Atom.make((get) => {
177 const next = get(environmentIdsAtom).filter(
178 (id) => get(environmentAtom(id))?.connectionState === "connected",
179 );
180 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>());
181 return previous !== null &&
182 previous.length === next.length &&
183 next.every((id, index) => id === previous[index])
184 ? previous
185 : next;
186 });
187 const machineByIdAtom = Atom.make((get) => {
188 const next = new Map(
189 get(environmentIdsAtom).map(
190 (environmentId) =>
191 [
192 environmentId,
193 resolveEnvironmentMachineKind(
194 get(input.presentationAtom(environmentId))?.serverConfig ?? null,
195 ),
196 ] as const,
197 ),
198 );
199 const previous = Option.getOrNull(
200 get.self<ReadonlyMap<EnvironmentId, EnvironmentMachineKind>>(),
201 );
202 return previous !== null && mapsEqual(previous, next) ? previous : next;
203 });
204 const pullRequestsSupportedAtom = Atom.make((get) =>
205 get(environmentIdsAtom).some(
206 (environmentId) =>
207 get(input.presentationAtom(environmentId))?.serverConfig?.environment.capabilities
208 .pullRequests === true,
209 ),
210 );
211 return {
212 environmentIdsAtom,
213 connectedEnvironmentIdsAtom,
214 identitiesAtom,
215 environmentsAtom,
216 machineByIdAtom,
217 pullRequestsSupportedAtom,
218 };
219}