aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/workspaces/index.js')
-rw-r--r--src/features/workspaces/index.js40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/features/workspaces/index.js b/src/features/workspaces/index.js
index 50ac3b414..8091f49fc 100644
--- a/src/features/workspaces/index.js
+++ b/src/features/workspaces/index.js
@@ -1,14 +1,28 @@
1import { reaction } from 'mobx'; 1import { reaction, runInAction } from 'mobx';
2import WorkspacesStore from './store'; 2import WorkspacesStore from './store';
3import api from './api'; 3import api from './api';
4import { state, resetState } from './state'; 4import { workspacesState, resetState } from './state';
5 5
6const debug = require('debug')('Franz:feature:workspaces'); 6const debug = require('debug')('Franz:feature:workspaces');
7 7
8let store = null; 8let store = null;
9 9
10export const filterServicesByActiveWorkspace = (services) => {
11 const { isFeatureActive, activeWorkspace } = workspacesState;
12 if (isFeatureActive && activeWorkspace) {
13 return services.filter(s => activeWorkspace.services.includes(s.id));
14 }
15 return services;
16};
17
18export const getActiveWorkspaceServices = (services) => {
19 return filterServicesByActiveWorkspace(services);
20};
21
10export default function initWorkspaces(stores, actions) { 22export default function initWorkspaces(stores, actions) {
11 const { features, user } = stores; 23 const { features, user } = stores;
24
25 // Toggle workspace feature
12 reaction( 26 reaction(
13 () => ( 27 () => (
14 features.features.isWorkspaceEnabled && ( 28 features.features.isWorkspaceEnabled && (
@@ -18,10 +32,12 @@ export default function initWorkspaces(stores, actions) {
18 (isEnabled) => { 32 (isEnabled) => {
19 if (isEnabled) { 33 if (isEnabled) {
20 debug('Initializing `workspaces` feature'); 34 debug('Initializing `workspaces` feature');
21 store = new WorkspacesStore(stores, api, actions, state); 35 store = new WorkspacesStore(stores, api, actions, workspacesState);
22 store.initialize(); 36 store.initialize();
37 runInAction(() => { workspacesState.isFeatureActive = true; });
23 } else if (store) { 38 } else if (store) {
24 debug('Disabling `workspaces` feature'); 39 debug('Disabling `workspaces` feature');
40 runInAction(() => { workspacesState.isFeatureActive = false; });
25 store.teardown(); 41 store.teardown();
26 store = null; 42 store = null;
27 resetState(); // Reset state to default 43 resetState(); // Reset state to default
@@ -31,4 +47,22 @@ export default function initWorkspaces(stores, actions) {
31 fireImmediately: true, 47 fireImmediately: true,
32 }, 48 },
33 ); 49 );
50
51 // Update active service on workspace switches
52 reaction(() => ({
53 isFeatureActive: workspacesState.isFeatureActive,
54 activeWorkspace: workspacesState.activeWorkspace,
55 }), ({ isFeatureActive, activeWorkspace }) => {
56 if (!isFeatureActive) return;
57 if (activeWorkspace) {
58 const services = stores.services.allDisplayed;
59 const activeService = services.find(s => s.isActive);
60 const workspaceServices = filterServicesByActiveWorkspace(services);
61 const isActiveServiceInWorkspace = workspaceServices.includes(activeService);
62 if (!isActiveServiceInWorkspace) {
63 console.log(workspaceServices[0].id);
64 actions.service.setActive({ serviceId: workspaceServices[0].id });
65 }
66 }
67 });
34} 68}