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.

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