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