diff options
Diffstat (limited to 'packages/main/src/stores/MainStore.ts')
-rw-r--r-- | packages/main/src/stores/MainStore.ts | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts index 93cabb9..d717bed 100644 --- a/packages/main/src/stores/MainStore.ts +++ b/packages/main/src/stores/MainStore.ts | |||
@@ -19,7 +19,8 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { Action, BrowserViewBounds } from '@sophie/shared'; | 21 | import type { Action, BrowserViewBounds } from '@sophie/shared'; |
22 | import { applySnapshot, Instance, types } from 'mobx-state-tree'; | 22 | import type { ResourceKey } from 'i18next'; |
23 | import { applySnapshot, flow, Instance, types } from 'mobx-state-tree'; | ||
23 | 24 | ||
24 | import type I18nStore from '../i18n/I18nStore'; | 25 | import type I18nStore from '../i18n/I18nStore'; |
25 | import type { UseTranslationResult } from '../i18n/I18nStore'; | 26 | import type { UseTranslationResult } from '../i18n/I18nStore'; |
@@ -67,6 +68,12 @@ const MainStore = types | |||
67 | useTranslation(ns?: string): UseTranslationResult { | 68 | useTranslation(ns?: string): UseTranslationResult { |
68 | return self.i18n?.useTranslation(ns) ?? { ready: false }; | 69 | return self.i18n?.useTranslation(ns) ?? { ready: false }; |
69 | }, | 70 | }, |
71 | getTranslation(language: string, namespace: string): Promise<ResourceKey> { | ||
72 | if (self.i18n === undefined) { | ||
73 | throw new Error('i18next has not been set'); | ||
74 | } | ||
75 | return self.i18n.getTranslation(language, namespace); | ||
76 | }, | ||
70 | })) | 77 | })) |
71 | .volatile( | 78 | .volatile( |
72 | (): { | 79 | (): { |
@@ -79,10 +86,43 @@ const MainStore = types | |||
79 | setBrowserViewBounds(bounds: BrowserViewBounds): void { | 86 | setBrowserViewBounds(bounds: BrowserViewBounds): void { |
80 | applySnapshot(self.browserViewBounds, bounds); | 87 | applySnapshot(self.browserViewBounds, bounds); |
81 | }, | 88 | }, |
89 | setMainWindow(mainWindow: MainWindow | undefined): void { | ||
90 | self.mainWindow = mainWindow; | ||
91 | }, | ||
92 | openWebpageInBrowser() { | ||
93 | getEnv(self).openURLInExternalBrowser( | ||
94 | 'https://gitlab.com/say-hi-to-sophie/shophie', | ||
95 | ); | ||
96 | }, | ||
97 | openAboutDialog() { | ||
98 | getEnv(self).openAboutDialog(); | ||
99 | }, | ||
100 | beforeDestroy(): void { | ||
101 | self.mainWindow?.dispose(); | ||
102 | }, | ||
103 | setI18n(i18n: I18nStore): void { | ||
104 | self.i18n = i18n; | ||
105 | }, | ||
106 | addMissingTranslation( | ||
107 | languages: string[], | ||
108 | namespace: string, | ||
109 | key: string, | ||
110 | value: string, | ||
111 | ): void { | ||
112 | self.i18n?.addMissingTranslation(languages, namespace, key, value); | ||
113 | }, | ||
114 | reloadTranslations: flow(function* reloadTranslations() { | ||
115 | if (self.i18n !== undefined) { | ||
116 | yield self.i18n.reloadTranslations(); | ||
117 | self.mainWindow?.reloadTranslations(); | ||
118 | } | ||
119 | }), | ||
120 | })) | ||
121 | .actions((self) => ({ | ||
82 | dispatch(action: Action): void { | 122 | dispatch(action: Action): void { |
83 | switch (action.action) { | 123 | switch (action.action) { |
84 | case 'set-browser-view-bounds': | 124 | case 'set-browser-view-bounds': |
85 | this.setBrowserViewBounds(action.browserViewBounds); | 125 | self.setBrowserViewBounds(action.browserViewBounds); |
86 | break; | 126 | break; |
87 | case 'set-selected-service-id': | 127 | case 'set-selected-service-id': |
88 | self.settings.setSelectedServiceId(action.serviceId); | 128 | self.settings.setSelectedServiceId(action.serviceId); |
@@ -98,11 +138,19 @@ const MainStore = types | |||
98 | break; | 138 | break; |
99 | case 'reload-all-translations': | 139 | case 'reload-all-translations': |
100 | if (self.i18n !== undefined) { | 140 | if (self.i18n !== undefined) { |
101 | self.i18n.reloadTranslations().catch((error) => { | 141 | self.reloadTranslations().catch((error) => { |
102 | log.error('Failed to reload translations', error); | 142 | log.error('Failed to reload translations', error); |
103 | }); | 143 | }); |
104 | } | 144 | } |
105 | break; | 145 | break; |
146 | case 'add-missing-translation': | ||
147 | self.addMissingTranslation( | ||
148 | action.languages, | ||
149 | action.namespace, | ||
150 | action.key, | ||
151 | action.value, | ||
152 | ); | ||
153 | break; | ||
106 | case 'dispatch-service-action': { | 154 | case 'dispatch-service-action': { |
107 | const { serviceId, serviceAction } = action; | 155 | const { serviceId, serviceAction } = action; |
108 | const service = self.shared.servicesById.get(serviceId); | 156 | const service = self.shared.servicesById.get(serviceId); |
@@ -123,23 +171,6 @@ const MainStore = types | |||
123 | break; | 171 | break; |
124 | } | 172 | } |
125 | }, | 173 | }, |
126 | setMainWindow(mainWindow: MainWindow | undefined): void { | ||
127 | self.mainWindow = mainWindow; | ||
128 | }, | ||
129 | openWebpageInBrowser() { | ||
130 | getEnv(self).openURLInExternalBrowser( | ||
131 | 'https://gitlab.com/say-hi-to-sophie/shophie', | ||
132 | ); | ||
133 | }, | ||
134 | openAboutDialog() { | ||
135 | getEnv(self).openAboutDialog(); | ||
136 | }, | ||
137 | beforeDestroy(): void { | ||
138 | self.mainWindow?.dispose(); | ||
139 | }, | ||
140 | setI18n(i18n: I18nStore): void { | ||
141 | self.i18n = i18n; | ||
142 | }, | ||
143 | })); | 174 | })); |
144 | 175 | ||
145 | /* | 176 | /* |