闭社主体 forked from https://github.com/tootsuite/mastodon
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.

142 lines
5.0 KiB

  1. import { connect } from 'react-redux';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import Column from '../ui/components/column';
  5. import { expandNotifications, clearNotifications, scrollTopNotifications } from '../../actions/notifications';
  6. import NotificationContainer from './containers/notification_container';
  7. import { ScrollContainer } from 'react-router-scroll';
  8. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  9. import ColumnSettingsContainer from './containers/column_settings_container';
  10. import { createSelector } from 'reselect';
  11. import Immutable from 'immutable';
  12. import LoadMore from '../../components/load_more';
  13. import ClearColumnButton from './components/clear_column_button';
  14. import { openModal } from '../../actions/modal';
  15. const messages = defineMessages({
  16. title: { id: 'column.notifications', defaultMessage: 'Notifications' },
  17. clearMessage: { id: 'notifications.clear_confirmation', defaultMessage: 'Are you sure you want to permanently clear all your notifications?' },
  18. clearConfirm: { id: 'notifications.clear', defaultMessage: 'Clear notifications' }
  19. });
  20. const getNotifications = createSelector([
  21. state => Immutable.List(state.getIn(['settings', 'notifications', 'shows']).filter(item => !item).keys()),
  22. state => state.getIn(['notifications', 'items'])
  23. ], (excludedTypes, notifications) => notifications.filterNot(item => excludedTypes.includes(item.get('type'))));
  24. const mapStateToProps = state => ({
  25. notifications: getNotifications(state),
  26. isLoading: state.getIn(['notifications', 'isLoading'], true),
  27. isUnread: state.getIn(['notifications', 'unread']) > 0
  28. });
  29. class Notifications extends React.PureComponent {
  30. constructor (props, context) {
  31. super(props, context);
  32. this.handleScroll = this.handleScroll.bind(this);
  33. this.handleLoadMore = this.handleLoadMore.bind(this);
  34. this.handleClear = this.handleClear.bind(this);
  35. this.setRef = this.setRef.bind(this);
  36. }
  37. handleScroll (e) {
  38. const { scrollTop, scrollHeight, clientHeight } = e.target;
  39. const offset = scrollHeight - scrollTop - clientHeight;
  40. this._oldScrollPosition = scrollHeight - scrollTop;
  41. if (250 > offset && !this.props.isLoading) {
  42. this.props.dispatch(expandNotifications());
  43. } else if (scrollTop < 100) {
  44. this.props.dispatch(scrollTopNotifications(true));
  45. } else {
  46. this.props.dispatch(scrollTopNotifications(false));
  47. }
  48. }
  49. componentDidUpdate (prevProps) {
  50. if (this.node.scrollTop > 0 && (prevProps.notifications.size < this.props.notifications.size && prevProps.notifications.first() !== this.props.notifications.first() && !!this._oldScrollPosition)) {
  51. this.node.scrollTop = this.node.scrollHeight - this._oldScrollPosition;
  52. }
  53. }
  54. handleLoadMore (e) {
  55. e.preventDefault();
  56. this.props.dispatch(expandNotifications());
  57. }
  58. handleClear () {
  59. const { dispatch, intl } = this.props;
  60. dispatch(openModal('CONFIRM', {
  61. message: intl.formatMessage(messages.clearMessage),
  62. confirm: intl.formatMessage(messages.clearConfirm),
  63. onConfirm: () => dispatch(clearNotifications())
  64. }));
  65. }
  66. setRef (c) {
  67. this.node = c;
  68. }
  69. render () {
  70. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread } = this.props;
  71. let loadMore = '';
  72. let scrollableArea = '';
  73. let unread = '';
  74. if (!isLoading && notifications.size > 0) {
  75. loadMore = <LoadMore onClick={this.handleLoadMore} />;
  76. }
  77. if (isUnread) {
  78. unread = <div className='notifications__unread-indicator' />;
  79. }
  80. if (isLoading || notifications.size > 0) {
  81. scrollableArea = (
  82. <div className='scrollable' onScroll={this.handleScroll} ref={this.setRef}>
  83. {unread}
  84. <div>
  85. {notifications.map(item => <NotificationContainer key={item.get('id')} notification={item} accountId={item.get('account')} />)}
  86. {loadMore}
  87. </div>
  88. </div>
  89. );
  90. } else {
  91. scrollableArea = (
  92. <div className='empty-column-indicator' ref={this.setRef}>
  93. <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />
  94. </div>
  95. );
  96. }
  97. return (
  98. <Column icon='bell' active={isUnread} heading={intl.formatMessage(messages.title)}>
  99. <ColumnSettingsContainer />
  100. <ClearColumnButton onClick={this.handleClear} />
  101. <ScrollContainer scrollKey='notifications' shouldUpdateScroll={shouldUpdateScroll}>
  102. {scrollableArea}
  103. </ScrollContainer>
  104. </Column>
  105. );
  106. }
  107. }
  108. Notifications.propTypes = {
  109. notifications: ImmutablePropTypes.list.isRequired,
  110. dispatch: PropTypes.func.isRequired,
  111. shouldUpdateScroll: PropTypes.func,
  112. intl: PropTypes.object.isRequired,
  113. isLoading: PropTypes.bool,
  114. isUnread: PropTypes.bool
  115. };
  116. Notifications.defaultProps = {
  117. trackScroll: true
  118. };
  119. export default connect(mapStateToProps)(injectIntl(Notifications));