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.

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