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.

146 lines
4.9 KiB

  1. import { connect } from 'react-redux';
  2. import PropTypes from 'prop-types';
  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. confirm: { id: 'notifications.clear_confirmation', defaultMessage: 'Are you sure you want to clear all your notifications?' }
  17. });
  18. const getNotifications = createSelector([
  19. state => Immutable.List(state.getIn(['settings', 'notifications', 'shows']).filter(item => !item).keys()),
  20. state => state.getIn(['notifications', 'items'])
  21. ], (excludedTypes, notifications) => notifications.filterNot(item => excludedTypes.includes(item.get('type'))));
  22. const mapStateToProps = state => ({
  23. notifications: getNotifications(state),
  24. isLoading: state.getIn(['notifications', 'isLoading'], true),
  25. isUnread: state.getIn(['notifications', 'unread']) > 0
  26. });
  27. class Notifications extends React.PureComponent {
  28. constructor (props, context) {
  29. super(props, context);
  30. this.handleScroll = this.handleScroll.bind(this);
  31. this.handleLoadMore = this.handleLoadMore.bind(this);
  32. this.handleClear = this.handleClear.bind(this);
  33. this.setRef = this.setRef.bind(this);
  34. }
  35. handleScroll (e) {
  36. const { scrollTop, scrollHeight, clientHeight } = e.target;
  37. const offset = scrollHeight - scrollTop - clientHeight;
  38. this._oldScrollPosition = scrollHeight - scrollTop;
  39. if (250 > offset && !this.props.isLoading) {
  40. this.props.dispatch(expandNotifications());
  41. } else if (scrollTop < 100) {
  42. this.props.dispatch(scrollTopNotifications(true));
  43. } else {
  44. this.props.dispatch(scrollTopNotifications(false));
  45. }
  46. }
  47. componentDidUpdate (prevProps) {
  48. if (this.node.scrollTop > 0 && (prevProps.notifications.size < this.props.notifications.size && prevProps.notifications.first() !== this.props.notifications.first() && !!this._oldScrollPosition)) {
  49. this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
  50. }
  51. }
  52. handleLoadMore (e) {
  53. e.preventDefault();
  54. this.props.dispatch(expandNotifications());
  55. }
  56. handleClear () {
  57. if (window.confirm(this.props.intl.formatMessage(messages.confirm))) {
  58. this.props.dispatch(clearNotifications());
  59. }
  60. }
  61. setRef (c) {
  62. this.node = c;
  63. }
  64. render () {
  65. const { intl, notifications, trackScroll, isLoading, isUnread } = this.props;
  66. let loadMore = '';
  67. let scrollableArea = '';
  68. let unread = '';
  69. if (!isLoading && notifications.size > 0) {
  70. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  71. }
  72. if (isUnread) {
  73. unread = <div className='notifications__unread-indicator' />;
  74. }
  75. if (isLoading || notifications.size > 0) {
  76. scrollableArea = (
  77. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  78. {unread}
  79. <div>
  80. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  81. {loadMore}
  82. </div>
  83. </div>
  84. );
  85. } else {
  86. scrollableArea = (
  87. <div className='empty-column-indicator' ref={this.setRef}>
  88. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  89. </div>
  90. );
  91. }
  92. if (trackScroll) {
  93. return (
  94. <Column icon='bell' active={isUnread} heading={intl.formatMessage(messages.title)}>
  95. <ColumnSettingsContainer />
  96. <ClearColumnButton onClick={this.handleClear} />
  97. <ScrollContainer scrollKey='notifications'>
  98. {scrollableArea}
  99. </ScrollContainer>
  100. </Column>
  101. );
  102. } else {
  103. return (
  104. <Column icon='bell' active={isUnread} heading={intl.formatMessage(messages.title)}>
  105. <ColumnSettingsContainer />
  106. <ClearColumnButton onClick={this.handleClear} />
  107. {scrollableArea}
  108. </Column>
  109. );
  110. }
  111. }
  112. }
  113. Notifications.propTypes = {
  114. notifications: ImmutablePropTypes.list.isRequired,
  115. dispatch: PropTypes.func.isRequired,
  116. trackScroll: PropTypes.bool,
  117. intl: PropTypes.object.isRequired,
  118. isLoading: PropTypes.bool,
  119. isUnread: PropTypes.bool
  120. };
  121. Notifications.defaultProps = {
  122. trackScroll: true
  123. };
  124. export default connect(mapStateToProps)(injectIntl(Notifications));