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.

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