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