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.

173 lines
5.7 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { injectIntl } from 'react-intl';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import ImmutablePureComponent from 'react-immutable-pure-component';
  6. import ReactSwipeableViews from 'react-swipeable-views';
  7. import { links, getIndex, getLink } from './tabs_bar';
  8. import BundleContainer from '../containers/bundle_container';
  9. import ColumnLoading from './column_loading';
  10. import DrawerLoading from './drawer_loading';
  11. import BundleColumnError from './bundle_column_error';
  12. import { Compose, Notifications, HomeTimeline, CommunityTimeline, PublicTimeline, HashtagTimeline, FavouritedStatuses } from '../../ui/util/async-components';
  13. import detectPassiveEvents from 'detect-passive-events';
  14. import { scrollRight } from '../../../scroll';
  15. const componentMap = {
  16. 'COMPOSE': Compose,
  17. 'HOME': HomeTimeline,
  18. 'NOTIFICATIONS': Notifications,
  19. 'PUBLIC': PublicTimeline,
  20. 'COMMUNITY': CommunityTimeline,
  21. 'HASHTAG': HashtagTimeline,
  22. 'FAVOURITES': FavouritedStatuses,
  23. };
  24. @component => injectIntl(component, { withRef: true })
  25. export default class ColumnsArea extends ImmutablePureComponent {
  26. static contextTypes = {
  27. router: PropTypes.object.isRequired,
  28. };
  29. static propTypes = {
  30. intl: PropTypes.object.isRequired,
  31. columns: ImmutablePropTypes.list.isRequired,
  32. singleColumn: PropTypes.bool,
  33. children: PropTypes.node,
  34. };
  35. state = {
  36. shouldAnimate: false,
  37. }
  38. componentWillReceiveProps() {
  39. this.setState({ shouldAnimate: false });
  40. }
  41. componentDidMount() {
  42. if (!this.props.singleColumn) {
  43. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  44. }
  45. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  46. this.setState({ shouldAnimate: true });
  47. }
  48. componentWillUpdate(nextProps) {
  49. if (this.props.singleColumn !== nextProps.singleColumn && nextProps.singleColumn) {
  50. this.node.removeEventListener('wheel', this.handleWheel);
  51. }
  52. }
  53. componentDidUpdate(prevProps) {
  54. if (this.props.singleColumn !== prevProps.singleColumn && !this.props.singleColumn) {
  55. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  56. }
  57. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  58. this.setState({ shouldAnimate: true });
  59. }
  60. componentWillUnmount () {
  61. if (!this.props.singleColumn) {
  62. this.node.removeEventListener('wheel', this.handleWheel);
  63. }
  64. }
  65. handleChildrenContentChange() {
  66. if (!this.props.singleColumn) {
  67. this._interruptScrollAnimation = scrollRight(this.node, this.node.scrollWidth - window.innerWidth);
  68. }
  69. }
  70. handleSwipe = (index) => {
  71. this.pendingIndex = index;
  72. const nextLinkTranslationId = links[index].props['data-preview-title-id'];
  73. const currentLinkSelector = '.tabs-bar__link.active';
  74. const nextLinkSelector = `.tabs-bar__link[data-preview-title-id="${nextLinkTranslationId}"]`;
  75. // HACK: Remove the active class from the current link and set it to the next one
  76. // React-router does this for us, but too late, feeling laggy.
  77. document.querySelector(currentLinkSelector).classList.remove('active');
  78. document.querySelector(nextLinkSelector).classList.add('active');
  79. }
  80. handleAnimationEnd = () => {
  81. if (typeof this.pendingIndex === 'number') {
  82. this.context.router.history.push(getLink(this.pendingIndex));
  83. this.pendingIndex = null;
  84. }
  85. }
  86. handleWheel = () => {
  87. if (typeof this._interruptScrollAnimation !== 'function') {
  88. return;
  89. }
  90. this._interruptScrollAnimation();
  91. }
  92. setRef = (node) => {
  93. this.node = node;
  94. }
  95. renderView = (link, index) => {
  96. const columnIndex = getIndex(this.context.router.history.location.pathname);
  97. const title = this.props.intl.formatMessage({ id: link.props['data-preview-title-id'] });
  98. const icon = link.props['data-preview-icon'];
  99. const view = (index === columnIndex) ?
  100. React.cloneElement(this.props.children) :
  101. <ColumnLoading title={title} icon={icon} />;
  102. return (
  103. <div className='columns-area' key={index}>
  104. {view}
  105. </div>
  106. );
  107. }
  108. renderLoading = columnId => () => {
  109. return columnId === 'COMPOSE' ? <DrawerLoading /> : <ColumnLoading />;
  110. }
  111. renderError = (props) => {
  112. return <BundleColumnError {...props} />;
  113. }
  114. render () {
  115. const { columns, children, singleColumn } = this.props;
  116. const { shouldAnimate } = this.state;
  117. const columnIndex = getIndex(this.context.router.history.location.pathname);
  118. this.pendingIndex = null;
  119. if (singleColumn) {
  120. return columnIndex !== -1 ? (
  121. <ReactSwipeableViews index={columnIndex} onChangeIndex={this.handleSwipe} onTransitionEnd={this.handleAnimationEnd} animateTransitions={shouldAnimate} springConfig={{ duration: '400ms', delay: '0s', easeFunction: 'ease' }} style={{ height: '100%' }}>
  122. {links.map(this.renderView)}
  123. </ReactSwipeableViews>
  124. ) : <div className='columns-area'>{children}</div>;
  125. }
  126. return (
  127. <div className='columns-area' ref={this.setRef}>
  128. {columns.map(column => {
  129. const params = column.get('params', null) === null ? null : column.get('params').toJS();
  130. return (
  131. <BundleContainer key={column.get('uuid')} fetchComponent={componentMap[column.get('id')]} loading={this.renderLoading(column.get('id'))} error={this.renderError}>
  132. {SpecificComponent => <SpecificComponent columnId={column.get('uuid')} params={params} multiColumn />}
  133. </BundleContainer>
  134. );
  135. })}
  136. {React.Children.map(children, child => React.cloneElement(child, { multiColumn: true }))}
  137. </div>
  138. );
  139. }
  140. }