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