Open History to see the last commit
1import {
2 AVAILABLE_CONNECTION_STATE,
3 connectionProjectionPhase,
4} from "@t3tools/client-runtime/connection";
5import {
6 createEnvironmentShellAtoms,
7 createEnvironmentSnapshotAtom,
8 createShellEnvironmentAtoms,
9 type EnvironmentShellState,
10 type EnvironmentShellStatus,
11} from "@t3tools/client-runtime/state/shell";
12import {
13 type EnvironmentCatalogState,
14 enabledEnvironmentIds,
15} from "@t3tools/client-runtime/state/connections";
16import type { EnvironmentId } from "@t3tools/contracts";
17import { useAtomValue } from "@effect/atom-react";
18import * as Option from "effect/Option";
19import { AsyncResult, Atom } from "effect/reactivity";
20
21import { environmentCatalog } from "../connection/catalog";
22import { connectionAtomRuntime } from "../connection/runtime";
23import { isHostedStaticApp } from "../hostedPairing";
24
25export const shellEnvironment = createShellEnvironmentAtoms(connectionAtomRuntime);
26export const environmentShell = createEnvironmentShellAtoms(connectionAtomRuntime);
27export const environmentSnapshotAtom = createEnvironmentSnapshotAtom(environmentShell.stateAtom);
28
29const NO_SHELL_STATUS_ATOM = Atom.make<EnvironmentShellStatus | null>(null).pipe(
30 Atom.withLabel("web-environment-shell-status:none"),
31);
32const NO_SHELL_SNAPSHOT_ATOM = Atom.make(false).pipe(
33 Atom.withLabel("web-environment-shell-has-snapshot:none"),
34);
35
36export function useEnvironmentShellStatus(
37 environmentId: EnvironmentId | null,
38): EnvironmentShellStatus | null {
39 return useAtomValue(
40 environmentId === null ? NO_SHELL_STATUS_ATOM : environmentShell.statusAtom(environmentId),
41 );
42}
43
44export function useEnvironmentShellHasSnapshot(environmentId: EnvironmentId | null): boolean {
45 return useAtomValue(
46 environmentId === null
47 ? NO_SHELL_SNAPSHOT_ATOM
48 : environmentShell.hasSnapshotAtom(environmentId),
49 );
50}
51
52let previousLiveEnvironmentIds: ReadonlySet<EnvironmentId> = new Set();
53/** Environments whose threads reflect the server now, not a cache or a resync in flight. */
54export const liveShellEnvironmentIdsAtom = Atom.make((get) => {
55 const next = new Set<EnvironmentId>();
56 for (const environmentId of enabledEnvironmentIds(get(environmentCatalog.catalogValueAtom))) {
57 if (get(environmentShell.statusAtom(environmentId)) === "live") next.add(environmentId);
58 }
59 if (
60 next.size === previousLiveEnvironmentIds.size &&
61 [...next].every((environmentId) => previousLiveEnvironmentIds.has(environmentId))
62 ) {
63 return previousLiveEnvironmentIds;
64 }
65 previousLiveEnvironmentIds = next;
66 return next;
67}).pipe(Atom.withLabel("web-live-shell-environment-ids"));
68
69export const allEnvironmentShellsBootstrappedAtom = Atom.make((get) => {
70 const catalog = AsyncResult.value(get(environmentCatalog.catalogAtom));
71 if (Option.isNone(catalog)) {
72 return false;
73 }
74 for (const environmentId of enabledEnvironmentIds(catalog.value)) {
75 if (Option.isSome(get(environmentShell.stateValueAtom(environmentId)).snapshot)) {
76 continue;
77 }
78 const connection = Option.getOrElse(
79 AsyncResult.value(get(environmentCatalog.stateAtom(environmentId))),
80 () => AVAILABLE_CONNECTION_STATE,
81 );
82 if (connectionProjectionPhase(connection) !== "disconnected") {
83 return false;
84 }
85 if (connection.phase === "backoff" && connection.desired && connection.attempt <= 2) {
86 return false;
87 }
88 }
89 return true;
90}).pipe(Atom.withLabel("web-all-environment-shells-bootstrapped"));
91
92/**
93 * The persisted catalog can emit before platform discovery registers the primary environment.
94 * Neither that gap nor an empty catalog proves a project is absent.
95 */
96function catalogIsComplete(
97 catalog: EnvironmentCatalogState,
98 requiresPrimaryEnvironment: boolean,
99): boolean {
100 if (!catalog.isReady || catalog.entries.size === 0) return false;
101 return (
102 !requiresPrimaryEnvironment ||
103 Array.from(catalog.entries.values()).some(
104 (entry) => entry.target._tag === "PrimaryConnectionTarget",
105 )
106 );
107}
108
109export function createAllEnvironmentProjectSnapshotsReadyAtom(input: {
110 readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>;
111 readonly shellStateValueAtom: (environmentId: EnvironmentId) => Atom.Atom<EnvironmentShellState>;
112 readonly requiresPrimaryEnvironment: boolean;
113}) {
114 return Atom.make((get) => {
115 const catalog = get(input.catalogValueAtom);
116 if (!catalogIsComplete(catalog, input.requiresPrimaryEnvironment)) return false;
117 for (const environmentId of enabledEnvironmentIds(catalog)) {
118 const shell = get(input.shellStateValueAtom(environmentId));
119 if (shell.status !== "live" || Option.isNone(shell.snapshot)) return false;
120 }
121 return true;
122 }).pipe(Atom.withLabel("web-all-environment-project-snapshots-ready"));
123}
124
125export const allEnvironmentProjectSnapshotsReadyAtom =
126 createAllEnvironmentProjectSnapshotsReadyAtom({
127 catalogValueAtom: environmentCatalog.catalogValueAtom,
128 shellStateValueAtom: environmentShell.stateValueAtom,
129 requiresPrimaryEnvironment: !isHostedStaticApp(),
130 });
131
132export const allReachableShellsLiveAtom = Atom.make((get) => {
133 const catalog = get(environmentCatalog.catalogValueAtom);
134 if (!catalogIsComplete(catalog, !isHostedStaticApp())) return false;
135 for (const environmentId of enabledEnvironmentIds(catalog)) {
136 const shell = get(environmentShell.stateValueAtom(environmentId));
137 if (shell.status === "live" && Option.isSome(shell.snapshot)) continue;
138 const state = AsyncResult.value(get(environmentCatalog.stateAtom(environmentId)));
139 if (Option.isNone(state)) return false;
140 const connection = state.value;
141 const settledOffline =
142 connection.phase === "offline" ||
143 connection.phase === "blocked" ||
144 (connection.phase === "backoff" && connection.attempt > 2);
145 if (!settledOffline) return false;
146 }
147 return true;
148}).pipe(Atom.withLabel("web-all-reachable-shells-live"));