-
Notifications
You must be signed in to change notification settings - Fork 2k
/
notifications.jsx
168 lines (145 loc) · 4.5 KB
/
notifications.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
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
/** @format */
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import ReactDom from 'react-dom';
import { connect } from 'react-redux';
import classNames from 'classnames';
import { partial } from 'lodash';
/**
* Internal dependencies
*/
import MasterbarItem from './item';
import AsyncLoad from 'components/async-load';
import store from 'store';
import { recordTracksEvent } from 'state/analytics/actions';
import { toggleNotificationsPanel } from 'state/ui/actions';
import isNotificationsOpen from 'state/selectors/is-notifications-open';
import TranslatableString from 'components/translatable/proptype';
class MasterbarItemNotifications extends Component {
static propTypes = {
user: PropTypes.object.isRequired,
isActive: PropTypes.bool,
className: PropTypes.string,
tooltip: TranslatableString,
//connected
isNotificationsOpen: PropTypes.bool,
};
state = {
animationState: 0,
};
componentWillMount() {
this.user = this.props.user.get();
this.setState( {
newNote: this.user && this.user.has_unseen_notes,
} );
}
componentWillReceiveProps( nextProps ) {
const { isNotificationsOpen: isOpen, recordOpening } = nextProps;
if ( ! this.props.isNotificationsOpen && isOpen ) {
recordOpening( {
unread_notifications: store.get( 'wpnotes_unseen_count' ),
} );
this.setNotesIndicator( 0 );
}
// focus on main window if we just closed the notes panel
if ( this.props.isNotificationsOpen && ! isOpen ) {
this.getNotificationLinkDomNode().blur();
window.focus();
}
}
checkToggleNotes = ( event, forceToggle ) => {
const target = event ? event.target : false;
const notificationNode = this.getNotificationLinkDomNode();
if ( target && notificationNode.contains( target ) ) {
return;
}
if ( this.props.isNotificationsOpen || forceToggle === true ) {
this.toggleNotesFrame( event );
}
};
toggleNotesFrame = event => {
if ( event ) {
event.preventDefault && event.preventDefault();
event.stopPropagation && event.stopPropagation();
}
this.props.toggleNotificationsPanel();
};
getNotificationLinkDomNode = () => {
return ReactDom.findDOMNode( this.refs.notificationLink );
};
/**
* Uses the passed number of unseen notifications
* and the locally-stored cache of that value to
* determine what state the notifications indicator
* should be in: on, off, or animate-to-on
*
* @param {Number} currentUnseenCount Number of reported unseen notifications
*/
setNotesIndicator = currentUnseenCount => {
const existingUnseenCount = store.get( 'wpnotes_unseen_count' );
let newAnimationState = this.state.animationState;
if ( 0 === currentUnseenCount ) {
// If we don't have new notes at load-time, remove the `-1` "init" status
newAnimationState = 0;
} else if ( currentUnseenCount > existingUnseenCount ) {
// Animate the indicator bubble by swapping CSS classes through the animation state
// Note that we could have an animation state of `-1` indicating the initial load
newAnimationState = 1 - Math.abs( this.state.animationState );
}
store.set( 'wpnotes_unseen_count', currentUnseenCount );
this.setState( {
newNote: currentUnseenCount > 0,
animationState: newAnimationState,
} );
};
render() {
const classes = classNames( this.props.className, {
'is-active': this.props.isNotificationsOpen,
'has-unread': this.state.newNote,
'is-initial-load': this.state.animationState === -1,
} );
return (
<div className="masterbar__notifications" ref="notificationLink">
<MasterbarItem
url="/notifications"
icon="bell"
onClick={ this.toggleNotesFrame }
isActive={ this.props.isActive }
tooltip={ this.props.tooltip }
className={ classes }
>
{ this.props.children }
<span
className="masterbar__notifications-bubble"
key={
'notification-indicator-animation-state-' + Math.abs( this.state.animationState )
}
/>
</MasterbarItem>
<AsyncLoad
require="notifications"
isShowing={ this.props.isNotificationsOpen }
checkToggle={ this.checkToggleNotes }
setIndicator={ this.setNotesIndicator }
placeholder={ null }
/>
</div>
);
}
}
const mapStateToProps = state => {
return {
isNotificationsOpen: isNotificationsOpen( state ),
};
};
const mapDispatchToProps = {
toggleNotificationsPanel,
recordOpening: partial( recordTracksEvent, 'calypso_notification_open' ),
};
export default connect(
mapStateToProps,
mapDispatchToProps
)( MasterbarItemNotifications );