Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type EditorId,
4 type EnvironmentId,
5 type EnvironmentTheme,
6 type ServerConfig,
7 type ServerConfigStreamEvent,
8 type ServerLifecycleLegacyThreadMigrationPayload,
9 type ServerLifecycleWelcomePayload,
10 type ServerProvider,
11 type ServerSettings,
12} from "@t3tools/contracts";
13import { createServerEnvironmentAtoms } from "@t3tools/client-runtime/state/server";
14import { createOutdatedServerUpdateCommand } from "@t3tools/client-runtime/state/outdatedServerUpdate";
15import { createEnvironmentServerConfigsAtom } from "@t3tools/client-runtime/state/shell";
16import { mergeWithDefaultKeybindings } from "@t3tools/shared/keybindings";
17import * as Option from "effect/Option";
18import { AsyncResult, Atom } from "effect/reactivity";
19
20import { environmentCatalog } from "../connection/catalog";
21import { connectionAtomRuntime } from "../connection/runtime";
22import { environmentAtomList } from "./environmentAtomList";
23import { primaryEnvironmentIdAtom } from "./primaryEnvironment";
24import { environmentSession } from "./session";
25import { reuseProvidersIgnoringCheckedAt } from "./serverProviders.logic";
26
27// Only the primary environment's themes are rendered, but which environment is primary changes
28// at runtime and the subscription payload is fixed when it is established -- gating on "primary
29// right now" would leave a newly promoted environment with no themes until it reconnected.
30export const serverEnvironment = createServerEnvironmentAtoms(connectionAtomRuntime, {
31 initialConfigValueAtom: environmentSession.initialConfigValueAtom,
32 environmentThemes: true,
33 usageLimitSources: true,
34 usageLimitsCommand: true,
35});
36export const environmentUpdateStatesAtom = environmentAtomList(
37 "web-environment-update-states",
38 serverEnvironment.updateStateAtom,
39);
40export const updateOutdatedServer = createOutdatedServerUpdateCommand(connectionAtomRuntime);
41export const environmentServerConfigsAtom = createEnvironmentServerConfigsAtom({
42 catalogValueAtom: environmentCatalog.catalogValueAtom,
43 serverConfigValueAtom: serverEnvironment.configValueAtom,
44});
45
46interface PrimaryServerState {
47 readonly config: ServerConfig | null;
48 readonly latestEvent: ServerConfigStreamEvent | null;
49 readonly welcome: ServerLifecycleWelcomePayload | null;
50}
51
52const EMPTY_AVAILABLE_EDITORS: ReadonlyArray<EditorId> = [];
53export const EMPTY_SERVER_PROVIDERS: ReadonlyArray<ServerProvider> = [];
54const EMPTY_PRIMARY_SERVER_STATE: PrimaryServerState = {
55 config: null,
56 latestEvent: null,
57 welcome: null,
58};
59
60const primaryServerStateAtom = Atom.make((get): PrimaryServerState => {
61 const environmentId = get(primaryEnvironmentIdAtom);
62 if (environmentId === null) {
63 return EMPTY_PRIMARY_SERVER_STATE;
64 }
65
66 const target = { environmentId, input: {} };
67 const configProjection = Option.getOrNull(
68 AsyncResult.value(get(serverEnvironment.configProjection(target))),
69 );
70 const welcome = Option.getOrNull(AsyncResult.value(get(serverEnvironment.welcome(target))));
71
72 return {
73 config: get(serverEnvironment.configValueAtom(environmentId)),
74 latestEvent: configProjection?.latestEvent ?? null,
75 welcome,
76 };
77}).pipe(Atom.withLabel("web-primary-server-state"));
78
79export const primaryServerConfigAtom = Atom.make(
80 (get): ServerConfig | null => get(primaryServerStateAtom).config,
81).pipe(Atom.withLabel("web-primary-server-config"));
82
83export const primaryServerConfigEventAtom = Atom.make(
84 (get): ServerConfigStreamEvent | null => get(primaryServerStateAtom).latestEvent,
85).pipe(Atom.withLabel("web-primary-server-config-event"));
86
87export const primaryServerWelcomeAtom = Atom.make(
88 (get): ServerLifecycleWelcomePayload | null => get(primaryServerStateAtom).welcome,
89).pipe(Atom.withLabel("web-primary-server-welcome"));
90
91export const primaryServerLegacyThreadMigrationAtom = Atom.make(
92 (get): ServerLifecycleLegacyThreadMigrationPayload | null => {
93 const environmentId = get(primaryEnvironmentIdAtom);
94 if (environmentId === null) {
95 return null;
96 }
97 return Option.getOrNull(
98 AsyncResult.value(get(serverEnvironment.legacyThreadMigration({ environmentId, input: {} }))),
99 );
100 },
101).pipe(Atom.withLabel("web-primary-server-legacy-thread-migration"));
102
103export const primaryServerSettingsAtom = Atom.make(
104 (get): ServerSettings => get(primaryServerConfigAtom)?.settings ?? DEFAULT_SERVER_SETTINGS,
105).pipe(Atom.withLabel("web-primary-server-settings"));
106
107export const primaryServerProvidersAtom = Atom.make(
108 (get): ReadonlyArray<ServerProvider> =>
109 get(primaryServerConfigAtom)?.providers ?? EMPTY_SERVER_PROVIDERS,
110).pipe(Atom.withLabel("web-primary-server-providers"));
111
112export const primaryServerKeybindingsAtom = Atom.make((get): ServerConfig["keybindings"] =>
113 mergeWithDefaultKeybindings(get(primaryServerConfigAtom)?.keybindings ?? []),
114).pipe(Atom.withLabel("web-primary-server-keybindings"));
115
116export const primaryServerAvailableEditorsAtom = Atom.make(
117 (get): ReadonlyArray<EditorId> =>
118 get(primaryServerConfigAtom)?.availableEditors ?? EMPTY_AVAILABLE_EDITORS,
119).pipe(Atom.withLabel("web-primary-server-available-editors"));
120
121const EMPTY_ENVIRONMENT_THEMES: ReadonlyArray<EnvironmentTheme> = [];
122
123export const primaryServerEnvironmentThemesAtom = Atom.make(
124 (get): ReadonlyArray<EnvironmentTheme> =>
125 get(primaryServerConfigAtom)?.environmentThemes ?? EMPTY_ENVIRONMENT_THEMES,
126).pipe(Atom.withLabel("web-primary-server-environment-themes"));
127
128/** Providers for views that do not show `checkedAt`; see {@link reuseProvidersIgnoringCheckedAt}. */
129export const providersIgnoringCheckedAtAtom = Atom.family((environmentId: EnvironmentId) =>
130 Atom.make((get): ReadonlyArray<ServerProvider> =>
131 reuseProvidersIgnoringCheckedAt(
132 Option.getOrNull(get.self<ReadonlyArray<ServerProvider>>()),
133 get(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS,
134 ),
135 ).pipe(Atom.withLabel(`web-providers-ignoring-checked-at:${environmentId}`)),
136);