The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

169 lines
5.1 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. handleMoveUp = id => {
  75. const elementIndex = this.props.notifications.findIndex(item => item.get('id') === id) - 1;
  76. this._selectChild(elementIndex);
  77. }
  78. handleMoveDown = id => {
  79. const elementIndex = this.props.notifications.findIndex(item => item.get('id') === id) + 1;
  80. this._selectChild(elementIndex);
  81. }
  82. _selectChild (index) {
  83. const element = this.column.node.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
  84. if (element) {
  85. element.focus();
  86. }
  87. }
  88. render () {
  89. const { intl, notifications, shouldUpdateScroll, isLoading, isUnread, columnId, multiColumn, hasMore } = this.props;
  90. const pinned = !!columnId;
  91. const emptyMessage = <FormattedMessage id='empty_column.notifications' defaultMessage="You don't have any notifications yet. Interact with others to start the conversation." />;
  92. let scrollableContent = null;
  93. if (isLoading && this.scrollableContent) {
  94. scrollableContent = this.scrollableContent;
  95. } else if (notifications.size > 0 || hasMore) {
  96. scrollableContent = notifications.map((item) => (
  97. <NotificationContainer
  98. key={item.get('id')}
  99. notification={item}
  100. accountId={item.get('account')}
  101. onMoveUp={this.handleMoveUp}
  102. onMoveDown={this.handleMoveDown}
  103. />
  104. ));
  105. } else {
  106. scrollableContent = null;
  107. }
  108. this.scrollableContent = scrollableContent;
  109. const scrollContainer = (
  110. <ScrollableList
  111. scrollKey={`notifications-${columnId}`}
  112. trackScroll={!pinned}
  113. isLoading={isLoading}
  114. hasMore={hasMore}
  115. emptyMessage={emptyMessage}
  116. onScrollToBottom={this.handleScrollToBottom}
  117. onScrollToTop={this.handleScrollToTop}
  118. onScroll={this.handleScroll}
  119. shouldUpdateScroll={shouldUpdateScroll}
  120. >
  121. {scrollableContent}
  122. </ScrollableList>
  123. );
  124. return (
  125. <Column ref={this.setColumnRef}>
  126. <ColumnHeader
  127. icon='bell'
  128. active={isUnread}
  129. title={intl.formatMessage(messages.title)}
  130. onPin={this.handlePin}
  131. onMove={this.handleMove}
  132. onClick={this.handleHeaderClick}
  133. pinned={pinned}
  134. multiColumn={multiColumn}
  135. >
  136. <ColumnSettingsContainer />
  137. </ColumnHeader>
  138. {scrollContainer}
  139. </Column>
  140. );
  141. }
  142. }