From cef1c2fd221e81424195026645386370662ef398 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20Marussy?= Date: Thu, 20 Jan 2022 19:13:45 +0100 Subject: feat: Add selected service field to SharedStore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lets the main process see which service is currently selected. Signed-off-by: Kristóf Marussy --- packages/main/src/index.ts | 3 +++ packages/main/src/stores/MainStore.ts | 22 ++++++++++++++++++++-- .../renderer/src/components/ServiceSwitcher.tsx | 15 +++++++-------- packages/renderer/src/stores/RendererStore.ts | 9 +++++++++ packages/renderer/src/stores/SharedStore.ts | 2 ++ packages/shared/src/schemas.ts | 6 ++++++ 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 { try { const actionToDispatch = action.parse(rawAction); switch (actionToDispatch.action) { + case 'set-selected-service-id': + store.setSelectedServiceId(actionToDispatch.serviceId); + break; case 'set-browser-view-bounds': store.setBrowserViewBounds(actionToDispatch.browserViewBounds); 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 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { BrowserViewBounds } from '@sophie/shared'; -import { applySnapshot, Instance, types } from 'mobx-state-tree'; +import { BrowserViewBounds, service } from '@sophie/shared'; +import { + applySnapshot, + Instance, + resolveIdentifier, + types, +} from 'mobx-state-tree'; + +import { getLogger } from '../utils/log'; import type { Config } from './Config.js'; import { sharedStore } from './SharedStore'; +const log = getLogger('mainStore'); + export const mainStore = types .model('MainStore', { browserViewBounds: types.optional( @@ -43,6 +52,15 @@ export const mainStore = types }, })) .actions((self) => ({ + setSelectedServiceId(serviceId: string): void { + const serviceInstance = resolveIdentifier(service, self, serviceId); + if (serviceInstance === undefined) { + log.warn('Trying to select unknown service', serviceId); + return; + } + self.shared.selectedService = serviceInstance; + log.debug('Selected service', serviceId); + }, setBrowserViewBounds(bounds: BrowserViewBounds): void { applySnapshot(self.browserViewBounds, bounds); }, 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'; import Tabs from '@mui/material/Tabs'; import { alpha, styled } from '@mui/material/styles'; import { observer } from 'mobx-react-lite'; -import React, { useState } from 'react'; +import React from 'react'; import ServiceIcon from './ServiceIcon'; import { useStore } from './StoreProvider'; @@ -62,18 +62,17 @@ const ServiceSwitcherTab = styled(Tab, { })); export default observer(() => { - const { services } = useStore(); - // TODO Move this to the `SharedStore`. - const [selectedService, setSelectedService] = useState( - false, - ); + const store = useStore(); + const { selectedService, services } = store; return ( setSelectedService(newValue)} + value={selectedService === undefined ? false : selectedService.id} + onChange={(_event, newValue: string) => + store.setSelectedServiceId(newValue) + } > {services.map((service) => ( ({ + setSelectedServiceId(serviceId: string): void { + getEnv(self).dispatchMainAction({ + action: 'set-selected-service-id', + serviceId, + }); + }, setBrowserViewBounds(browserViewBounds: BrowserViewBounds): void { getEnv(self).dispatchMainAction({ 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'; import { Instance, types } from 'mobx-state-tree'; import { config } from './Config'; +import { service } from './Service'; export const sharedStore = originalSharedStore.props({ config: types.optional(config, {}), + selectedService: types.safeReference(service), }); export interface SharedStore extends Instance {} 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 @@ import { z } from 'zod'; +const setSelectedServiceId = z.object({ + action: z.literal('set-selected-service-id'), + serviceId: z.string(), +}); + export const browserViewBounds = z.object({ x: z.number().int().nonnegative(), y: z.number().int().nonnegative(), @@ -48,6 +53,7 @@ const reloadAllServicesAction = z.object({ }); export const action = z.union([ + setSelectedServiceId, setBrowserViewBoundsAction, setThemeSourceAction, 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 { import { config } from './Config'; import { runtimeService } from './RuntimeService'; +import { service } from './Service'; export const sharedStore = types.model('SharedStore', { config: types.optional(config, {}), - shouldUseDarkColors: false, runtimeServices: types.map(runtimeService), + selectedService: types.safeReference(service), + shouldUseDarkColors: false, }); export interface SharedStore extends Instance {} -- cgit v1.2.3