/* * Copyright (C) 2021-2022 Kristóf Marussy * * This file is part of Sophie. * * Sophie is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * SPDX-License-Identifier: AGPL-3.0-only */ import { sharedStore as originalSharedStore } from '@sophie/shared'; import { applySnapshot, getSnapshot, IMSTArray, IMSTMap, Instance, IReferenceType, IStateTreeNode, IType, resolveIdentifier, types, } from 'mobx-state-tree'; import { getLogger } from '../utils/log'; import overrideProps from '../utils/overrideProps'; import { globalSettings, GlobalSettingsSnapshotIn } from './GlobalSettings'; import { addMissingProfileIds, profile, ProfileConfig } from './Profile'; import { addMissingServiceIdsAndProfiles, service, ServiceConfig, } from './Service'; const log = getLogger('sharedStore'); export interface Config extends GlobalSettingsSnapshotIn { profiles?: ProfileConfig[] | undefined; services?: ServiceConfig[] | undefined; } function getConfigs(models: { config: T }[]): T[] | undefined { return models.length === 0 ? undefined : models.map((model) => model.config); } function applySettings< C, D extends IType< { id: string; settings: C }, unknown, { settings: IStateTreeNode> } >, >( current: IMSTArray>, currentById: IMSTMap, toApply: [string, C][], ): void { const toApplyById = new Map(toApply); const toDelete = new Set(currentById.keys()); toApplyById.forEach((settingsSnapshot, id) => { const model = currentById.get(id); if (model === undefined) { currentById.set(id, { id, settings: settingsSnapshot, }); } else { toDelete.delete(id); applySnapshot(model.settings, settingsSnapshot); } }); current.clear(); toDelete.forEach((id) => currentById.delete(id)); current.push(...toApply.map(([id]) => id)); } export const sharedStore = overrideProps(originalSharedStore, { settings: types.optional(globalSettings, {}), profilesById: types.map(profile), profiles: types.array(types.reference(profile)), servicesById: types.map(service), services: types.array(types.reference(service)), selectedService: types.safeReference(service), }) .views((self) => ({ get config(): Config { const { settings, profiles, services } = self; const globalSettingsConfig = getSnapshot(settings); return { ...globalSettingsConfig, profiles: getConfigs(profiles), services: getConfigs(services), }; }, })) .actions((self) => ({ loadConfig(config: Config): void { const { profiles, profilesById, selectedService, services, servicesById, settings, } = self; const { id: selectedServiceId } = selectedService ?? { id: undefined }; const { profiles: profilesConfig, services: servicesConfig, ...settingsToApply } = config; const profilesToApply = addMissingProfileIds(profilesConfig); const servicesToApply = addMissingServiceIdsAndProfiles( servicesConfig, profilesToApply, ); applySettings(profiles, profilesById, profilesToApply); applySettings(services, servicesById, servicesToApply); applySnapshot(settings, settingsToApply); let newSelectedService; if (selectedServiceId !== undefined) { newSelectedService = servicesById.get(selectedServiceId); } if (newSelectedService === undefined && services.length > 0) { [newSelectedService] = services; } self.selectedService = newSelectedService; }, setShouldUseDarkColors(shouldUseDarkColors: boolean): void { self.shouldUseDarkColors = shouldUseDarkColors; }, setSelectedServiceId(serviceId: string): void { const serviceInstance = resolveIdentifier(service, self, serviceId); if (serviceInstance === undefined) { log.warn('Trying to select unknown service', serviceId); return; } self.selectedService = serviceInstance; log.debug('Selected service', serviceId); }, })); export interface SharedStore extends Instance {} export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut, } from '@sophie/shared';