summaryrefslogtreecommitdiffstats
path: root/webapp/components/announcement_bar/announcement_bar.jsx
blob: 0e731fd7c5e76e3879c9e386bfc24f56e1515b4f (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';
import {FormattedMessage, FormattedHTMLMessage} from 'react-intl';
import {Link} from 'react-router';

import AnalyticsStore from 'stores/analytics_store.jsx';
import ErrorStore from 'stores/error_store.jsx';
import UserStore from 'stores/user_store.jsx';

import * as AdminActions from 'actions/admin_actions.jsx';
import {ErrorBarTypes, StatTypes} from 'utils/constants.jsx';
import {isLicenseExpiring, isLicenseExpired, isLicensePastGracePeriod, displayExpiryDate} from 'utils/license_utils.jsx';
import * as Utils from 'utils/utils.jsx';

const RENEWAL_LINK = 'https://licensing.mattermost.com/renew';

const BAR_DEVELOPER_TYPE = 'developer';
const BAR_CRITICAL_TYPE = 'critical';
const BAR_ANNOUNCEMENT_TYPE = 'announcement';

const ANNOUNCEMENT_KEY = 'announcement--';

export default class AnnouncementBar extends React.PureComponent {
    static propTypes = {

        /*
         * Set if the user is logged in
         */
        isLoggedIn: React.PropTypes.bool.isRequired
    }

    constructor() {
        super();

        this.onErrorChange = this.onErrorChange.bind(this);
        this.onAnalyticsChange = this.onAnalyticsChange.bind(this);
        this.handleClose = this.handleClose.bind(this);

        ErrorStore.clearLastError();

        this.setInitialError();

        this.state = this.getState();
    }

    setInitialError() {
        let isSystemAdmin = false;
        const user = UserStore.getCurrentUser();
        if (user) {
            isSystemAdmin = Utils.isSystemAdmin(user.roles);
        }

        const errorIgnored = ErrorStore.getIgnoreNotification();

        if (!errorIgnored) {
            if (isSystemAdmin && global.mm_config.SiteURL === '') {
                ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.SITE_URL});
                return;
            } else if (global.mm_config.SendEmailNotifications === 'false') {
                ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.PREVIEW_MODE});
                return;
            }
        }

        if (isLicensePastGracePeriod()) {
            if (isSystemAdmin) {
                ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.LICENSE_EXPIRED, type: BAR_CRITICAL_TYPE});
            } else {
                ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.LICENSE_PAST_GRACE, type: BAR_CRITICAL_TYPE});
            }
        } else if (isLicenseExpired() && isSystemAdmin) {
            ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.LICENSE_EXPIRED, type: BAR_CRITICAL_TYPE});
        } else if (isLicenseExpiring() && isSystemAdmin) {
            ErrorStore.storeLastError({notification: true, message: ErrorBarTypes.LICENSE_EXPIRING, type: BAR_CRITICAL_TYPE});
        }
    }

    getState() {
        const error = ErrorStore.getLastError();
        if (error) {
            return {message: error.message, color: null, textColor: null, type: error.type, allowDismissal: true};
        }

        const bannerText = global.window.mm_config.BannerText || '';
        const allowDismissal = global.window.mm_config.AllowBannerDismissal === 'true';
        const bannerDismissed = localStorage.getItem(ANNOUNCEMENT_KEY + global.window.mm_config.BannerText);

        if (global.window.mm_config.EnableBanner === 'true' &&
                bannerText.length > 0 &&
                (!bannerDismissed || !allowDismissal)) {
            // Remove old local storage items
            Utils.removePrefixFromLocalStorage(ANNOUNCEMENT_KEY);
            return {
                message: bannerText,
                color: global.window.mm_config.BannerColor,
                textColor: global.window.mm_config.BannerTextColor,
                type: BAR_ANNOUNCEMENT_TYPE,
                allowDismissal
            };
        }

        return {message: null, color: null, colorText: null, textColor: null, type: null, allowDismissal: true};
    }

    isValidState(s) {
        if (!s) {
            return false;
        }

        if (!s.message) {
            return false;
        }

        if (s.message === ErrorBarTypes.LICENSE_EXPIRING && !this.state.totalUsers) {
            return false;
        }

        return true;
    }

    componentDidMount() {
        if (this.props.isLoggedIn && !this.state.allowDismissal) {
            document.body.classList.add('error-bar--fixed');
        }

        ErrorStore.addChangeListener(this.onErrorChange);
        AnalyticsStore.addChangeListener(this.onAnalyticsChange);
    }

    componentWillUnmount() {
        document.body.classList.remove('error-bar--fixed');
        ErrorStore.removeChangeListener(this.onErrorChange);
        AnalyticsStore.removeChangeListener(this.onAnalyticsChange);
    }

    componentDidUpdate(prevProps, prevState) {
        if (!this.props.isLoggedIn) {
            return;
        }

        if (!prevState.allowDismissal && this.state.allowDismissal) {
            document.body.classList.remove('error-bar--fixed');
        } else if (prevState.allowDismissal && !this.state.allowDismissal) {
            document.body.classList.add('error-bar--fixed');
        }
    }

    onErrorChange() {
        const newState = this.getState();
        if (newState.message === ErrorBarTypes.LICENSE_EXPIRING && !this.state.totalUsers) {
            AdminActions.getStandardAnalytics();
        }
        this.setState(newState);
    }

    onAnalyticsChange() {
        const stats = AnalyticsStore.getAllSystem();
        this.setState({totalUsers: stats[StatTypes.TOTAL_USERS]});
    }

    handleClose(e) {
        if (e) {
            e.preventDefault();
        }

        if (this.state.type === BAR_ANNOUNCEMENT_TYPE) {
            localStorage.setItem(ANNOUNCEMENT_KEY + this.state.message, true);
        }

        if (ErrorStore.getLastError() && ErrorStore.getLastError().notification) {
            ErrorStore.clearNotificationError();
        } else {
            ErrorStore.clearLastError();
        }

        this.setState(this.getState());
    }

    render() {
        if (!this.isValidState(this.state)) {
            return <div/>;
        }

        if (!this.props.isLoggedIn && this.state.type === BAR_ANNOUNCEMENT_TYPE) {
            return <div/>;
        }

        let errClass = 'error-bar';
        let dismissClass = ' error-bar--fixed';
        const barStyle = {};
        const linkStyle = {};
        if (this.state.color && this.state.textColor) {
            barStyle.backgroundColor = this.state.color;
            barStyle.color = this.state.textColor;
            linkStyle.color = this.state.textColor;
        } else if (this.state.type === BAR_DEVELOPER_TYPE) {
            errClass = 'error-bar-developer';
        } else if (this.state.type === BAR_CRITICAL_TYPE) {
            errClass = 'error-bar-critical';
        }

        let closeButton;
        if (this.state.allowDismissal) {
            dismissClass = '';
            closeButton = (
                <a
                    href='#'
                    className='error-bar__close'
                    style={linkStyle}
                    onClick={this.handleClose}
                >
                    {'×'}
                </a>
            );
        }

        const renewalLink = RENEWAL_LINK + '?id=' + global.window.mm_license.Id + '&user_count=' + this.state.totalUsers;

        let message = this.state.message;
        if (message === ErrorBarTypes.PREVIEW_MODE) {
            message = (
                <FormattedMessage
                    id={ErrorBarTypes.PREVIEW_MODE}
                    defaultMessage='Preview Mode: Email notifications have not been configured'
                />
            );
        } else if (message === ErrorBarTypes.LICENSE_EXPIRING) {
            message = (
                <FormattedHTMLMessage
                    id={ErrorBarTypes.LICENSE_EXPIRING}
                    defaultMessage='Enterprise license expires on {date}. <a href="{link}" target="_blank">Please renew</a>.'
                    values={{
                        date: displayExpiryDate(),
                        link: renewalLink
                    }}
                />
            );
        } else if (message === ErrorBarTypes.LICENSE_EXPIRED) {
            message = (
                <FormattedHTMLMessage
                    id={ErrorBarTypes.LICENSE_EXPIRED}
                    defaultMessage='Enterprise license is expired and some features may be disabled. <a href="{link}" target="_blank">Please renew</a>.'
                    values={{
                        link: renewalLink
                    }}
                />
            );
        } else if (message === ErrorBarTypes.LICENSE_PAST_GRACE) {
            message = (
                <FormattedMessage
                    id={ErrorBarTypes.LICENSE_PAST_GRACE}
                    defaultMessage='Enterprise license is expired and some features may be disabled. Please contact your System Administrator for details.'
                />
            );
        } else if (message === ErrorBarTypes.WEBSOCKET_PORT_ERROR) {
            message = (
                <FormattedHTMLMessage
                    id={ErrorBarTypes.WEBSOCKET_PORT_ERROR}
                    defaultMessage='Please check connection, Mattermost unreachable. If issue persists, ask administrator to <a href="https://about.mattermost.com/default-websocket-port-help" target="_blank">check WebSocket port</a>.'
                />
            );
        } else if (message === ErrorBarTypes.SITE_URL) {
            let id;
            let defaultMessage;
            if (global.mm_config.EnableSignUpWithGitLab === 'true') {
                id = 'error_bar.site_url_gitlab';
                defaultMessage = 'Please configure your {docsLink} in the System Console or in gitlab.rb if you\'re using GitLab Mattermost.';
            } else {
                id = 'error_bar.site_url';
                defaultMessage = 'Please configure your {docsLink} in the System Console.';
            }

            message = (
                <FormattedMessage
                    id={id}
                    defaultMessage={defaultMessage}
                    values={{
                        docsLink: (
                            <a
                                href='https://docs.mattermost.com/administration/config-settings.html#site-url'
                                rel='noopener noreferrer'
                                target='_blank'
                            >
                                <FormattedMessage
                                    id='error_bar.site_url.docsLink'
                                    defaultMessage='Site URL'
                                />
                            </a>
                        ),
                        link: (
                            <Link to='/admin_console/general/configuration'>
                                <FormattedMessage
                                    id='error_bar.site_url.link'
                                    defaultMessage='the System Console'
                                />
                            </Link>
                        )
                    }}
                />
            );
        }

        return (
            <div
                className={errClass + dismissClass}
                style={barStyle}
            >
                <span>{message}</span>
                {closeButton}
            </div>
        );
    }
}