diff options
author | 2019-04-11 16:44:16 +0200 | |
---|---|---|
committer | 2019-04-11 16:44:16 +0200 | |
commit | eaf4aff646eed56e65c8dd8e70143ab5634ad4b4 (patch) | |
tree | ae400dca67edfd828a30da1e11d7e5e507785860 /src/features/announcements/store.js | |
parent | refactor announcements to newest feature pattern (diff) | |
download | ferdium-app-eaf4aff646eed56e65c8dd8e70143ab5634ad4b4.tar.gz ferdium-app-eaf4aff646eed56e65c8dd8e70143ab5634ad4b4.tar.zst ferdium-app-eaf4aff646eed56e65c8dd8e70143ab5634ad4b4.zip |
WIP: announcement feature and workspace fixes
Diffstat (limited to 'src/features/announcements/store.js')
-rw-r--r-- | src/features/announcements/store.js | 151 |
1 files changed, 88 insertions, 63 deletions
diff --git a/src/features/announcements/store.js b/src/features/announcements/store.js index c59700926..d4fb0a52c 100644 --- a/src/features/announcements/store.js +++ b/src/features/announcements/store.js | |||
@@ -1,96 +1,93 @@ | |||
1 | import { action, observable, reaction } from 'mobx'; | 1 | import { |
2 | action, | ||
3 | computed, | ||
4 | observable, | ||
5 | reaction, | ||
6 | } from 'mobx'; | ||
2 | import semver from 'semver'; | 7 | import semver from 'semver'; |
8 | import localStorage from 'mobx-localstorage'; | ||
9 | |||
3 | import { FeatureStore } from '../utils/FeatureStore'; | 10 | import { FeatureStore } from '../utils/FeatureStore'; |
4 | import { getAnnouncementRequest, getCurrentVersionRequest } from './api'; | 11 | import { getAnnouncementRequest, getChangelogRequest, getCurrentVersionRequest } from './api'; |
12 | import { announcementActions } from './actions'; | ||
13 | |||
14 | const LOCAL_STORAGE_KEY = 'announcements'; | ||
5 | 15 | ||
6 | const debug = require('debug')('Franz:feature:announcements:store'); | 16 | const debug = require('debug')('Franz:feature:announcements:store'); |
7 | 17 | ||
8 | export class AnnouncementsStore extends FeatureStore { | 18 | export class AnnouncementsStore extends FeatureStore { |
9 | 19 | @observable targetVersion = null; | |
10 | @observable announcement = null; | ||
11 | |||
12 | @observable currentVersion = null; | ||
13 | |||
14 | @observable lastUsedVersion = null; | ||
15 | 20 | ||
16 | @observable isAnnouncementVisible = false; | 21 | @observable isAnnouncementVisible = false; |
17 | 22 | ||
18 | @observable isFeatureActive = false; | 23 | @observable isFeatureActive = false; |
19 | 24 | ||
20 | async start(stores, actions) { | 25 | @computed get changelog() { |
21 | debug('AnnouncementsStore::start'); | 26 | return getChangelogRequest.result; |
22 | this.stores = stores; | ||
23 | this.actions = actions; | ||
24 | await this.fetchLastUsedVersion(); | ||
25 | await this.fetchCurrentVersion(); | ||
26 | await this.fetchReleaseAnnouncement(); | ||
27 | this.showAnnouncementIfNotSeenYet(); | ||
28 | |||
29 | this.actions.announcements.show.listen(this._showAnnouncement.bind(this)); | ||
30 | this.isFeatureActive = true; | ||
31 | } | 27 | } |
32 | 28 | ||
33 | stop() { | 29 | @computed get announcement() { |
34 | debug('AnnouncementsStore::stop'); | 30 | return getAnnouncementRequest.result; |
35 | this.isFeatureActive = false; | ||
36 | this.isAnnouncementVisible = false; | ||
37 | } | 31 | } |
38 | 32 | ||
39 | // ====== PUBLIC ====== | 33 | @computed get settings() { |
40 | 34 | return localStorage.getItem(LOCAL_STORAGE_KEY) || {}; | |
41 | async fetchLastUsedVersion() { | ||
42 | debug('getting last used version from local storage'); | ||
43 | const lastUsedVersion = window.localStorage.getItem('lastUsedVersion'); | ||
44 | this._setLastUsedVersion(lastUsedVersion == null ? '0.0.0' : lastUsedVersion); | ||
45 | } | 35 | } |
46 | 36 | ||
47 | async fetchCurrentVersion() { | 37 | @computed get lastSeenAnnouncementVersion() { |
48 | debug('getting current version from api'); | 38 | return this.settings.lastSeenAnnouncementVersion || null; |
49 | const version = await getCurrentVersionRequest.execute(); | ||
50 | this._setCurrentVersion(version); | ||
51 | } | 39 | } |
52 | 40 | ||
53 | async fetchReleaseAnnouncement() { | 41 | @computed get currentVersion() { |
54 | debug('getting release announcement from api'); | 42 | return getCurrentVersionRequest.result; |
55 | try { | ||
56 | const announcement = await getAnnouncementRequest.execute(this.currentVersion); | ||
57 | this._setAnnouncement(announcement); | ||
58 | } catch (error) { | ||
59 | this._setAnnouncement(null); | ||
60 | } | ||
61 | } | 43 | } |
62 | 44 | ||
63 | showAnnouncementIfNotSeenYet() { | 45 | @computed get isNewUser() { |
64 | const { announcement, currentVersion, lastUsedVersion } = this; | 46 | return this.stores.settings.stats.appStarts <= 1; |
65 | if (announcement && semver.gt(currentVersion, lastUsedVersion)) { | ||
66 | debug(`${currentVersion} < ${lastUsedVersion}: announcement is shown`); | ||
67 | this._showAnnouncement(); | ||
68 | } else { | ||
69 | debug(`${currentVersion} >= ${lastUsedVersion}: announcement is hidden`); | ||
70 | this._hideAnnouncement(); | ||
71 | } | ||
72 | } | 47 | } |
73 | 48 | ||
74 | // ====== PRIVATE ====== | 49 | async start(stores, actions) { |
50 | debug('AnnouncementsStore::start'); | ||
51 | this.stores = stores; | ||
52 | this.actions = actions; | ||
53 | getCurrentVersionRequest.execute(); | ||
75 | 54 | ||
76 | @action _setCurrentVersion(version) { | 55 | this._registerActions([ |
77 | debug(`setting current version to ${version}`); | 56 | [announcementActions.show, this._showAnnouncement], |
78 | this.currentVersion = version; | 57 | ]); |
79 | } | ||
80 | 58 | ||
81 | @action _setLastUsedVersion(version) { | 59 | this._registerReactions([ |
82 | debug(`setting last used version to ${version}`); | 60 | this._fetchAnnouncements, |
83 | this.lastUsedVersion = version; | 61 | this._showAnnouncementToUsersWhoUpdatedApp, |
62 | ]); | ||
63 | this.isFeatureActive = true; | ||
84 | } | 64 | } |
85 | 65 | ||
86 | @action _setAnnouncement(announcement) { | 66 | stop() { |
87 | debug(`setting announcement to ${announcement}`); | 67 | super.stop(); |
88 | this.announcement = announcement; | 68 | debug('AnnouncementsStore::stop'); |
69 | this.isFeatureActive = false; | ||
70 | this.isAnnouncementVisible = false; | ||
89 | } | 71 | } |
90 | 72 | ||
91 | @action _showAnnouncement() { | 73 | // ======= HELPERS ======= // |
74 | |||
75 | _updateSettings = (changes) => { | ||
76 | localStorage.setItem(LOCAL_STORAGE_KEY, { | ||
77 | ...this.settings, | ||
78 | ...changes, | ||
79 | }); | ||
80 | }; | ||
81 | |||
82 | // ======= ACTIONS ======= // | ||
83 | |||
84 | @action _showAnnouncement = ({ targetVersion } = {}) => { | ||
85 | this.targetVersion = targetVersion || this.currentVersion; | ||
92 | this.isAnnouncementVisible = true; | 86 | this.isAnnouncementVisible = true; |
93 | this.actions.service.blurActive(); | 87 | this.actions.service.blurActive(); |
88 | this._updateSettings({ | ||
89 | lastSeenAnnouncementVersion: this.currentVersion, | ||
90 | }); | ||
94 | const dispose = reaction( | 91 | const dispose = reaction( |
95 | () => this.stores.services.active, | 92 | () => this.stores.services.active, |
96 | () => { | 93 | () => { |
@@ -98,9 +95,37 @@ export class AnnouncementsStore extends FeatureStore { | |||
98 | dispose(); | 95 | dispose(); |
99 | }, | 96 | }, |
100 | ); | 97 | ); |
101 | } | 98 | }; |
102 | 99 | ||
103 | @action _hideAnnouncement() { | 100 | @action _hideAnnouncement() { |
104 | this.isAnnouncementVisible = false; | 101 | this.isAnnouncementVisible = false; |
105 | } | 102 | } |
103 | |||
104 | // ======= REACTIONS ======== | ||
105 | |||
106 | _showAnnouncementToUsersWhoUpdatedApp = () => { | ||
107 | const { announcement, isNewUser } = this; | ||
108 | console.log(announcement, isNewUser); | ||
109 | // Check if there is an announcement and on't show announcements to new users | ||
110 | if (!announcement || isNewUser) return; | ||
111 | |||
112 | this._showAnnouncement(); | ||
113 | |||
114 | // Check if the user has already used current version (= has seen the announcement) | ||
115 | // const { currentVersion, lastSeenAnnouncementVersion } = this; | ||
116 | // if (semver.gt(currentVersion, lastSeenAnnouncementVersion)) { | ||
117 | // debug(`${currentVersion} < ${lastSeenAnnouncementVersion}: announcement is shown`); | ||
118 | // this._showAnnouncement(); | ||
119 | // } else { | ||
120 | // debug(`${currentVersion} >= ${lastSeenAnnouncementVersion}: announcement is hidden`); | ||
121 | // this._hideAnnouncement(); | ||
122 | // } | ||
123 | }; | ||
124 | |||
125 | _fetchAnnouncements = () => { | ||
126 | const targetVersion = this.targetVersion || this.currentVersion; | ||
127 | if (!targetVersion) return; | ||
128 | getChangelogRequest.execute('5.0.1'); | ||
129 | getAnnouncementRequest.execute('5.1.0'); | ||
130 | } | ||
106 | } | 131 | } |