diff options
Diffstat (limited to 'packages/main/src/stores/SharedStore.ts')
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 79 |
1 files changed, 64 insertions, 15 deletions
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts index 182b693..e1c2d3c 100644 --- a/packages/main/src/stores/SharedStore.ts +++ b/packages/main/src/stores/SharedStore.ts | |||
@@ -18,28 +18,24 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { SharedStore as SharedStoreBase } from '@sophie/shared'; | 21 | import { defineSharedStoreModel, WritingDirection } from '@sophie/shared'; |
22 | import { getSnapshot, Instance, types } from 'mobx-state-tree'; | 22 | import { getSnapshot, Instance } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import overrideProps from '../utils/overrideProps'; | 24 | import getLogger from '../utils/getLogger.js'; |
25 | 25 | ||
26 | import GlobalSettings from './GlobalSettings'; | 26 | import GlobalSettings from './GlobalSettings.js'; |
27 | import Profile from './Profile'; | 27 | import Profile from './Profile.js'; |
28 | import Service from './Service'; | 28 | import Service from './Service.js'; |
29 | import type Config from './config/Config'; | 29 | import type Config from './config/Config.js'; |
30 | import loadConfig from './config/loadConfig'; | 30 | import loadConfig from './config/loadConfig.js'; |
31 | |||
32 | const log = getLogger('SharedStore'); | ||
31 | 33 | ||
32 | function getConfigs<T>(models: { config: T }[]): T[] | undefined { | 34 | function getConfigs<T>(models: { config: T }[]): T[] | undefined { |
33 | return models.length === 0 ? undefined : models.map((model) => model.config); | 35 | return models.length === 0 ? undefined : models.map((model) => model.config); |
34 | } | 36 | } |
35 | 37 | ||
36 | const SharedStore = overrideProps(SharedStoreBase, { | 38 | const SharedStore = defineSharedStoreModel(GlobalSettings, Profile, Service) |
37 | settings: types.optional(GlobalSettings, {}), | ||
38 | profilesById: types.map(Profile), | ||
39 | profiles: types.array(types.reference(Profile)), | ||
40 | servicesById: types.map(Service), | ||
41 | services: types.array(types.reference(Service)), | ||
42 | }) | ||
43 | .views((self) => ({ | 39 | .views((self) => ({ |
44 | get config(): Config { | 40 | get config(): Config { |
45 | const { settings, profiles, services } = self; | 41 | const { settings, profiles, services } = self; |
@@ -50,14 +46,67 @@ const SharedStore = overrideProps(SharedStoreBase, { | |||
50 | services: getConfigs(services), | 46 | services: getConfigs(services), |
51 | }; | 47 | }; |
52 | }, | 48 | }, |
49 | get canSwitchServices(): boolean { | ||
50 | return self.services.length >= 2; | ||
51 | }, | ||
52 | get selectedServiceIndex(): number { | ||
53 | const { | ||
54 | services, | ||
55 | settings: { selectedService }, | ||
56 | } = self; | ||
57 | if (selectedService === undefined) { | ||
58 | return -1; | ||
59 | } | ||
60 | return services.indexOf(selectedService); | ||
61 | }, | ||
53 | })) | 62 | })) |
54 | .actions((self) => ({ | 63 | .actions((self) => ({ |
55 | loadConfig(config: Config): void { | 64 | loadConfig(config: Config): void { |
56 | loadConfig(self, config); | 65 | loadConfig(self, config); |
57 | }, | 66 | }, |
67 | setLanguage(langauge: string, writingDirection: WritingDirection): void { | ||
68 | self.language = langauge; | ||
69 | self.writingDirection = writingDirection; | ||
70 | }, | ||
58 | setShouldUseDarkColors(shouldUseDarkColors: boolean): void { | 71 | setShouldUseDarkColors(shouldUseDarkColors: boolean): void { |
59 | self.shouldUseDarkColors = shouldUseDarkColors; | 72 | self.shouldUseDarkColors = shouldUseDarkColors; |
60 | }, | 73 | }, |
74 | activateServiceByOffset(offset: number): void { | ||
75 | if (offset === 0) { | ||
76 | return; | ||
77 | } | ||
78 | const { selectedServiceIndex: index, services, settings } = self; | ||
79 | if (index < 0) { | ||
80 | log.warn('No selected service to offset'); | ||
81 | return; | ||
82 | } | ||
83 | const { length } = services; | ||
84 | const indexWithOffset = (index + offset) % length; | ||
85 | // Make sure that `newIndex` is positive even for large negative `offset`. | ||
86 | const newIndex = | ||
87 | indexWithOffset < 0 ? indexWithOffset + length : indexWithOffset; | ||
88 | const newService = services.at(newIndex); | ||
89 | if (newService === undefined) { | ||
90 | log.error( | ||
91 | 'Could not advance selected service index from', | ||
92 | index, | ||
93 | 'by', | ||
94 | offset, | ||
95 | 'to', | ||
96 | newIndex, | ||
97 | ); | ||
98 | return; | ||
99 | } | ||
100 | settings.setSelectedService(newService); | ||
101 | }, | ||
102 | })) | ||
103 | .actions((self) => ({ | ||
104 | activateNextService(): void { | ||
105 | self.activateServiceByOffset(1); | ||
106 | }, | ||
107 | activatePreviousService(): void { | ||
108 | self.activateServiceByOffset(-1); | ||
109 | }, | ||
61 | })); | 110 | })); |
62 | 111 | ||
63 | /* | 112 | /* |