aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/store.js
diff options
context:
space:
mode:
authorLibravatar Muhamed <unknown>2022-11-24 02:56:10 +0530
committerLibravatar Vijay Aravamudhan <vraravam@users.noreply.github.com>2022-11-25 05:04:25 +0530
commitf92933c396db9e94ffd297c41add86de88dfc6c6 (patch)
tree57789f7f64c618086c3792833d076244d879aa75 /src/features/workspaces/store.js
parentfix: use 'Route' from 'react-router-dom' package (diff)
downloadferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.tar.gz
ferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.tar.zst
ferdium-app-f92933c396db9e94ffd297c41add86de88dfc6c6.zip
chore: transform workspace action store and todo store into ts
Diffstat (limited to 'src/features/workspaces/store.js')
-rw-r--r--src/features/workspaces/store.js359
1 files changed, 0 insertions, 359 deletions
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js
deleted file mode 100644
index 2323019fe..000000000
--- a/src/features/workspaces/store.js
+++ /dev/null
@@ -1,359 +0,0 @@
1import { computed, observable, action, makeObservable } from 'mobx';
2import localStorage from 'mobx-localstorage';
3import matchRoute from '../../helpers/routing-helpers';
4import workspaceActions from './actions';
5import FeatureStore from '../utils/FeatureStore';
6import {
7 createWorkspaceRequest,
8 deleteWorkspaceRequest,
9 getUserWorkspacesRequest,
10 updateWorkspaceRequest,
11} from './api';
12import { WORKSPACES_ROUTES } from './constants';
13import { createReactions } from '../../stores/lib/Reaction';
14import { createActionBindings } from '../utils/ActionBinding';
15
16import { KEEP_WS_LOADED_USID } from '../../config';
17
18const debug = require('../../preload-safe-debug')(
19 'Ferdium:feature:workspaces:store',
20);
21
22export default class WorkspacesStore extends FeatureStore {
23 @observable isFeatureActive = false;
24
25 @observable activeWorkspace = null;
26
27 @observable nextWorkspace = null;
28
29 @observable workspaceBeingEdited = null;
30
31 @observable isSwitchingWorkspace = false;
32
33 @observable isWorkspaceDrawerOpen = false;
34
35 @observable isSettingsRouteActive = null;
36
37 constructor() {
38 super();
39
40 makeObservable(this);
41 }
42
43 @computed get workspaces() {
44 if (!this.isFeatureActive) return [];
45 return getUserWorkspacesRequest.result || [];
46 }
47
48 @computed get isLoadingWorkspaces() {
49 if (!this.isFeatureActive) return false;
50 return getUserWorkspacesRequest.isExecutingFirstTime;
51 }
52
53 @computed get settings() {
54 return localStorage.getItem('workspaces') || {};
55 }
56
57 @computed get userHasWorkspaces() {
58 return getUserWorkspacesRequest.wasExecuted && this.workspaces.length > 0;
59 }
60
61 @computed get isUserAllowedToUseFeature() {
62 return true;
63 }
64
65 @computed get isAnyWorkspaceActive() {
66 return !!this.activeWorkspace;
67 }
68
69 // ========== PRIVATE PROPERTIES ========= //
70
71 _wasDrawerOpenBeforeSettingsRoute = null;
72
73 _allActions = [];
74
75 _allReactions = [];
76
77 // ========== PUBLIC API ========= //
78
79 @action start(stores, actions) {
80 debug('WorkspacesStore::start');
81 this.stores = stores;
82 this.actions = actions;
83
84 // ACTIONS
85
86 this._allActions = createActionBindings([
87 [workspaceActions.toggleWorkspaceDrawer, this._toggleWorkspaceDrawer],
88 [workspaceActions.openWorkspaceSettings, this._openWorkspaceSettings],
89 [workspaceActions.edit, this._edit],
90 [workspaceActions.create, this._create],
91 [workspaceActions.delete, this._delete],
92 [workspaceActions.update, this._update],
93 [workspaceActions.activate, this._setActivateWorkspace],
94 [workspaceActions.deactivate, this._deactivateActiveWorkspace],
95 [
96 workspaceActions.toggleKeepAllWorkspacesLoadedSetting,
97 this._toggleKeepAllWorkspacesLoadedSetting,
98 ],
99 ]);
100 this._registerActions(this._allActions);
101
102 // REACTIONS
103
104 this._allReactions = createReactions([
105 this._openDrawerWithSettingsReaction,
106 this._cleanupInvalidServiceReferences,
107 this._setActiveServiceOnWorkspaceSwitchReaction,
108 this._activateLastUsedWorkspaceReaction,
109 this._setWorkspaceBeingEditedReaction,
110 ]);
111 this._registerReactions(this._allReactions);
112
113 this.isFeatureActive = true;
114 }
115
116 @action reset() {
117 this._setActiveWorkspace(null);
118 this._setNextWorkspace(null);
119 this.workspaceBeingEdited = null;
120 this._setIsSwitchingWorkspace(false);
121 this.isWorkspaceDrawerOpen = false;
122 }
123
124 @action stop() {
125 super.stop();
126 debug('WorkspacesStore::stop');
127 this.reset();
128 this.isFeatureActive = false;
129 }
130
131 filterServicesByActiveWorkspace = services => {
132 const { activeWorkspace, isFeatureActive } = this;
133 if (isFeatureActive && activeWorkspace) {
134 return this.getWorkspaceServices(activeWorkspace);
135 }
136 return services;
137 };
138
139 getWorkspaceServices(workspace) {
140 const { services } = this.stores;
141 return workspace.services.map(id => services.one(id)).filter(s => !!s);
142 }
143
144 // ========== PRIVATE METHODS ========= //
145
146 _getWorkspaceById = id => this.workspaces.find(w => w.id === id);
147
148 _updateSettings = changes => {
149 localStorage.setItem('workspaces', {
150 ...this.settings,
151 ...changes,
152 });
153 };
154
155 // Actions
156
157 @action _edit = ({ workspace }) => {
158 this.stores.router.push(`/settings/workspaces/edit/${workspace.id}`);
159 };
160
161 @action _create = async ({ name }) => {
162 const workspace = await createWorkspaceRequest.execute(name);
163 await getUserWorkspacesRequest.result.push(workspace);
164 this._edit({ workspace });
165 };
166
167 @action _delete = async ({ workspace }) => {
168 await deleteWorkspaceRequest.execute(workspace);
169 await getUserWorkspacesRequest.result.remove(workspace);
170 this.stores.router.push('/settings/workspaces');
171 if (this.activeWorkspace === workspace) {
172 this._deactivateActiveWorkspace();
173 }
174 };
175
176 @action _update = async ({ workspace }) => {
177 await updateWorkspaceRequest.execute(workspace);
178 // Path local result optimistically
179 const localWorkspace = this._getWorkspaceById(workspace.id);
180 Object.assign(localWorkspace, workspace);
181 this.stores.router.push('/settings/workspaces');
182 };
183
184 @action _setNextWorkspace(workspace) {
185 this.nextWorkspace = workspace;
186 }
187
188 @action _setIsSwitchingWorkspace(bool) {
189 this.isSwitchingWorkspace = bool;
190 }
191
192 @action _setActiveWorkspace(workspace) {
193 this.activeWorkspace = workspace;
194 }
195
196 @action _setActivateWorkspace = ({ workspace }) => {
197 // Indicate that we are switching to another workspace
198 this._setIsSwitchingWorkspace(true);
199 this._setNextWorkspace(workspace);
200 // Delay switching to next workspace so that the services loading does not drag down UI
201 setTimeout(() => {
202 this._setActiveWorkspace(workspace);
203 this._updateSettings({ lastActiveWorkspace: workspace.id });
204 }, 100);
205 // Indicate that we are done switching to the next workspace
206 setTimeout(() => {
207 this._setIsSwitchingWorkspace(false);
208 this._setNextWorkspace(null);
209 if (this.stores.settings.app.splitMode) {
210 const serviceNames = new Set(
211 this.getWorkspaceServices(workspace).map(service => service.name),
212 );
213 for (const wrapper of document.querySelectorAll(
214 '.services__webview-wrapper',
215 )) {
216 wrapper.style.display = serviceNames.has(wrapper.dataset.name)
217 ? ''
218 : 'none';
219 }
220 }
221 }, 500);
222 };
223
224 @action _deactivateActiveWorkspace = () => {
225 // Indicate that we are switching to default workspace
226 this._setIsSwitchingWorkspace(true);
227 this._setNextWorkspace(null);
228 this._updateSettings({ lastActiveWorkspace: null });
229 // Delay switching to next workspace so that the services loading does not drag down UI
230 setTimeout(() => {
231 this._setActiveWorkspace(null);
232 }, 100);
233 // Indicate that we are done switching to the default workspace
234 setTimeout(() => {
235 this._setIsSwitchingWorkspace(false);
236 if (this.stores.settings.app.splitMode) {
237 for (const wrapper of document.querySelectorAll(
238 '.services__webview-wrapper',
239 )) {
240 wrapper.style.display = '';
241 }
242 }
243 }, 500);
244 };
245
246 @action _toggleWorkspaceDrawer = () => {
247 this.isWorkspaceDrawerOpen = !this.isWorkspaceDrawerOpen;
248 };
249
250 @action _openWorkspaceSettings = () => {
251 this.actions.ui.openSettings({ path: 'workspaces' });
252 };
253
254 @action reorderServicesOfActiveWorkspace = async ({ oldIndex, newIndex }) => {
255 const { activeWorkspace } = this;
256 const { services } = activeWorkspace;
257 // Move services from the old to the new position
258 services.splice(newIndex, 0, services.splice(oldIndex, 1)[0]);
259 await updateWorkspaceRequest.execute(activeWorkspace);
260 };
261
262 @action _setOpenDrawerWithSettings() {
263 const { router } = this.stores;
264 const isWorkspaceSettingsRoute = router.location.pathname.includes(
265 WORKSPACES_ROUTES.ROOT,
266 );
267 const isSwitchingToSettingsRoute =
268 !this.isSettingsRouteActive && isWorkspaceSettingsRoute;
269 const isLeavingSettingsRoute =
270 !isWorkspaceSettingsRoute && this.isSettingsRouteActive;
271
272 if (isSwitchingToSettingsRoute) {
273 this.isSettingsRouteActive = true;
274 this._wasDrawerOpenBeforeSettingsRoute = this.isWorkspaceDrawerOpen;
275 if (!this._wasDrawerOpenBeforeSettingsRoute) {
276 workspaceActions.toggleWorkspaceDrawer();
277 }
278 } else if (isLeavingSettingsRoute) {
279 this.isSettingsRouteActive = false;
280 if (
281 !this._wasDrawerOpenBeforeSettingsRoute &&
282 this.isWorkspaceDrawerOpen
283 ) {
284 workspaceActions.toggleWorkspaceDrawer();
285 }
286 }
287 }
288
289 @action _setWorkspaceBeingEdited(match) {
290 this.workspaceBeingEdited = this._getWorkspaceById(match.id);
291 }
292
293 _toggleKeepAllWorkspacesLoadedSetting = async () => {
294 this._updateSettings({
295 keepAllWorkspacesLoaded: !this.settings.keepAllWorkspacesLoaded,
296 });
297 };
298
299 // Reactions
300
301 _setWorkspaceBeingEditedReaction = () => {
302 const { pathname } = this.stores.router.location;
303 const match = matchRoute('/settings/workspaces/edit/:id', pathname);
304 if (match) {
305 this._setWorkspaceBeingEdited(match);
306 }
307 };
308
309 _setActiveServiceOnWorkspaceSwitchReaction = () => {
310 if (!this.isFeatureActive) return;
311 if (this.activeWorkspace) {
312 const activeService = this.stores.services.active;
313 const workspaceServices = this.getWorkspaceServices(this.activeWorkspace);
314 if (workspaceServices.length <= 0) return;
315 const isActiveServiceInWorkspace =
316 workspaceServices.includes(activeService);
317 if (!isActiveServiceInWorkspace) {
318 this.actions.service.setActive({
319 serviceId: workspaceServices[0].id,
320 keepActiveRoute: true,
321 });
322 }
323 }
324 };
325
326 _activateLastUsedWorkspaceReaction = () => {
327 debug('_activateLastUsedWorkspaceReaction');
328 if (!this.activeWorkspace && this.userHasWorkspaces) {
329 const { lastActiveWorkspace } = this.settings;
330 if (lastActiveWorkspace) {
331 const workspace = this._getWorkspaceById(lastActiveWorkspace);
332 if (workspace) this._setActivateWorkspace({ workspace });
333 }
334 }
335 };
336
337 _openDrawerWithSettingsReaction = () => {
338 this._setOpenDrawerWithSettings();
339 };
340
341 _cleanupInvalidServiceReferences = () => {
342 const { services } = this.stores;
343 const { allServicesRequest } = services;
344 const servicesHaveBeenLoaded =
345 allServicesRequest.wasExecuted && !allServicesRequest.isError;
346 // Loop through all workspaces and remove invalid service ids (locally)
347 for (const workspace of this.workspaces) {
348 for (const serviceId of workspace.services) {
349 if (
350 servicesHaveBeenLoaded &&
351 !services.one(serviceId) &&
352 serviceId !== KEEP_WS_LOADED_USID
353 ) {
354 workspace.services.remove(serviceId);
355 }
356 }
357 }
358 };
359}