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

ProviderUpdateLaunchNotification.environments.ts

86 lines3.1 KB

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