aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/index.js
blob: b4cfd3c2d73010ddff29c5c1f980a8c832749788 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { observable, reaction } from 'mobx';
import { merge } from 'lodash';
import WorkspacesStore from './store';
import api from './api';

const debug = require('debug')('Franz:feature:workspaces');

let store = null;
const defaultState = { workspaces: [] };

export const state = observable(defaultState);

export default function initWorkspaces(stores, actions) {
  const { features, user } = stores;
  reaction(
    () => (
      features.features.isWorkspaceEnabled && (
        !features.features.isWorkspacePremiumFeature || user.data.isPremium
      )
    ),
    (isEnabled) => {
      if (isEnabled) {
        debug('Initializing `workspaces` feature');
        store = new WorkspacesStore(stores, api, actions, state);
        store.initialize();
      } else if (store) {
        debug('Disabling `workspaces` feature');
        store.teardown();
        store = null;
        // Reset state to default
        merge(state, defaultState);
      }
    },
    {
      fireImmediately: true,
    },
  );
}