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.

186 lines
5.6 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import Column from '../../components/column';
  6. import ColumnHeader from '../../components/column_header';
  7. import { expandNotifications, scrollTopNotifications } from '../../actions/notifications';
  8. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  9. import NotificationContainer from './containers/notification_container';
  10. import { ScrollContainer } from 'react-router-scroll';
  11. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  12. import ColumnSettingsContainer from './containers/column_settings_container';
  13. import { createSelector } from 'reselect';
  14. import Immutable from 'immutable';
  15. import LoadMore from '../../components/load_more';
  16. import { debounce } from 'lodash';
  17. const messages = defineMessages({
  18. title: { id: 'column.notifications', defaultMessage: '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. hasMore: !!state.getIn(['notifications', 'next']),
  29. });
  30. @connect(mapStateToProps)
  31. @injectIntl
  32. export default class Notifications extends React.PureComponent {
  33. static propTypes = {
  34. columnId: PropTypes.string,
  35. notifications: ImmutablePropTypes.list.isRequired,
  36. dispatch: PropTypes.func.isRequired,
  37. shouldUpdateScroll: PropTypes.func,
  38. intl: PropTypes.object.isRequired,
  39. isLoading: PropTypes.bool,
  40. isUnread: PropTypes.bool,
  41. multiColumn: PropTypes.bool,
  42. hasMore: PropTypes.bool,
  43. };
  44. static defaultProps = {
  45. trackScroll: true,
  46. };
  47. dispatchExpandNotifications = debounce(() => {
  48. this.props.dispatch(expandNotifications());
  49. }, 300, { leading: true });
  50. dispatchScrollToTop = debounce((top) => {
  51. this.props.dispatch(scrollTopNotifications(top));
  52. }, 100);
  53. handleScroll = (e) => {
  54. const { scrollTop, scrollHeight, clientHeight } = e.target;
  55. const offset = scrollHeight - scrollTop - clientHeight;
  56. this._oldScrollPosition = scrollHeight - scrollTop;
  57. if (250 > offset && this.props.hasMore && !this.props.isLoading) {
  58. this.dispatchExpandNotifications();
  59. }
  60. if (scrollTop < 100) {
  61. this.dispatchScrollToTop(true);
  62. } else {
  63. this.dispatchScrollToTop(false);
  64. }
  65. }
  66. componentDidUpdate (prevProps) {
  67. if (this.node.scrollTop > 0 && (prevProps.notifications.size < this.props.notifications.size && prevProps.notifications.first() !== this.props.notifications.first() && !!this._oldScrollPosition)) {
  68. this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
  69. }
  70. }
  71. handleLoadMore = (e) => {
  72. e.preventDefault();
  73. this.dispatchExpandNotifications();
  74. }
  75. handlePin = () => {
  76. const { columnId, dispatch } = this.props;
  77. if (columnId) {
  78. dispatch(removeColumn(columnId));
  79. } else {
  80. dispatch(addColumn('NOTIFICATIONS', {}));
  81. }
  82. }
  83. handleMove = (dir) => {
  84. const { columnId, dispatch } = this.props;
  85. dispatch(moveColumn(columnId, dir));
  86. }
  87. handleHeaderClick = () => {
  88. this.column.scrollTop();
  89. }
  90. setRef = (c) => {
  91. this.node = c;
  92. }
  93. setColumnRef = c => {
  94. this.column = c;
  95. }
  96. render () {
  97. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread, columnId, multiColumn, hasMore } = this.props;
  98. const pinned = !!columnId;
  99. let loadMore = '';
  100. let scrollableArea = '';
  101. let unread = '';
  102. let scrollContainer = '';
  103. if (!isLoading && notifications.size > 0 && hasMore) {
  104. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  105. }
  106. if (isUnread) {
  107. unread = <div className='notifications__unread-indicator' />;
  108. }
  109. if (isLoading && this.scrollableArea) {
  110. scrollableArea = this.scrollableArea;
  111. } else if (notifications.size > 0) {
  112. scrollableArea = (
  113. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  114. {unread}
  115. <div>
  116. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  117. {loadMore}
  118. </div>
  119. </div>
  120. );
  121. } else {
  122. scrollableArea = (
  123. <div className='empty-column-indicator' ref={this.setRef}>
  124. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  125. </div>
  126. );
  127. }
  128. if (pinned) {
  129. scrollContainer = scrollableArea;
  130. } else {
  131. scrollContainer = (
  132. <ScrollContainer scrollKey={`notifications-${columnId}`} shouldUpdateScroll={shouldUpdateScroll}>
  133. {scrollableArea}
  134. </ScrollContainer>
  135. );
  136. }
  137. this.scrollableArea = scrollableArea;
  138. return (
  139. <Column ref={this.setColumnRef}>
  140. <ColumnHeader
  141. icon='bell'
  142. active={isUnread}
  143. title={intl.formatMessage(messages.title)}
  144. onPin={this.handlePin}
  145. onMove={this.handleMove}
  146. onClick={this.handleHeaderClick}
  147. pinned={pinned}
  148. multiColumn={multiColumn}
  149. >
  150. <ColumnSettingsContainer />
  151. </ColumnHeader>
  152. {scrollContainer}
  153. </Column>
  154. );
  155. }
  156. }