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

workspaceModel.ts

115 lines4.4 KB

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;
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;
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 };
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 === "unsupported")) {
69 return "unsupported";
70 }
71 if (environments.some((environment) => environment.connectionState === "error")) {
72 return "error";
73 }
74 if (environments.some((environment) => environment.connectionState === "offline")) {
75 return "offline";
76 }
77 return "available";
79​
80export function projectWorkspaceState(input: {
81 readonly isReady: boolean;
82 readonly networkStatus: NetworkStatus;
83 readonly environments: ReadonlyArray<WorkspaceEnvironment>;
84 readonly shellSummary: EnvironmentShellSummary;
85}): WorkspaceState {
86 // Switched-off environments still count as saved connections, but they do
87 // not drive the overall connection state or surface their last error.
88 const activeEnvironments = input.environments.filter((environment) => environment.isEnabled);
89 const connectingEnvironments = activeEnvironments.filter(
90 (environment) =>
91 environment.connectionState === "connecting" ||
92 environment.connectionState === "reconnecting",
93 );
94​
95 return {
96 isLoadingConnections: !input.isReady,
97 hasConnections: input.environments.length > 0,
98 hasLoadedShellSnapshot: input.shellSummary.hasSnapshot,
99 hasPendingShellSnapshot: input.shellSummary.hasSynchronizingShell,
100 hasReadyEnvironment:
101 input.networkStatus !== "offline" &&
102 activeEnvironments.some((environment) => environment.connectionState === "connected"),
103 hasConnectingEnvironment: connectingEnvironments.length > 0,
104 connectingEnvironments,
105 connectionState: overallConnectionState(activeEnvironments, input.networkStatus),
106 connectionError:
107 activeEnvironments.find((environment) => environment.connectionError !== null)
108 ?.connectionError ?? null,
109 shellSnapshotError: input.shellSummary.firstError,
110 latestCachedSnapshotReceivedAt: input.shellSummary.latestSnapshotUpdatedAt,
111 networkStatus: input.networkStatus,
112 };
114​
115export type ServerConfigByEnvironmentId = ReadonlyMap<EnvironmentId, ServerConfig>;