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.

141 lines
4.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 { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  11. import ColumnSettingsContainer from './containers/column_settings_container';
  12. import { createSelector } from 'reselect';
  13. import { List as ImmutableList } from 'immutable';
  14. import { debounce } from 'lodash';
  15. import ScrollableList from '../../components/scrollable_list';
  16. const messages = defineMessages({
  17. title: { id: 'column.notifications', defaultMessage: 'Notifications' },
  18. });
  19. const getNotifications = createSelector([
  20. state => ImmutableList(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. handleScrollToBottom = debounce(() => {
  47. this.props.dispatch(scrollTopNotifications(false));
  48. this.props.dispatch(expandNotifications());
  49. }, 300, { leading: true });
  50. handleScrollToTop = debounce(() => {
  51. this.props.dispatch(scrollTopNotifications(true));
  52. }, 100);
  53. handleScroll = debounce(() => {
  54. this.props.dispatch(scrollTopNotifications(false));
  55. }, 100);
  56. handlePin = () => {
  57. const { columnId, dispatch } = this.props;
  58. if (columnId) {
  59. dispatch(removeColumn(columnId));
  60. } else {
  61. dispatch(addColumn('NOTIFICATIONS', {}));
  62. }
  63. }
  64. handleMove = (dir) => {
  65. const { columnId, dispatch } = this.props;
  66. dispatch(moveColumn(columnId, dir));
  67. }
  68. handleHeaderClick = () => {
  69. this.column.scrollTop();
  70. }
  71. setColumnRef = c => {
  72. this.column = c;
  73. }
  74. render () {
  75. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread, columnId, multiColumn, hasMore } = this.props;
  76. const pinned = !!columnId;
  77. const emptyMessage = <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />;
  78. let scrollableContent = null;
  79. if (isLoading && this.scrollableContent) {
  80. scrollableContent = this.scrollableContent;
  81. } else if (notifications.size > 0 || hasMore) {
  82. scrollableContent = notifications.map((item) => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />);
  83. } else {
  84. scrollableContent = null;
  85. }
  86. this.scrollableContent = scrollableContent;
  87. const scrollContainer = (
  88. <ScrollableList
  89. scrollKey={`notifications-${columnId}`}
  90. isLoading={isLoading}
  91. hasMore={hasMore}
  92. emptyMessage={emptyMessage}
  93. onScrollToBottom={this.handleScrollToBottom}
  94. onScrollToTop={this.handleScrollToTop}
  95. onScroll={this.handleScroll}
  96. shouldUpdateScroll={shouldUpdateScroll}
  97. >
  98. {scrollableContent}
  99. </ScrollableList>
  100. );
  101. return (
  102. <Column ref={this.setColumnRef}>
  103. <ColumnHeader
  104. icon='bell'
  105. active={isUnread}
  106. title={intl.formatMessage(messages.title)}
  107. onPin={this.handlePin}
  108. onMove={this.handleMove}
  109. onClick={this.handleHeaderClick}
  110. pinned={pinned}
  111. multiColumn={multiColumn}
  112. >
  113. <ColumnSettingsContainer />
  114. </ColumnHeader>
  115. {scrollContainer}
  116. </Column>
  117. );
  118. }
  119. }