You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.6 KiB

  1. import { connect } from 'react-redux';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import Column from '../ui/components/column';
  5. import { expandNotifications, clearNotifications, scrollTopNotifications } from '../../actions/notifications';
  6. import NotificationContainer from './containers/notification_container';
  7. import { ScrollContainer } from 'react-router-scroll';
  8. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  9. import ColumnSettingsContainer from './containers/column_settings_container';
  10. import { createSelector } from 'reselect';
  11. import Immutable from 'immutable';
  12. import LoadMore from '../../components/load_more';
  13. import ClearColumnButton from './components/clear_column_button';
  14. const messages = defineMessages({
  15. title: { id: 'column.notifications', defaultMessage: 'Notifications' }
  16. });
  17. const getNotifications = createSelector([
  18. state => Immutable.List(state.getIn(['settings', 'notifications', 'shows']).filter(item => !item).keys()),
  19. state => state.getIn(['notifications', 'items'])
  20. ], (excludedTypes, notifications) => notifications.filterNot(item => excludedTypes.includes(item.get('type'))));
  21. const mapStateToProps = state => ({
  22. notifications: getNotifications(state),
  23. isLoading: state.getIn(['notifications', 'isLoading'], true),
  24. isUnread: state.getIn(['notifications', 'unread']) > 0
  25. });
  26. const Notifications = React.createClass({
  27. propTypes: {
  28. notifications: ImmutablePropTypes.list.isRequired,
  29. dispatch: React.PropTypes.func.isRequired,
  30. trackScroll: React.PropTypes.bool,
  31. intl: React.PropTypes.object.isRequired,
  32. isLoading: React.PropTypes.bool,
  33. isUnread: React.PropTypes.bool
  34. },
  35. getDefaultProps () {
  36. return {
  37. trackScroll: true
  38. };
  39. },
  40. mixins: [PureRenderMixin],
  41. handleScroll (e) {
  42. const { scrollTop, scrollHeight, clientHeight } = e.target;
  43. const offset = scrollHeight - scrollTop - clientHeight;
  44. this._oldScrollPosition = scrollHeight - scrollTop;
  45. if (250 > offset && !this.props.isLoading) {
  46. this.props.dispatch(expandNotifications());
  47. } else if (scrollTop < 100) {
  48. this.props.dispatch(scrollTopNotifications(true));
  49. } else {
  50. this.props.dispatch(scrollTopNotifications(false));
  51. }
  52. },
  53. componentDidUpdate (prevProps) {
  54. if (this.node.scrollTop > 0 && (prevProps.notifications.size < this.props.notifications.size && prevProps.notifications.first() !== this.props.notifications.first() && !!this._oldScrollPosition)) {
  55. this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
  56. }
  57. },
  58. handleLoadMore (e) {
  59. e.preventDefault();
  60. this.props.dispatch(expandNotifications());
  61. },
  62. handleClear () {
  63. this.props.dispatch(clearNotifications());
  64. },
  65. setRef (c) {
  66. this.node = c;
  67. },
  68. render () {
  69. const { intl, notifications, trackScroll, isLoading, isUnread } = this.props;
  70. let loadMore = '';
  71. let scrollableArea = '';
  72. let unread = '';
  73. if (!isLoading && notifications.size > 0) {
  74. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  75. }
  76. if (isUnread) {
  77. unread = <div className='notifications__unread-indicator' />;
  78. }
  79. if (isLoading || notifications.size > 0) {
  80. scrollableArea = (
  81. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  82. {unread}
  83. <div>
  84. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  85. {loadMore}
  86. </div>
  87. </div>
  88. );
  89. } else {
  90. scrollableArea = (
  91. <div className='empty-column-indicator' ref={this.setRef}>
  92. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  93. </div>
  94. );
  95. }
  96. if (trackScroll) {
  97. return (
  98. <Column icon='bell' active={isUnread} heading={intl.formatMessage(messages.title)}>
  99. <ColumnSettingsContainer />
  100. <ClearColumnButton onClick={this.handleClear} />
  101. <ScrollContainer scrollKey='notifications'>
  102. {scrollableArea}
  103. </ScrollContainer>
  104. </Column>
  105. );
  106. } else {
  107. return (
  108. <Column icon='bell' active={isUnread} heading={intl.formatMessage(messages.title)}>
  109. <ColumnSettingsContainer />
  110. <ClearColumnButton onClick={this.handleClear} />
  111. {scrollableArea}
  112. </Column>
  113. );
  114. }
  115. }
  116. });
  117. export default connect(mapStateToProps)(injectIntl(Notifications));