File size: 2,362 Bytes
1e92f2d |
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 |
import { Gridicon } from '@automattic/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { filter, get, flatMap } from 'lodash';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import { connect } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import { getCommentById } from 'calypso/state/comments/selectors';
import { getDocumentHeadCappedUnreadCount } from 'calypso/state/document-head/selectors/get-document-head-capped-unread-count';
import { getStream } from 'calypso/state/reader/streams/selectors';
import './style.scss';
const noop = () => {};
class UpdateNotice extends PureComponent {
static propTypes = {
streamKey: PropTypes.string,
onClick: PropTypes.func,
cappedUnreadCount: PropTypes.string,
};
static defaultProps = { onClick: noop };
render() {
const { count, cappedUnreadCount, translate } = this.props;
const counterClasses = clsx( {
'reader-update-notice': true,
'is-active': count > 0,
} );
return (
<button className={ counterClasses } onClick={ this.handleClick }>
<DocumentHead unreadCount={ count } />
<Gridicon icon="arrow-up" size={ 18 } />
{ translate( '%s new post', '%s new posts', {
args: [ cappedUnreadCount ],
count,
comment: '%s is the number of new posts. For example: "1" or "40+"',
} ) }
</button>
);
}
handleClick = ( event ) => {
event.preventDefault();
this.props.onClick();
};
}
const countNewComments = ( state, postKeys ) => {
const newComments = flatMap( postKeys, ( postKey ) => {
return filter( postKey.comments, ( commentId ) => {
return ! getCommentById( {
state,
siteId: postKey.blogId,
commentId: commentId,
} );
} );
} );
return newComments.length;
};
const mapStateToProps = ( state, ownProps ) => {
const stream = getStream( state, ownProps.streamKey );
const pendingItems = stream.pendingItems.items;
const updateCount = stream.pendingItems.items.length;
// ugly hack for convos
const isConversations = !! get( pendingItems, [ 0, 'comments' ] );
const count = isConversations ? countNewComments( state, pendingItems ) : updateCount;
return {
cappedUnreadCount: getDocumentHeadCappedUnreadCount( state ),
count,
};
};
export default connect( mapStateToProps )( localize( UpdateNotice ) );
|