Open History to see the last commit
1import { type EnvironmentShellSummary } from "@t3tools/client-runtime/state/shell";
2import { type NetworkStatus } from "@t3tools/client-runtime/connection";
3import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
4import type { EnvironmentId, ServerConfig } from "@t3tools/contracts";
5
6import type { EnvironmentPresentation } from "./environments";
7
8export interface WorkspaceEnvironment {
9 readonly environmentId: EnvironmentId;
10 readonly environmentLabel: string;
11 readonly displayUrl: string;
12 readonly isRelayManaged: boolean;
13 readonly isEnabled: boolean;
14 readonly connectionState: EnvironmentConnectionPhase;
15 readonly connectionError: string | null;
16 readonly connectionErrorTraceId: string | null;
17}
18
19export interface WorkspaceState {
20 readonly isLoadingConnections: boolean;
21 readonly hasConnections: boolean;
22 readonly hasLoadedShellSnapshot: boolean;
23 readonly hasPendingShellSnapshot: boolean;
24 readonly hasReadyEnvironment: boolean;
25 readonly hasConnectingEnvironment: boolean;
26 readonly connectingEnvironments: ReadonlyArray<WorkspaceEnvironment>;
27 readonly connectionState: EnvironmentConnectionPhase;
28 readonly connectionError: string | null;
29 readonly shellSnapshotError: string | null;
30 readonly latestCachedSnapshotReceivedAt: string | null;
31 readonly networkStatus: NetworkStatus;
32}
33
34export function projectWorkspaceEnvironment(
35 environment: EnvironmentPresentation,
36): WorkspaceEnvironment {
37 return {
38 environmentId: environment.environmentId,
39 environmentLabel: environment.label,
40 displayUrl: environment.displayUrl ?? "",
41 isRelayManaged: environment.relayManaged,
42 isEnabled: environment.entry.enabled,
43 connectionState: environment.connection.phase,
44 connectionError: environment.connection.error,
45 connectionErrorTraceId: environment.connection.traceId,
46 };
47}
48
49function overallConnectionState(
50 environments: ReadonlyArray<WorkspaceEnvironment>,
51 networkStatus: NetworkStatus,
52): EnvironmentConnectionPhase {
53 if (environments.length === 0) {
54 return "available";
55 }
56 if (networkStatus === "offline") {
57 return "offline";
58 }
59 if (environments.some((environment) => environment.connectionState === "connected")) {
60 return "connected";
61 }
62 if (environments.some((environment) => environment.connectionState === "reconnecting")) {
63 return "reconnecting";
64 }
65 if (environments.some((environment) => environment.connectionState === "connecting")) {
66 return "connecting";
67 }
68 if (environments.some((environment) => environment.connectionState === "error")) {
69 return "error";
70 }
71 if (environments.some((environment) => environment.connectionState === "offline")) {
72 return "offline";
73 }
74 return "available";
75}
76
77export function projectWorkspaceState(input: {
78 readonly isReady: boolean;
79 readonly networkStatus: NetworkStatus;
80 readonly environments: ReadonlyArray<WorkspaceEnvironment>;
81 readonly shellSummary: EnvironmentShellSummary;
82}): WorkspaceState {
83 // Switched-off environments still count as saved connections, but they do
84 // not drive the overall connection state or surface their last error.
85 const activeEnvironments = input.environments.filter((environment) => environment.isEnabled);
86 const connectingEnvironments = activeEnvironments.filter(
87 (environment) =>
88 environment.connectionState === "connecting" ||
89 environment.connectionState === "reconnecting",
90 );
91
92 return {
93 isLoadingConnections: !input.isReady,
94 hasConnections: input.environments.length > 0,
95 hasLoadedShellSnapshot: input.shellSummary.hasSnapshot,
96 hasPendingShellSnapshot: input.shellSummary.hasSynchronizingShell,
97 hasReadyEnvironment:
98 input.networkStatus !== "offline" &&
99 activeEnvironments.some((environment) => environment.connectionState === "connected"),
100 hasConnectingEnvironment: connectingEnvironments.length > 0,
101 connectingEnvironments,
102 connectionState: overallConnectionState(activeEnvironments, input.networkStatus),
103 connectionError:
104 activeEnvironments.find((environment) => environment.connectionError !== null)
105 ?.connectionError ?? null,
106 shellSnapshotError: input.shellSummary.firstError,
107 latestCachedSnapshotReceivedAt: input.shellSummary.latestSnapshotUpdatedAt,
108 networkStatus: input.networkStatus,
109 };
110}
111
112export type ServerConfigByEnvironmentId = ReadonlyMap<EnvironmentId, ServerConfig>;