aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/src/stores/SharedStoreBase.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/shared/src/stores/SharedStoreBase.ts')
-rw-r--r--packages/shared/src/stores/SharedStoreBase.ts101
1 files changed, 101 insertions, 0 deletions
diff --git a/packages/shared/src/stores/SharedStoreBase.ts b/packages/shared/src/stores/SharedStoreBase.ts
new file mode 100644
index 0000000..786dc5f
--- /dev/null
+++ b/packages/shared/src/stores/SharedStoreBase.ts
@@ -0,0 +1,101 @@
1/*
2 * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import {
22 type IJsonPatch,
23 type Instance,
24 types,
25 type SnapshotIn,
26 type SnapshotOut,
27 type IAnyModelType,
28} from 'mobx-state-tree';
29
30import GlobalSettingsBase from './GlobalSettingsBase.js';
31import ProfileBase from './Profile.js';
32import ServiceBase from './ServiceBase.js';
33import WritingDirection from './WritingDirection.js';
34
35export const FALLBACK_LOCALE = 'en';
36
37export function defineSharedStoreModel<
38 TG extends IAnyModelType,
39 TP extends IAnyModelType,
40 TS extends IAnyModelType,
41>(globalSettings: TG, profile: TP, service: TS) {
42 return types
43 .model('SharedStore', {
44 settings: types.optional(globalSettings, {}),
45 profilesById: types.map(profile),
46 profiles: types.array(types.reference(profile)),
47 servicesById: types.map(service),
48 services: types.array(types.reference(service)),
49 shouldUseDarkColors: false,
50 language: FALLBACK_LOCALE,
51 writingDirection: types.optional(WritingDirection, 'ltr'),
52 })
53 .views((self) => ({
54 get alwaysHideLocationBar(): boolean {
55 const settings = self.settings as GlobalSettingsBase;
56 return settings.selectedService === undefined;
57 },
58 get alwaysShowLocationBar(): boolean {
59 const settings = self.settings as GlobalSettingsBase;
60 const selectedService = settings.selectedService as
61 | ServiceBase
62 | undefined;
63 return selectedService?.alwaysShowLocationBar ?? false;
64 },
65 }))
66 .views((self) => ({
67 get locationBarVisible(): boolean {
68 const settings = self.settings as GlobalSettingsBase;
69 return (
70 !self.alwaysHideLocationBar &&
71 (self.alwaysShowLocationBar || settings.showLocationBar)
72 );
73 },
74 get canToggleLocationBar(): boolean {
75 return !(self.alwaysHideLocationBar || self.alwaysShowLocationBar);
76 },
77 }));
78}
79
80const SharedStoreBase = /* @__PURE__ */ (() =>
81 defineSharedStoreModel(GlobalSettingsBase, ProfileBase, ServiceBase))();
82
83/*
84 eslint-disable-next-line @typescript-eslint/no-redeclare --
85 Intentionally naming the type the same as the store definition.
86*/
87interface SharedStoreBase extends Instance<typeof SharedStoreBase> {}
88
89export default SharedStoreBase;
90
91export interface SharedStoreSnapshotIn
92 extends SnapshotIn<typeof SharedStoreBase> {}
93
94export interface SharedStoreSnapshotOut
95 extends SnapshotOut<typeof SharedStoreBase> {}
96
97export interface SharedStoreListener {
98 onSnapshot(snapshot: SharedStoreSnapshotIn): void;
99
100 onPatch(patches: IJsonPatch[]): void;
101}