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

ProviderUpdateLaunchNotification.environments.ts

97 lines3.7 KB

1import type { ConnectionCatalogEntry } from "@t3tools/client-runtime/connection";
2import type { ServerConfig } from "@t3tools/contracts";
3import { useMemo } from "react";
4​
5import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
6import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
7import {
8 buildLocalEnvironmentUpdateGroups,
9 deriveEnvironmentDisplayLabel,
10 type EnvironmentUpdateConnectionState,
11 type LocalEnvironmentProvidersInput,
12 type LocalEnvironmentUpdateGroup,
13} from "./ProviderUpdateLaunchNotification.logic";
14​
15/**
16 * A local environment is either the same-origin primary backend or a
17 * desktop-local secondary (the parallel WSL backend), which connects over
18 * loopback with a bearer token and carries a `local:<backendInstanceId>`
19 * connection id. SSH, relay, and other remote targets are excluded.
20 */
21function isLocalConnectionTarget(target: ConnectionCatalogEntry["target"]): boolean {
22 return target._tag === "PrimaryConnectionTarget" || isDesktopLocalConnectionTarget(target);
24​
25function normalizeConnectionState(phase: string | undefined): EnvironmentUpdateConnectionState {
26 switch (phase) {
27 case "connected":
28 return "ready";
29 case "connecting":
30 case "reconnecting":
31 return "connecting";
32 case "error":
33 return "error";
34 case "offline":
35 return "disconnected";
36 default:
37 // "available" (or anything not yet observed) — the backend has not
38 // confirmed it is serving yet, so treat it as still settling so the
39 // popover waits for it.
40 return "connecting";
41 }
43​
44/**
45 * Reactively enumerate the enabled local environments (the primary plus any
46 * desktop-local secondary such as WSL) with each one's full provider list and a
47 * flag for whether any is still connecting. Drives the launch popover's gating
48 * and its per-environment update triggers.
49 */
50export function useLocalEnvironmentUpdateGroups(): {
51 readonly groups: LocalEnvironmentUpdateGroup[];
52 readonly isAnySettling: boolean;
53} {
54 const { environments } = useEnvironments();
55 const primaryEnvironmentId = usePrimaryEnvironmentId();
56​
57 return useMemo(() => {
58 const inputs: LocalEnvironmentProvidersInput[] = [];
59​
60 for (const environment of environments) {
61 if (!isLocalConnectionTarget(environment.entry.target)) {
62 continue;
63 }
64​
65 const isPrimary = environment.environmentId === primaryEnvironmentId;
66 const serverConfig: ServerConfig | null = environment.serverConfig;
67​
68 inputs.push({
69 environmentId: environment.environmentId,
70 // Secondaries carry a meaningful label straight from the platform source
71 // (e.g. "WSL (Ubuntu)"). The primary's catalog label can be the account
72 // name, so fall back to its platform OS so the row reads "Windows"/"Linux".
73 label: isPrimary
74 ? deriveEnvironmentDisplayLabel({
75 isWsl: false,
76 wslDistro: null,
77 platformOs: serverConfig?.environment.platform.os,
78 fallbackLabel: environment.label,
79 })
80 : environment.label,
81 isPrimary,
82 // The primary is the backend serving this renderer, so it is ready
83 // whenever its providers are available; secondaries report their live
84 // connection phase.
85 connectionState: isPrimary
86 ? "ready"
87 : normalizeConnectionState(environment.connection.phase),
88 providers: serverConfig?.providers ?? [],
89 });
90 }
91​
92 // Primary first, then the rest in catalog order.
93 inputs.sort((left, right) => Number(right.isPrimary) - Number(left.isPrimary));
94​
95 return buildLocalEnvironmentUpdateGroups(inputs);
96 }, [environments, primaryEnvironmentId]);