aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/stores/SharedStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/stores/SharedStore.ts')
-rw-r--r--packages/main/src/stores/SharedStore.ts91
1 files changed, 7 insertions, 84 deletions
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts
index c34af75..b9983f6 100644
--- a/packages/main/src/stores/SharedStore.ts
+++ b/packages/main/src/stores/SharedStore.ts
@@ -19,71 +19,23 @@
19 */ 19 */
20 20
21import { SharedStore as SharedStoreBase } from '@sophie/shared'; 21import { SharedStore as SharedStoreBase } from '@sophie/shared';
22import { 22import { getSnapshot, Instance, types } from 'mobx-state-tree';
23 applySnapshot,
24 getSnapshot,
25 IMSTArray,
26 IMSTMap,
27 Instance,
28 IReferenceType,
29 IStateTreeNode,
30 IType,
31 types,
32} from 'mobx-state-tree';
33 23
34import { getLogger } from '../utils/log'; 24import { getLogger } from '../utils/log';
35import overrideProps from '../utils/overrideProps'; 25import overrideProps from '../utils/overrideProps';
36 26
37import GlobalSettings, { GlobalSettingsSnapshotIn } from './GlobalSettings'; 27import GlobalSettings from './GlobalSettings';
38import Profile, { addMissingProfileIds, ProfileConfig } from './Profile'; 28import Profile from './Profile';
39import Service, { 29import Service from './Service';
40 addMissingServiceIdsAndProfiles, 30import type Config from './config/Config';
41 ServiceConfig, 31import loadConfig from './config/loadConfig';
42} from './Service';
43 32
44const log = getLogger('sharedStore'); 33const log = getLogger('sharedStore');
45 34
46export interface Config extends GlobalSettingsSnapshotIn {
47 profiles?: ProfileConfig[] | undefined;
48
49 services?: ServiceConfig[] | undefined;
50}
51
52function getConfigs<T>(models: { config: T }[]): T[] | undefined { 35function getConfigs<T>(models: { config: T }[]): T[] | undefined {
53 return models.length === 0 ? undefined : models.map((model) => model.config); 36 return models.length === 0 ? undefined : models.map((model) => model.config);
54} 37}
55 38
56function applySettings<
57 C,
58 D extends IType<
59 { id: string; settings: C },
60 unknown,
61 { settings: IStateTreeNode<IType<C, unknown, unknown>> }
62 >,
63>(
64 current: IMSTArray<IReferenceType<D>>,
65 currentById: IMSTMap<D>,
66 toApply: [string, C][],
67): void {
68 const toApplyById = new Map(toApply);
69 const toDelete = new Set(currentById.keys());
70 toApplyById.forEach((settingsSnapshot, id) => {
71 const model = currentById.get(id);
72 if (model === undefined) {
73 currentById.set(id, {
74 id,
75 settings: settingsSnapshot,
76 });
77 } else {
78 toDelete.delete(id);
79 applySnapshot(model.settings, settingsSnapshot);
80 }
81 });
82 current.clear();
83 toDelete.forEach((id) => currentById.delete(id));
84 current.push(...toApply.map(([id]) => id));
85}
86
87const SharedStore = overrideProps(SharedStoreBase, { 39const SharedStore = overrideProps(SharedStoreBase, {
88 settings: types.optional(GlobalSettings, {}), 40 settings: types.optional(GlobalSettings, {}),
89 profilesById: types.map(Profile), 41 profilesById: types.map(Profile),
@@ -105,36 +57,7 @@ const SharedStore = overrideProps(SharedStoreBase, {
105 })) 57 }))
106 .actions((self) => ({ 58 .actions((self) => ({
107 loadConfig(config: Config): void { 59 loadConfig(config: Config): void {
108 const { 60 loadConfig(self, config);
109 profiles,
110 profilesById,
111 selectedService,
112 services,
113 servicesById,
114 settings,
115 } = self;
116 const { id: selectedServiceId } = selectedService ?? { id: undefined };
117 const {
118 profiles: profilesConfig,
119 services: servicesConfig,
120 ...settingsToApply
121 } = config;
122 const profilesToApply = addMissingProfileIds(profilesConfig);
123 const servicesToApply = addMissingServiceIdsAndProfiles(
124 servicesConfig,
125 profilesToApply,
126 );
127 applySettings(profiles, profilesById, profilesToApply);
128 applySettings(services, servicesById, servicesToApply);
129 applySnapshot(settings, settingsToApply);
130 let newSelectedService;
131 if (selectedServiceId !== undefined) {
132 newSelectedService = servicesById.get(selectedServiceId);
133 }
134 if (newSelectedService === undefined && services.length > 0) {
135 [newSelectedService] = services;
136 }
137 self.selectedService = newSelectedService;
138 }, 61 },
139 setShouldUseDarkColors(shouldUseDarkColors: boolean): void { 62 setShouldUseDarkColors(shouldUseDarkColors: boolean): void {
140 self.shouldUseDarkColors = shouldUseDarkColors; 63 self.shouldUseDarkColors = shouldUseDarkColors;