闭社主体 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.

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