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.

178 lines
5.8 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. });
  32. 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. };
  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. this.props.dispatch(expandNotifications());
  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. handleClear = () => {
  68. const { dispatch, intl } = this.props;
  69. dispatch(openModal('CONFIRM', {
  70. message: intl.formatMessage(messages.clearMessage),
  71. confirm: intl.formatMessage(messages.clearConfirm),
  72. onConfirm: () => dispatch(clearNotifications()),
  73. }));
  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 } = this.props;
  98. const pinned = !!columnId;
  99. let loadMore = '';
  100. let scrollableArea = '';
  101. let unread = '';
  102. if (!isLoading && notifications.size > 0) {
  103. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  104. }
  105. if (isUnread) {
  106. unread = <div className='notifications__unread-indicator' />;
  107. }
  108. if (isLoading && this.scrollableArea) {
  109. scrollableArea = this.scrollableArea;
  110. } else if (notifications.size > 0) {
  111. scrollableArea = (
  112. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  113. {unread}
  114. <div>
  115. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  116. {loadMore}
  117. </div>
  118. </div>
  119. );
  120. } else {
  121. scrollableArea = (
  122. <div className='empty-column-indicator' ref={this.setRef}>
  123. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  124. </div>
  125. );
  126. }
  127. this.scrollableArea = scrollableArea;
  128. return (
  129. <Column ref={this.setColumnRef}>
  130. <ColumnHeader
  131. icon='bell'
  132. active={isUnread}
  133. title={intl.formatMessage(messages.title)}
  134. onPin={this.handlePin}
  135. onMove={this.handleMove}
  136. onClick={this.handleHeaderClick}
  137. pinned={pinned}
  138. multiColumn={multiColumn}
  139. >
  140. <ColumnSettingsContainer />
  141. </ColumnHeader>
  142. <ScrollContainer scrollKey={`notifications-${columnId}`} shouldUpdateScroll={shouldUpdateScroll}>
  143. {scrollableArea}
  144. </ScrollContainer>
  145. </Column>
  146. );
  147. }
  148. }
  149. export default connect(mapStateToProps)(injectIntl(Notifications));