The code powering m.abunchtell.com https://m.abunchtell.com
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

176 行
5.3 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. componentWillUnmount () {
  47. this.handleScrollToBottom.cancel();
  48. this.handleScrollToTop.cancel();
  49. this.handleScroll.cancel();
  50. this.props.dispatch(scrollTopNotifications(false));
  51. }
  52. handleScrollToBottom = debounce(() => {
  53. this.props.dispatch(scrollTopNotifications(false));
  54. this.props.dispatch(expandNotifications());
  55. }, 300, { leading: true });
  56. handleScrollToTop = debounce(() => {
  57. this.props.dispatch(scrollTopNotifications(true));
  58. }, 100);
  59. handleScroll = debounce(() => {
  60. this.props.dispatch(scrollTopNotifications(false));
  61. }, 100);
  62. handlePin = () => {
  63. const { columnId, dispatch } = this.props;
  64. if (columnId) {
  65. dispatch(removeColumn(columnId));
  66. } else {
  67. dispatch(addColumn('NOTIFICATIONS', {}));
  68. }
  69. }
  70. handleMove = (dir) => {
  71. const { columnId, dispatch } = this.props;
  72. dispatch(moveColumn(columnId, dir));
  73. }
  74. handleHeaderClick = () => {
  75. this.column.scrollTop();
  76. }
  77. setColumnRef = c => {
  78. this.column = c;
  79. }
  80. handleMoveUp = id => {
  81. const elementIndex = this.props.notifications.findIndex(item => item.get('id') === id) - 1;
  82. this._selectChild(elementIndex);
  83. }
  84. handleMoveDown = id => {
  85. const elementIndex = this.props.notifications.findIndex(item => item.get('id') === id) + 1;
  86. this._selectChild(elementIndex);
  87. }
  88. _selectChild (index) {
  89. const element = this.column.node.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  90. if (element) {
  91. element.focus();
  92. }
  93. }
  94. render () {
  95. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread, columnId, multiColumn, hasMore } = this.props;
  96. const pinned = !!columnId;
  97. const emptyMessage = <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />;
  98. let scrollableContent = null;
  99. if (isLoading && this.scrollableContent) {
  100. scrollableContent = this.scrollableContent;
  101. } else if (notifications.size > 0 || hasMore) {
  102. scrollableContent = notifications.map((item) => (
  103. <NotificationContainer
  104. key={item.get('id')}
  105. notification={item}
  106. accountId={item.get('account')}
  107. onMoveUp={this.handleMoveUp}
  108. onMoveDown={this.handleMoveDown}
  109. />
  110. ));
  111. } else {
  112. scrollableContent = null;
  113. }
  114. this.scrollableContent = scrollableContent;
  115. const scrollContainer = (
  116. <ScrollableList
  117. scrollKey={`notifications-${columnId}`}
  118. trackScroll={!pinned}
  119. isLoading={isLoading}
  120. hasMore={hasMore}
  121. emptyMessage={emptyMessage}
  122. onScrollToBottom={this.handleScrollToBottom}
  123. onScrollToTop={this.handleScrollToTop}
  124. onScroll={this.handleScroll}
  125. shouldUpdateScroll={shouldUpdateScroll}
  126. >
  127. {scrollableContent}
  128. </ScrollableList>
  129. );
  130. return (
  131. <Column ref={this.setColumnRef}>
  132. <ColumnHeader
  133. icon='bell'
  134. active={isUnread}
  135. title={intl.formatMessage(messages.title)}
  136. onPin={this.handlePin}
  137. onMove={this.handleMove}
  138. onClick={this.handleHeaderClick}
  139. pinned={pinned}
  140. multiColumn={multiColumn}
  141. >
  142. <ColumnSettingsContainer />
  143. </ColumnHeader>
  144. {scrollContainer}
  145. </Column>
  146. );
  147. }
  148. }