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.

177 lines
5.5 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. const messages = defineMessages({
  17. title: { id: 'column.notifications', defaultMessage: 'Notifications' },
  18. });
  19. const getNotifications = createSelector([
  20. state => Immutable.List(state.getIn(['settings', 'notifications', 'shows']).filter(item => !item).keys()),
  21. state => state.getIn(['notifications', 'items']),
  22. ], (excludedTypes, notifications) => notifications.filterNot(item => excludedTypes.includes(item.get('type'))));
  23. const mapStateToProps = state => ({
  24. notifications: getNotifications(state),
  25. isLoading: state.getIn(['notifications', 'isLoading'], true),
  26. isUnread: state.getIn(['notifications', 'unread']) > 0,
  27. hasMore: !!state.getIn(['notifications', 'next']),
  28. });
  29. class Notifications extends React.PureComponent {
  30. static propTypes = {
  31. columnId: PropTypes.string,
  32. notifications: ImmutablePropTypes.list.isRequired,
  33. dispatch: PropTypes.func.isRequired,
  34. shouldUpdateScroll: PropTypes.func,
  35. intl: PropTypes.object.isRequired,
  36. isLoading: PropTypes.bool,
  37. isUnread: PropTypes.bool,
  38. multiColumn: PropTypes.bool,
  39. hasMore: PropTypes.bool,
  40. };
  41. static defaultProps = {
  42. trackScroll: true,
  43. };
  44. handleScroll = (e) => {
  45. const { scrollTop, scrollHeight, clientHeight } = e.target;
  46. const offset = scrollHeight - scrollTop - clientHeight;
  47. this._oldScrollPosition = scrollHeight - scrollTop;
  48. if (250 > offset && !this.props.isLoading) {
  49. if (this.props.hasMore) {
  50. this.props.dispatch(expandNotifications());
  51. }
  52. } else if (scrollTop < 100) {
  53. this.props.dispatch(scrollTopNotifications(true));
  54. } else {
  55. this.props.dispatch(scrollTopNotifications(false));
  56. }
  57. }
  58. componentDidUpdate (prevProps) {
  59. if (this.node.scrollTop > 0 && (prevProps.notifications.size < this.props.notifications.size && prevProps.notifications.first() !== this.props.notifications.first() && !!this._oldScrollPosition)) {
  60. this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
  61. }
  62. }
  63. handleLoadMore = (e) => {
  64. e.preventDefault();
  65. this.props.dispatch(expandNotifications());
  66. }
  67. handlePin = () => {
  68. const { columnId, dispatch } = this.props;
  69. if (columnId) {
  70. dispatch(removeColumn(columnId));
  71. } else {
  72. dispatch(addColumn('NOTIFICATIONS', {}));
  73. }
  74. }
  75. handleMove = (dir) => {
  76. const { columnId, dispatch } = this.props;
  77. dispatch(moveColumn(columnId, dir));
  78. }
  79. handleHeaderClick = () => {
  80. this.column.scrollTop();
  81. }
  82. setRef = (c) => {
  83. this.node = c;
  84. }
  85. setColumnRef = c => {
  86. this.column = c;
  87. }
  88. render () {
  89. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread, columnId, multiColumn, hasMore } = this.props;
  90. const pinned = !!columnId;
  91. let loadMore = '';
  92. let scrollableArea = '';
  93. let unread = '';
  94. let scrollContainer = '';
  95. if (!isLoading && notifications.size > 0 && hasMore) {
  96. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  97. }
  98. if (isUnread) {
  99. unread = <div className='notifications__unread-indicator' />;
  100. }
  101. if (isLoading && this.scrollableArea) {
  102. scrollableArea = this.scrollableArea;
  103. } else if (notifications.size > 0) {
  104. scrollableArea = (
  105. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  106. {unread}
  107. <div>
  108. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  109. {loadMore}
  110. </div>
  111. </div>
  112. );
  113. } else {
  114. scrollableArea = (
  115. <div className='empty-column-indicator' ref={this.setRef}>
  116. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  117. </div>
  118. );
  119. }
  120. if (pinned) {
  121. scrollContainer = scrollableArea;
  122. } else {
  123. scrollContainer = (
  124. <ScrollContainer scrollKey={`notifications-${columnId}`} shouldUpdateScroll={shouldUpdateScroll}>
  125. {scrollableArea}
  126. </ScrollContainer>
  127. );
  128. }
  129. this.scrollableArea = scrollableArea;
  130. return (
  131. <Column ref={this.setColumnRef}>
  132. <ColumnHeader
  133. icon='bell'
  134. active={isUnread}
  135. title={intl.formatMessage(messages.title)}
  136. onPin={this.handlePin}
  137. onMove={this.handleMove}
  138. onClick={this.handleHeaderClick}
  139. pinned={pinned}
  140. multiColumn={multiColumn}
  141. >
  142. <ColumnSettingsContainer />
  143. </ColumnHeader>
  144. {scrollContainer}
  145. </Column>
  146. );
  147. }
  148. }
  149. export default connect(mapStateToProps)(injectIntl(Notifications));