aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/delayApp/index.js
blob: 910b54959b492e749aff1613ae36ad55412f22c3 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { autorun, observable, reaction } from 'mobx';
import moment from 'moment';
import DelayAppComponent from './Component';

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

const DEFAULT_DELAY_DURATION = 10000; // 10 seconds
const DEFAULT_DELAY_OFFSET = 3600000; // 1 hour
const DEFAULT_VISIBILITY = false;

export const config = {
  delayOffset: DEFAULT_DELAY_OFFSET,
  delayDuration: DEFAULT_DELAY_DURATION,
};

export const state = observable({
  isDelayAppScreenVisible: DEFAULT_VISIBILITY,
});

function setVisibility(value) {
  Object.assign(state, {
    isDelayAppScreenVisible: value,
  });
}

export default function init(stores) {
  reaction(
    () => stores.features.features.needToWaitToProceed,
    (enabled, r) => {
      if (enabled) {
        debug('Initializing `delayApp` feature');

        // Dispose the reaction to run this only once
        r.dispose();

        const { needToWaitToProceedConfig: globalConfig } = stores.features.features;

        let shownAfterLaunch = false;
        let timeLastDelay = moment();

        config.delayOffset = globalConfig.delayOffset || DEFAULT_DELAY_OFFSET;
        config.delayDuration = globalConfig.wait || DEFAULT_DELAY_DURATION;

        autorun(() => {
          const diff = moment().diff(timeLastDelay);
          if ((stores.app.isFocused && diff >= config.delayOffset) || !shownAfterLaunch) {
            debug(`App will be delayed for ${config.delayDuration / 1000}s`);

            setVisibility(true);

            timeLastDelay = moment();
            shownAfterLaunch = true;

            setTimeout(() => {
              debug('Resetting app delay');

              setVisibility(false);
            }, DEFAULT_DELAY_DURATION + 1000); // timer needs to be able to hit 0
          }
        });
      }
    },
  );
}

export const Component = DelayAppComponent;