diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-01-20 19:13:45 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-02-08 21:43:16 +0100 |
commit | cef1c2fd221e81424195026645386370662ef398 (patch) | |
tree | 77fc9d044adef735adb2c2acc0ba3e74858aa1f8 | |
parent | refactor: Rename main services to infrastructure (diff) | |
download | sophie-cef1c2fd221e81424195026645386370662ef398.tar.gz sophie-cef1c2fd221e81424195026645386370662ef398.tar.zst sophie-cef1c2fd221e81424195026645386370662ef398.zip |
feat: Add selected service field to SharedStore
Lets the main process see which service is currently selected.
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
-rw-r--r-- | packages/main/src/index.ts | 3 | ||||
-rw-r--r-- | packages/main/src/stores/MainStore.ts | 22 | ||||
-rw-r--r-- | packages/renderer/src/components/ServiceSwitcher.tsx | 15 | ||||
-rw-r--r-- | packages/renderer/src/stores/RendererStore.ts | 9 | ||||
-rw-r--r-- | packages/renderer/src/stores/SharedStore.ts | 2 | ||||
-rw-r--r-- | packages/shared/src/schemas.ts | 6 | ||||
-rw-r--r-- | packages/shared/src/stores/SharedStore.ts | 4 |
7 files changed, 50 insertions, 11 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index ee89f49..66405ea 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -269,6 +269,9 @@ async function createWindow(): Promise<unknown> { | |||
269 | try { | 269 | try { |
270 | const actionToDispatch = action.parse(rawAction); | 270 | const actionToDispatch = action.parse(rawAction); |
271 | switch (actionToDispatch.action) { | 271 | switch (actionToDispatch.action) { |
272 | case 'set-selected-service-id': | ||
273 | store.setSelectedServiceId(actionToDispatch.serviceId); | ||
274 | break; | ||
272 | case 'set-browser-view-bounds': | 275 | case 'set-browser-view-bounds': |
273 | store.setBrowserViewBounds(actionToDispatch.browserViewBounds); | 276 | store.setBrowserViewBounds(actionToDispatch.browserViewBounds); |
274 | break; | 277 | break; |
diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts index eaf5b3c..f0d6472 100644 --- a/packages/main/src/stores/MainStore.ts +++ b/packages/main/src/stores/MainStore.ts | |||
@@ -18,12 +18,21 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { BrowserViewBounds } from '@sophie/shared'; | 21 | import { BrowserViewBounds, service } from '@sophie/shared'; |
22 | import { applySnapshot, Instance, types } from 'mobx-state-tree'; | 22 | import { |
23 | applySnapshot, | ||
24 | Instance, | ||
25 | resolveIdentifier, | ||
26 | types, | ||
27 | } from 'mobx-state-tree'; | ||
28 | |||
29 | import { getLogger } from '../utils/log'; | ||
23 | 30 | ||
24 | import type { Config } from './Config.js'; | 31 | import type { Config } from './Config.js'; |
25 | import { sharedStore } from './SharedStore'; | 32 | import { sharedStore } from './SharedStore'; |
26 | 33 | ||
34 | const log = getLogger('mainStore'); | ||
35 | |||
27 | export const mainStore = types | 36 | export const mainStore = types |
28 | .model('MainStore', { | 37 | .model('MainStore', { |
29 | browserViewBounds: types.optional( | 38 | browserViewBounds: types.optional( |
@@ -43,6 +52,15 @@ export const mainStore = types | |||
43 | }, | 52 | }, |
44 | })) | 53 | })) |
45 | .actions((self) => ({ | 54 | .actions((self) => ({ |
55 | setSelectedServiceId(serviceId: string): void { | ||
56 | const serviceInstance = resolveIdentifier(service, self, serviceId); | ||
57 | if (serviceInstance === undefined) { | ||
58 | log.warn('Trying to select unknown service', serviceId); | ||
59 | return; | ||
60 | } | ||
61 | self.shared.selectedService = serviceInstance; | ||
62 | log.debug('Selected service', serviceId); | ||
63 | }, | ||
46 | setBrowserViewBounds(bounds: BrowserViewBounds): void { | 64 | setBrowserViewBounds(bounds: BrowserViewBounds): void { |
47 | applySnapshot(self.browserViewBounds, bounds); | 65 | applySnapshot(self.browserViewBounds, bounds); |
48 | }, | 66 | }, |
diff --git a/packages/renderer/src/components/ServiceSwitcher.tsx b/packages/renderer/src/components/ServiceSwitcher.tsx index b454451..0786b71 100644 --- a/packages/renderer/src/components/ServiceSwitcher.tsx +++ b/packages/renderer/src/components/ServiceSwitcher.tsx | |||
@@ -22,7 +22,7 @@ import Tab from '@mui/material/Tab'; | |||
22 | import Tabs from '@mui/material/Tabs'; | 22 | import Tabs from '@mui/material/Tabs'; |
23 | import { alpha, styled } from '@mui/material/styles'; | 23 | import { alpha, styled } from '@mui/material/styles'; |
24 | import { observer } from 'mobx-react-lite'; | 24 | import { observer } from 'mobx-react-lite'; |
25 | import React, { useState } from 'react'; | 25 | import React from 'react'; |
26 | 26 | ||
27 | import ServiceIcon from './ServiceIcon'; | 27 | import ServiceIcon from './ServiceIcon'; |
28 | import { useStore } from './StoreProvider'; | 28 | import { useStore } from './StoreProvider'; |
@@ -62,18 +62,17 @@ const ServiceSwitcherTab = styled(Tab, { | |||
62 | })); | 62 | })); |
63 | 63 | ||
64 | export default observer(() => { | 64 | export default observer(() => { |
65 | const { services } = useStore(); | 65 | const store = useStore(); |
66 | // TODO Move this to the `SharedStore`. | 66 | const { selectedService, services } = store; |
67 | const [selectedService, setSelectedService] = useState<string | boolean>( | ||
68 | false, | ||
69 | ); | ||
70 | 67 | ||
71 | return ( | 68 | return ( |
72 | <ServiceSwitcherRoot | 69 | <ServiceSwitcherRoot |
73 | variant="scrollable" | 70 | variant="scrollable" |
74 | orientation="vertical" | 71 | orientation="vertical" |
75 | value={selectedService} | 72 | value={selectedService === undefined ? false : selectedService.id} |
76 | onChange={(_event, newValue: string) => setSelectedService(newValue)} | 73 | onChange={(_event, newValue: string) => |
74 | store.setSelectedServiceId(newValue) | ||
75 | } | ||
77 | > | 76 | > |
78 | {services.map((service) => ( | 77 | {services.map((service) => ( |
79 | <ServiceSwitcherTab | 78 | <ServiceSwitcherTab |
diff --git a/packages/renderer/src/stores/RendererStore.ts b/packages/renderer/src/stores/RendererStore.ts index d0e7843..731ca28 100644 --- a/packages/renderer/src/stores/RendererStore.ts +++ b/packages/renderer/src/stores/RendererStore.ts | |||
@@ -42,8 +42,17 @@ export const rendererStore = types | |||
42 | get services(): Service[] { | 42 | get services(): Service[] { |
43 | return this.config.services; | 43 | return this.config.services; |
44 | }, | 44 | }, |
45 | get selectedService(): Service | undefined { | ||
46 | return self.shared.selectedService; | ||
47 | }, | ||
45 | })) | 48 | })) |
46 | .actions((self) => ({ | 49 | .actions((self) => ({ |
50 | setSelectedServiceId(serviceId: string): void { | ||
51 | getEnv(self).dispatchMainAction({ | ||
52 | action: 'set-selected-service-id', | ||
53 | serviceId, | ||
54 | }); | ||
55 | }, | ||
47 | setBrowserViewBounds(browserViewBounds: BrowserViewBounds): void { | 56 | setBrowserViewBounds(browserViewBounds: BrowserViewBounds): void { |
48 | getEnv(self).dispatchMainAction({ | 57 | getEnv(self).dispatchMainAction({ |
49 | action: 'set-browser-view-bounds', | 58 | action: 'set-browser-view-bounds', |
diff --git a/packages/renderer/src/stores/SharedStore.ts b/packages/renderer/src/stores/SharedStore.ts index baaf061..962f7e2 100644 --- a/packages/renderer/src/stores/SharedStore.ts +++ b/packages/renderer/src/stores/SharedStore.ts | |||
@@ -22,9 +22,11 @@ import { sharedStore as originalSharedStore } from '@sophie/shared'; | |||
22 | import { Instance, types } from 'mobx-state-tree'; | 22 | import { Instance, types } from 'mobx-state-tree'; |
23 | 23 | ||
24 | import { config } from './Config'; | 24 | import { config } from './Config'; |
25 | import { service } from './Service'; | ||
25 | 26 | ||
26 | export const sharedStore = originalSharedStore.props({ | 27 | export const sharedStore = originalSharedStore.props({ |
27 | config: types.optional(config, {}), | 28 | config: types.optional(config, {}), |
29 | selectedService: types.safeReference(service), | ||
28 | }); | 30 | }); |
29 | 31 | ||
30 | export interface SharedStore extends Instance<typeof sharedStore> {} | 32 | export interface SharedStore extends Instance<typeof sharedStore> {} |
diff --git a/packages/shared/src/schemas.ts b/packages/shared/src/schemas.ts index 2f108bd..7fb9717 100644 --- a/packages/shared/src/schemas.ts +++ b/packages/shared/src/schemas.ts | |||
@@ -20,6 +20,11 @@ | |||
20 | 20 | ||
21 | import { z } from 'zod'; | 21 | import { z } from 'zod'; |
22 | 22 | ||
23 | const setSelectedServiceId = z.object({ | ||
24 | action: z.literal('set-selected-service-id'), | ||
25 | serviceId: z.string(), | ||
26 | }); | ||
27 | |||
23 | export const browserViewBounds = z.object({ | 28 | export const browserViewBounds = z.object({ |
24 | x: z.number().int().nonnegative(), | 29 | x: z.number().int().nonnegative(), |
25 | y: z.number().int().nonnegative(), | 30 | y: z.number().int().nonnegative(), |
@@ -48,6 +53,7 @@ const reloadAllServicesAction = z.object({ | |||
48 | }); | 53 | }); |
49 | 54 | ||
50 | export const action = z.union([ | 55 | export const action = z.union([ |
56 | setSelectedServiceId, | ||
51 | setBrowserViewBoundsAction, | 57 | setBrowserViewBoundsAction, |
52 | setThemeSourceAction, | 58 | setThemeSourceAction, |
53 | reloadAllServicesAction, | 59 | reloadAllServicesAction, |
diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStore.ts index ffa387f..e6e2cad 100644 --- a/packages/shared/src/stores/SharedStore.ts +++ b/packages/shared/src/stores/SharedStore.ts | |||
@@ -28,11 +28,13 @@ import { | |||
28 | 28 | ||
29 | import { config } from './Config'; | 29 | import { config } from './Config'; |
30 | import { runtimeService } from './RuntimeService'; | 30 | import { runtimeService } from './RuntimeService'; |
31 | import { service } from './Service'; | ||
31 | 32 | ||
32 | export const sharedStore = types.model('SharedStore', { | 33 | export const sharedStore = types.model('SharedStore', { |
33 | config: types.optional(config, {}), | 34 | config: types.optional(config, {}), |
34 | shouldUseDarkColors: false, | ||
35 | runtimeServices: types.map(runtimeService), | 35 | runtimeServices: types.map(runtimeService), |
36 | selectedService: types.safeReference(service), | ||
37 | shouldUseDarkColors: false, | ||
36 | }); | 38 | }); |
37 | 39 | ||
38 | export interface SharedStore extends Instance<typeof sharedStore> {} | 40 | export interface SharedStore extends Instance<typeof sharedStore> {} |