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

presentation.ts

221 lines8.0 KB

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/unstable/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";
18​
19function mapsEqual<K, V>(left: ReadonlyMap<K, V>, right: ReadonlyMap<K, V>): boolean {
20 if (left.size !== right.size) {
21 return false;
22 }
23 for (const [key, value] of left) {
24 if (right.get(key) !== value) {
25 return false;
26 }
27 }
28 return true;
30​
31export function createEnvironmentPresentationAtoms<E>(input: {
32 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
33 readonly stateAtom: (
34 environmentId: EnvironmentId,
35 ) => Atom.Atom<AsyncResult.AsyncResult<SupervisorConnectionState, E>>;
36 /** Authoritative live server config, including streamed provider/settings updates. */
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 };
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;
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: environment.entry.target._tag === "RelayConnectionTarget",
102 isEnabled: environment.entry.enabled,
103 connectionState: environment.connection.phase,
104 connectionError: environment.connection.error,
105 connectionErrorTraceId: environment.connection.traceId,
106 };
108​
109// Keep list membership and connection chrome independent of provider/config refreshes.
110export function createEnvironmentSummaryAtoms(input: {
111 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
112 readonly presentationAtom: (
113 environmentId: EnvironmentId,
114 ) => Atom.Atom<EnvironmentPresentation | null>;
115}) {
116 const environmentIdsAtom = Atom.make((get) => {
117 const next = [...get(input.catalogValueAtom).entries.keys()];
118 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>());
119 return previous !== null &&
120 previous.length === next.length &&
121 next.every((id, index) => id === previous[index])
122 ? previous
123 : next;
124 });
125 const identitiesAtom = Atom.make((get) => {
126 const next = [...get(input.catalogValueAtom).entries].map(([environmentId, entry]) => ({
127 environmentId,
128 label: entry.target.label,
129 }));
130 const previous = Option.getOrNull(
131 get.self<ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }>>(),
132 );
133 return previous !== null &&
134 previous.length === next.length &&
135 next.every(
136 (value, index) =>
137 value.environmentId === previous[index]?.environmentId &&
138 value.label === previous[index]?.label,
139 )
140 ? previous
141 : next;
142 });
143 const environmentAtom = Atom.family((environmentId: EnvironmentId) =>
144 Atom.make((get) => {
145 const presentation = get(input.presentationAtom(environmentId));
146 if (presentation === null) return null;
147 const next = projectEnvironmentConnectionSummary(environmentId, presentation);
148 const previous = Option.getOrNull(get.self<EnvironmentConnectionSummary | null>());
149 // Provider refreshes and transport heartbeats do not change connection UI.
150 if (
151 previous !== null &&
152 previous.environmentId === next.environmentId &&
153 previous.environmentLabel === next.environmentLabel &&
154 previous.displayUrl === next.displayUrl &&
155 previous.isRelayManaged === next.isRelayManaged &&
156 previous.isEnabled === next.isEnabled &&
157 previous.connectionState === next.connectionState &&
158 previous.connectionError === next.connectionError &&
159 previous.connectionErrorTraceId === next.connectionErrorTraceId
160 )
161 return previous;
162 return next;
163 }),
164 );
165 const environmentsAtom = Atom.make((get) => {
166 const next: Array<EnvironmentConnectionSummary> = [];
167 for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
168 const environment = get(environmentAtom(environmentId));
169 if (environment !== null) next.push(environment);
170 }
171 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentConnectionSummary>>());
172 return previous !== null &&
173 previous.length === next.length &&
174 next.every((value, index) => value === previous[index])
175 ? previous
176 : next;
177 }).pipe(Atom.withLabel("environment-connection-summaries"));
178 const connectedEnvironmentIdsAtom = Atom.make((get) => {
179 const next = get(environmentIdsAtom).filter(
180 (id) => get(environmentAtom(id))?.connectionState === "connected",
181 );
182 const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>());
183 return previous !== null &&
184 previous.length === next.length &&
185 next.every((id, index) => id === previous[index])
186 ? previous
187 : next;
188 });
189 const machineByIdAtom = Atom.make((get) => {
190 const next = new Map(
191 get(environmentIdsAtom).map(
192 (environmentId) =>
193 [
194 environmentId,
195 resolveEnvironmentMachineKind(
196 get(input.presentationAtom(environmentId))?.serverConfig ?? null,
197 ),
198 ] as const,
199 ),
200 );
201 const previous = Option.getOrNull(
202 get.self<ReadonlyMap<EnvironmentId, EnvironmentMachineKind>>(),
203 );
204 return previous !== null && mapsEqual(previous, next) ? previous : next;
205 });
206 const pullRequestsSupportedAtom = Atom.make((get) =>
207 get(environmentIdsAtom).some(
208 (environmentId) =>
209 get(input.presentationAtom(environmentId))?.serverConfig?.environment.capabilities
210 .pullRequests === true,
211 ),
212 );
213 return {
214 environmentIdsAtom,
215 connectedEnvironmentIdsAtom,
216 identitiesAtom,
217 environmentsAtom,
218 machineByIdAtom,
219 pullRequestsSupportedAtom,
220 };