aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/announcements/store.js
blob: c597009260a1ba2de8e8533bf12503cf41ff9f1f (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { action, observable, reaction } from 'mobx';
import semver from 'semver';
import { FeatureStore } from '../utils/FeatureStore';
import { getAnnouncementRequest, getCurrentVersionRequest } from './api';

const debug = require('debug')('Franz:feature:announcements:store');

export class AnnouncementsStore extends FeatureStore {

  @observable announcement = null;

  @observable currentVersion = null;

  @observable lastUsedVersion = null;

  @observable isAnnouncementVisible = false;

  @observable isFeatureActive = false;

  async start(stores, actions) {
    debug('AnnouncementsStore::start');
    this.stores = stores;
    this.actions = actions;
    await this.fetchLastUsedVersion();
    await this.fetchCurrentVersion();
    await this.fetchReleaseAnnouncement();
    this.showAnnouncementIfNotSeenYet();

    this.actions.announcements.show.listen(this._showAnnouncement.bind(this));
    this.isFeatureActive = true;
  }

  stop() {
    debug('AnnouncementsStore::stop');
    this.isFeatureActive = false;
    this.isAnnouncementVisible = false;
  }

  // ====== PUBLIC ======

  async fetchLastUsedVersion() {
    debug('getting last used version from local storage');
    const lastUsedVersion = window.localStorage.getItem('lastUsedVersion');
    this._setLastUsedVersion(lastUsedVersion == null ? '0.0.0' : lastUsedVersion);
  }

  async fetchCurrentVersion() {
    debug('getting current version from api');
    const version = await getCurrentVersionRequest.execute();
    this._setCurrentVersion(version);
  }

  async fetchReleaseAnnouncement() {
    debug('getting release announcement from api');
    try {
      const announcement = await getAnnouncementRequest.execute(this.currentVersion);
      this._setAnnouncement(announcement);
    } catch (error) {
      this._setAnnouncement(null);
    }
  }

  showAnnouncementIfNotSeenYet() {
    const { announcement, currentVersion, lastUsedVersion } = this;
    if (announcement && semver.gt(currentVersion, lastUsedVersion)) {
      debug(`${currentVersion} < ${lastUsedVersion}: announcement is shown`);
      this._showAnnouncement();
    } else {
      debug(`${currentVersion} >= ${lastUsedVersion}: announcement is hidden`);
      this._hideAnnouncement();
    }
  }

  // ====== PRIVATE ======

  @action _setCurrentVersion(version) {
    debug(`setting current version to ${version}`);
    this.currentVersion = version;
  }

  @action _setLastUsedVersion(version) {
    debug(`setting last used version to ${version}`);
    this.lastUsedVersion = version;
  }

  @action _setAnnouncement(announcement) {
    debug(`setting announcement to ${announcement}`);
    this.announcement = announcement;
  }

  @action _showAnnouncement() {
    this.isAnnouncementVisible = true;
    this.actions.service.blurActive();
    const dispose = reaction(
      () => this.stores.services.active,
      () => {
        this._hideAnnouncement();
        dispose();
      },
    );
  }

  @action _hideAnnouncement() {
    this.isAnnouncementVisible = false;
  }
}