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// Only the primary environment's themes are rendered, but which environment is primary changes
25// at runtime and the subscription payload is fixed when it is established -- gating on "primary
26// right now" would leave a newly promoted environment with no themes until it reconnected.
27export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
28 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
29 environmentThemes: true,
30 usageLimitSources: true,
31 usageLimitsCommand: true,
32});
33export const updateOutdatedServer = createOutdatedServerUpdateCommand(connectionAtomRuntime);
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 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((get): ServerConfig["keybindings"] =>
106 mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
107).pipe(Atom.withLabel("web-primary-server-keybindings"));
108
109export const primaryServerAvailableEditorsAtom = Atom.make(
110 (get): ReadonlyArray<EditorId> =>
111 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
112).pipe(Atom.withLabel("web-primary-server-available-editors"));
113
114const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
115
116export const primaryServerEnvironmentThemesAtom = Atom.make(
117 (get): ReadonlyArray<EnvironmentTheme> =>
118 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
119).pipe(Atom.withLabel("web-primary-server-environment-themes"));