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.

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