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 ServerLifecycleLegacyThreadMigrationPayload,
8 type ServerLifecycleWelcomePayload,
9 type ServerProvider,
10 type ServerSettings,
11} from "@t3tools/contracts";
12import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
13import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
14import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
15import * as Option from "effect/Option";
16import { AsyncResult, Atom } from "effect/unstable/reactivity";
17
18import { environmentCatalog } from "../connection/catalog";
19import { connectionAtomRuntime } from "../connection/runtime";
20import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
21import { environmentSession } from "./session";
22
23// Opted in for every environment, not just the primary one. Only the primary
24// environment's themes are rendered, but which environment is primary changes
25// at runtime and the subscription payload is fixed when it is established --
26// gating on "primary right now" would leave a newly promoted environment with
27// no themes until it reconnected. The set is capped server-side and stripped
28// before caching, so following all of them costs a few KB per environment.
29export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
30 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
31 environmentThemes: true,
32 usageLimitSources: 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
53export const 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 primaryServerLegacyThreadMigrationAtom = Atom.make(
85 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
86 const environmentId = get(primaryEnvironmentIdAtom);
87 if (environmentId === null) {
88 return null;
89 }
90 return Option.getOrNull(
91 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
92 );
93 },
94).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
95
96export const primaryServerSettingsAtom = Atom.make(
97 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
98).pipe(Atom.withLabel("web-primary-server-settings"));
99
100export const primaryServerProvidersAtom = Atom.make(
101 (get): ReadonlyArray<ServerProvider> =>
102 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
103).pipe(Atom.withLabel("web-primary-server-providers"));
104
105export const primaryServerKeybindingsAtom = Atom.make(
106 (get): ServerConfig["keybindings"] =>
107 get(primaryServerConfigAtom)?.keybindings ?? DEFAULT_RESOLVED_KEYBINDINGS,
108).pipe(Atom.withLabel("web-primary-server-keybindings"));
109
110export const primaryServerAvailableEditorsAtom = Atom.make(
111 (get): ReadonlyArray<EditorId> =>
112 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
113).pipe(Atom.withLabel("web-primary-server-available-editors"));
114
115export const primaryServerKeybindingsConfigPathAtom = Atom.make(
116 (get): string | null => get(primaryServerConfigAtom)?.keybindingsConfigPath ?? null,
117).pipe(Atom.withLabel("web-primary-server-keybindings-config-path"));
118
119const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
120
121/**
122 * Palettes published by the primary environment's machine. Only the primary
123 * environment: a client follows the machine it is anchored to, not every
124 * environment it happens to be connected to.
125 */
126export const primaryServerEnvironmentThemesAtom = Atom.make(
127 (get): ReadonlyArray<EnvironmentTheme> =>
128 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
129).pipe(Atom.withLabel("web-primary-server-environment-themes"));
130
131export const primaryServerObservabilityAtom = Atom.make(
132 (get): ServerConfig["observability"] | null =>
133 get(primaryServerConfigAtom)?.observability ?? null,
134).pipe(Atom.withLabel("web-primary-server-observability"));