This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
text_dismissable_bar.jsx
76 lines (63 loc) · 1.97 KB
/
text_dismissable_bar.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import PropTypes from 'prop-types';
import {trackEvent} from 'actions/telemetry_actions.jsx';
import Markdown from 'components/markdown';
import AnnouncementBar from './default_announcement_bar';
const localStoragePrefix = '__announcement__';
export default class TextDismissableBar extends React.PureComponent {
static propTypes = {
allowDismissal: PropTypes.bool.isRequired,
text: PropTypes.string.isRequired,
onDismissal: PropTypes.func,
// Any props that AnnouncementBar supports
};
constructor(props) {
super(props);
this.state = {
dismissed: true,
};
}
static getDerivedStateFromProps(props) {
const dismissed = localStorage.getItem(localStoragePrefix + props.text);
return {
dismissed,
};
}
handleDismiss = () => {
if (!this.props.allowDismissal) {
return;
}
trackEvent('signup', 'click_dismiss_bar');
localStorage.setItem(localStoragePrefix + this.props.text, true);
this.setState({
dismissed: true,
});
if (this.props.onDismissal) {
this.props.onDismissal();
}
}
render() {
if (this.state.dismissed) {
return null;
}
const {allowDismissal, text, ...extraProps} = this.props;
return (
<AnnouncementBar
{...extraProps}
showCloseButton={allowDismissal}
handleClose={this.handleDismiss}
message={
<Markdown
message={text}
options={{
singleline: true,
mentionHighlight: false,
}}
/>
}
/>
);
}
}