aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/announcements/store.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/announcements/store.js')
-rw-r--r--src/features/announcements/store.js61
1 files changed, 31 insertions, 30 deletions
diff --git a/src/features/announcements/store.js b/src/features/announcements/store.js
index 7ecc0e346..de7ed2596 100644
--- a/src/features/announcements/store.js
+++ b/src/features/announcements/store.js
@@ -2,18 +2,19 @@ import {
2 action, 2 action,
3 computed, 3 computed,
4 observable, 4 observable,
5 reaction,
6} from 'mobx'; 5} from 'mobx';
7import semver from 'semver'; 6import semver from 'semver';
8import localStorage from 'mobx-localstorage'; 7import localStorage from 'mobx-localstorage';
9 8
10import { FeatureStore } from '../utils/FeatureStore'; 9import { FeatureStore } from '../utils/FeatureStore';
11import { GA_CATEGORY_ANNOUNCEMENTS } from '.'; 10import { ANNOUNCEMENTS_ROUTES, GA_CATEGORY_ANNOUNCEMENTS } from '.';
12import { getAnnouncementRequest, getChangelogRequest, getCurrentVersionRequest } from './api'; 11import { getAnnouncementRequest, getChangelogRequest, getCurrentVersionRequest } from './api';
13import { announcementActions } from './actions'; 12import { announcementActions } from './actions';
14import { createActionBindings } from '../utils/ActionBinding'; 13import { createActionBindings } from '../utils/ActionBinding';
15import { createReactions } from '../../stores/lib/Reaction'; 14import { createReactions } from '../../stores/lib/Reaction';
16import { gaEvent } from '../../lib/analytics'; 15import { gaEvent } from '../../lib/analytics';
16import { matchRoute } from '../../helpers/routing-helpers';
17import { DEFAULT_APP_SETTINGS } from '../../config';
17 18
18const LOCAL_STORAGE_KEY = 'announcements'; 19const LOCAL_STORAGE_KEY = 'announcements';
19 20
@@ -22,8 +23,6 @@ const debug = require('debug')('Franz:feature:announcements:store');
22export class AnnouncementsStore extends FeatureStore { 23export class AnnouncementsStore extends FeatureStore {
23 @observable targetVersion = null; 24 @observable targetVersion = null;
24 25
25 @observable isAnnouncementVisible = false;
26
27 @observable isFeatureActive = false; 26 @observable isFeatureActive = false;
28 27
29 @computed get changelog() { 28 @computed get changelog() {
@@ -31,7 +30,15 @@ export class AnnouncementsStore extends FeatureStore {
31 } 30 }
32 31
33 @computed get announcement() { 32 @computed get announcement() {
34 return getAnnouncementRequest.result; 33 if (!this.stores || !getAnnouncementRequest.result) return null;
34 const { locale } = this.stores.app;
35 const announcement = getAnnouncementRequest.result;
36 // User locale
37 if (announcement[locale]) return announcement[locale];
38 // Default locale
39 if (announcement[DEFAULT_APP_SETTINGS.fallbackLocale]) return announcement[DEFAULT_APP_SETTINGS.fallbackLocale];
40 // No locales specified
41 return announcement;
35 } 42 }
36 43
37 @computed get areNewsAvailable() { 44 @computed get areNewsAvailable() {
@@ -67,8 +74,9 @@ export class AnnouncementsStore extends FeatureStore {
67 ])); 74 ]));
68 75
69 this._reactions = createReactions([ 76 this._reactions = createReactions([
70 this._fetchAnnouncements, 77 this._showAnnouncementOnRouteMatch,
71 this._showAnnouncementToUsersWhoUpdatedApp, 78 this._showAnnouncementToUsersWhoUpdatedApp,
79 this._fetchAnnouncements,
72 ]); 80 ]);
73 this._registerReactions(this._reactions); 81 this._registerReactions(this._reactions);
74 this.isFeatureActive = true; 82 this.isFeatureActive = true;
@@ -78,7 +86,6 @@ export class AnnouncementsStore extends FeatureStore {
78 super.stop(); 86 super.stop();
79 debug('AnnouncementsStore::stop'); 87 debug('AnnouncementsStore::stop');
80 this.isFeatureActive = false; 88 this.isFeatureActive = false;
81 this.isAnnouncementVisible = false;
82 } 89 }
83 90
84 // ======= HELPERS ======= // 91 // ======= HELPERS ======= //
@@ -93,39 +100,29 @@ export class AnnouncementsStore extends FeatureStore {
93 // ======= ACTIONS ======= // 100 // ======= ACTIONS ======= //
94 101
95 @action _showAnnouncement = ({ targetVersion } = {}) => { 102 @action _showAnnouncement = ({ targetVersion } = {}) => {
96 if (!this.areNewsAvailable) return; 103 const { router } = this.stores;
97 this.targetVersion = targetVersion || this.currentVersion; 104 this.targetVersion = targetVersion || this.currentVersion;
98 this.isAnnouncementVisible = true;
99 this.actions.service.blurActive();
100 this._updateSettings({ 105 this._updateSettings({
101 lastSeenAnnouncementVersion: this.currentVersion, 106 lastSeenAnnouncementVersion: this.currentVersion,
102 }); 107 });
103 const dispose = reaction( 108 const targetRoute = `/announcements/${this.targetVersion}`;
104 () => this.stores.services.active, 109 if (router.location.pathname !== targetRoute) {
105 () => { 110 this.stores.router.push(targetRoute);
106 this._hideAnnouncement(); 111 }
107 dispose();
108 },
109 );
110
111 gaEvent(GA_CATEGORY_ANNOUNCEMENTS, 'show'); 112 gaEvent(GA_CATEGORY_ANNOUNCEMENTS, 'show');
112 }; 113 };
113 114
114 @action _hideAnnouncement() {
115 this.isAnnouncementVisible = false;
116 }
117
118 // ======= REACTIONS ======== 115 // ======= REACTIONS ========
119 116
120 _showAnnouncementToUsersWhoUpdatedApp = () => { 117 _showAnnouncementToUsersWhoUpdatedApp = () => {
121 const { announcement, isNewUser } = this; 118 const { announcement, isNewUser } = this;
122 // Check if there is an announcement and on't show announcements to new users 119 // Check if there is an announcement and don't show announcements to new users
123 if (!announcement || isNewUser) return; 120 if (!announcement || isNewUser) return;
124 121
125 // Check if the user has already used current version (= has seen the announcement) 122 // Check if the user has already used current version (= has seen the announcement)
126 const { currentVersion, lastSeenAnnouncementVersion } = this; 123 const { currentVersion, lastSeenAnnouncementVersion } = this;
127 if (semver.gt(currentVersion, lastSeenAnnouncementVersion || '0.0.0')) { 124 if (semver.gt(currentVersion, lastSeenAnnouncementVersion || '0.0.0')) {
128 debug(`${currentVersion} < ${lastSeenAnnouncementVersion}: announcement is shown`); 125 debug(`${currentVersion} > ${lastSeenAnnouncementVersion}: announcement is shown`);
129 this._showAnnouncement(); 126 this._showAnnouncement();
130 } 127 }
131 }; 128 };
@@ -133,12 +130,16 @@ export class AnnouncementsStore extends FeatureStore {
133 _fetchAnnouncements = () => { 130 _fetchAnnouncements = () => {
134 const targetVersion = this.targetVersion || this.currentVersion; 131 const targetVersion = this.targetVersion || this.currentVersion;
135 if (!targetVersion) return; 132 if (!targetVersion) return;
136 getChangelogRequest.execute(targetVersion); 133 getChangelogRequest.reset().execute(targetVersion);
137 // We only fetch announcements for current / older versions 134 getAnnouncementRequest.reset().execute(targetVersion);
138 if (targetVersion <= this.currentVersion) { 135 };
139 getAnnouncementRequest.execute(targetVersion); 136
140 } else { 137 _showAnnouncementOnRouteMatch = () => {
141 getAnnouncementRequest.reset(); 138 const { router } = this.stores;
139 const match = matchRoute(ANNOUNCEMENTS_ROUTES.TARGET, router.location.pathname);
140 if (match) {
141 const targetVersion = match.id;
142 this._showAnnouncement({ targetVersion });
142 } 143 }
143 } 144 }
144} 145}