Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type EnvironmentTheme,
5 type ServerConfig,
6 type ServerConfigStreamEvent,
7 type ServerLifecycleWelcomePayload,
8 type ServerProvider,
9 type ServerSettings,
10} from "@t3tools/contracts";
11import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
12import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
13import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
14import * as Option from "effect/Option";
15import { AsyncResult, Atom } from "effect/unstable/reactivity";
16
17import { environmentCatalog } from "../connection/catalog";
18import { connectionAtomRuntime } from "../connection/runtime";
19import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
20import { environmentSession } from "./session";
21
22// Opted in for every environment, not just the primary one. Only the primary
23// environment's themes are rendered, but which environment is primary changes
24// at runtime and the subscription payload is fixed when it is established --
25// gating on "primary right now" would leave a newly promoted environment with
26// no themes until it reconnected. The set is capped server-side and stripped
27// before caching, so following all of them costs a few KB per environment.
28export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
29 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
30 environmentThemes: true,
31 usageLimitSources: true,
32 usageLimitsCommand: true,
33});
34export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
35 catalogValueAtom: environmentCatalog.catalogValueAtom,
36 serverConfigValueAtom: serverEnvironment.configValueAtom,
37});
38
39interface PrimaryServerState {
40 readonly config: ServerConfig | null;
41 readonly latestEvent: ServerConfigStreamEvent | null;
42 readonly welcome: ServerLifecycleWelcomePayload | null;
43}
44
45const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
46export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
47const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
48 config: null,
49 latestEvent: null,
50 welcome: null,
51};
52
53const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
54 const environmentId = get(primaryEnvironmentIdAtom);
55 if (environmentId === null) {
56 return EMPTY_PRIMARY_SERVER_STATE;
57 }
58
59 const target = { environmentId, input: {} };
60 const configProjection = Option.getOrNull(
61 AsyncResult.value(get(serverEnvironment.configProjection(target))),
62 );
63 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
64
65 return {
66 config: get(serverEnvironment.configValueAtom(environmentId)),
67 latestEvent: configProjection?.latestEvent ?? null,
68 welcome,
69 };
70}).pipe(Atom.withLabel("web-primary-server-state"));
71
72export const primaryServerConfigAtom = Atom.make(
73 (get): ServerConfig | null => get(primaryServerStateAtom).config,
74).pipe(Atom.withLabel("web-primary-server-config"));
75
76export const primaryServerConfigEventAtom = Atom.make(
77 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
78).pipe(Atom.withLabel("web-primary-server-config-event"));
79
80export const primaryServerWelcomeAtom = Atom.make(
81 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
82).pipe(Atom.withLabel("web-primary-server-welcome"));
83
84export const primaryServerSettingsAtom = Atom.make(
85 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
86).pipe(Atom.withLabel("web-primary-server-settings"));
87
88export const primaryServerProvidersAtom = Atom.make(
89 (get): ReadonlyArray<ServerProvider> =>
90 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
91).pipe(Atom.withLabel("web-primary-server-providers"));
92
93export const primaryServerKeybindingsAtom = Atom.make(
94 (get): ServerConfig["keybindings"] =>
95 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
96).pipe(Atom.withLabel("web-primary-server-keybindings"));
97
98export const primaryServerAvailableEditorsAtom = Atom.make(
99 (get): ReadonlyArray<EditorId> =>
100 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
101).pipe(Atom.withLabel("web-primary-server-available-editors"));
102
103export const primaryServerKeybindingsConfigPathAtom = Atom.make(
104 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
105).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
106
107const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
108
109/**
110 * Palettes published by the primary environment's machine. Only the primary
111 * environment: a client follows the machine it is anchored to, not every
112 * environment it happens to be connected to.
113 */
114export const primaryServerEnvironmentThemesAtom = Atom.make(
115 (get): ReadonlyArray<EnvironmentTheme> =>
116 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
117).pipe(Atom.withLabel("web-primary-server-environment-themes"));
118
119export const primaryServerObservabilityAtom = Atom.make(
120 (get): ServerConfig["observability"] | null =>
121 get(primaryServerConfigAtom)?.observability ?? null,
122).pipe(Atom.withLabel("web-primary-server-observability"));