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.

240 lines
8.0 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, 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 TabsBar, { links, getIndex, getLink } from './tabs_bar';
  8. import { Link } from 'react-router-dom';
  9. import BundleContainer from '../containers/bundle_container';
  10. import ColumnLoading from './column_loading';
  11. import DrawerLoading from './drawer_loading';
  12. import BundleColumnError from './bundle_column_error';
  13. import {
  14. Compose,
  15. Notifications,
  16. HomeTimeline,
  17. CommunityTimeline,
  18. PublicTimeline,
  19. HashtagTimeline,
  20. DirectTimeline,
  21. FavouritedStatuses,
  22. ListTimeline,
  23. Directory,
  24. } from '../../ui/util/async-components';
  25. import Icon from 'mastodon/components/icon';
  26. import ComposePanel from './compose_panel';
  27. import NavigationPanel from './navigation_panel';
  28. import detectPassiveEvents from 'detect-passive-events';
  29. import { scrollRight } from '../../../scroll';
  30. import TrendsContainer from '../../getting_started/containers/trends_container';
  31. const componentMap = {
  32. 'COMPOSE': Compose,
  33. 'HOME': HomeTimeline,
  34. 'NOTIFICATIONS': Notifications,
  35. 'PUBLIC': PublicTimeline,
  36. 'COMMUNITY': CommunityTimeline,
  37. 'HASHTAG': HashtagTimeline,
  38. 'DIRECT': DirectTimeline,
  39. 'FAVOURITES': FavouritedStatuses,
  40. 'LIST': ListTimeline,
  41. 'DIRECTORY': Directory,
  42. };
  43. const messages = defineMessages({
  44. publish: { id: 'compose_form.publish', defaultMessage: 'Toot' },
  45. });
  46. const shouldHideFAB = path => path.match(/^\/statuses\/|^\/search|^\/getting-started/);
  47. export default @(component => injectIntl(component, { withRef: true }))
  48. class ColumnsArea extends ImmutablePureComponent {
  49. static contextTypes = {
  50. router: PropTypes.object.isRequired,
  51. };
  52. static propTypes = {
  53. intl: PropTypes.object.isRequired,
  54. columns: ImmutablePropTypes.list.isRequired,
  55. isModalOpen: PropTypes.bool.isRequired,
  56. singleColumn: PropTypes.bool,
  57. children: PropTypes.node,
  58. };
  59. state = {
  60. shouldAnimate: false,
  61. }
  62. componentWillReceiveProps() {
  63. this.setState({ shouldAnimate: false });
  64. }
  65. componentDidMount() {
  66. if (!this.props.singleColumn) {
  67. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  68. }
  69. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  70. this.isRtlLayout = document.getElementsByTagName('body')[0].classList.contains('rtl');
  71. this.setState({ shouldAnimate: true });
  72. }
  73. componentWillUpdate(nextProps) {
  74. if (this.props.singleColumn !== nextProps.singleColumn && nextProps.singleColumn) {
  75. this.node.removeEventListener('wheel', this.handleWheel);
  76. }
  77. }
  78. componentDidUpdate(prevProps) {
  79. if (this.props.singleColumn !== prevProps.singleColumn && !this.props.singleColumn) {
  80. this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
  81. }
  82. this.lastIndex = getIndex(this.context.router.history.location.pathname);
  83. this.setState({ shouldAnimate: true });
  84. }
  85. componentWillUnmount () {
  86. if (!this.props.singleColumn) {
  87. this.node.removeEventListener('wheel', this.handleWheel);
  88. }
  89. }
  90. handleChildrenContentChange() {
  91. if (!this.props.singleColumn) {
  92. const modifier = this.isRtlLayout ? -1 : 1;
  93. this._interruptScrollAnimation = scrollRight(this.node, (this.node.scrollWidth - window.innerWidth) * modifier);
  94. }
  95. }
  96. handleSwipe = (index) => {
  97. this.pendingIndex = index;
  98. const nextLinkTranslationId = links[index].props['data-preview-title-id'];
  99. const currentLinkSelector = '.tabs-bar__link.active';
  100. const nextLinkSelector = `.tabs-bar__link[data-preview-title-id="${nextLinkTranslationId}"]`;
  101. // HACK: Remove the active class from the current link and set it to the next one
  102. // React-router does this for us, but too late, feeling laggy.
  103. document.querySelector(currentLinkSelector).classList.remove('active');
  104. document.querySelector(nextLinkSelector).classList.add('active');
  105. if (!this.state.shouldAnimate && typeof this.pendingIndex === 'number') {
  106. this.context.router.history.push(getLink(this.pendingIndex));
  107. this.pendingIndex = null;
  108. }
  109. }
  110. handleAnimationEnd = () => {
  111. if (typeof this.pendingIndex === 'number') {
  112. this.context.router.history.push(getLink(this.pendingIndex));
  113. this.pendingIndex = null;
  114. }
  115. }
  116. handleWheel = () => {
  117. if (typeof this._interruptScrollAnimation !== 'function') {
  118. return;
  119. }
  120. this._interruptScrollAnimation();
  121. }
  122. setRef = (node) => {
  123. this.node = node;
  124. }
  125. renderView = (link, index) => {
  126. const columnIndex = getIndex(this.context.router.history.location.pathname);
  127. const title = this.props.intl.formatMessage({ id: link.props['data-preview-title-id'] });
  128. const icon = link.props['data-preview-icon'];
  129. const view = (index === columnIndex) ?
  130. React.cloneElement(this.props.children) :
  131. <ColumnLoading title={title} icon={icon} />;
  132. return (
  133. <div className='columns-area columns-area--mobile' key={index}>
  134. {
  135. link.props['data-preview-title-id'] == 'column.community' &&
  136. <TrendsContainer />
  137. }
  138. {view}
  139. </div>
  140. );
  141. }
  142. renderLoading = columnId => () => {
  143. return columnId === 'COMPOSE' ? <DrawerLoading /> : <ColumnLoading />;
  144. }
  145. renderError = (props) => {
  146. return <BundleColumnError {...props} />;
  147. }
  148. render () {
  149. const { columns, children, singleColumn, isModalOpen, intl } = this.props;
  150. const { shouldAnimate } = this.state;
  151. const columnIndex = getIndex(this.context.router.history.location.pathname);
  152. if (singleColumn) {
  153. const floatingActionButton = shouldHideFAB(this.context.router.history.location.pathname) ? null : <Link key='floating-action-button' to='/statuses/new' className='floating-action-button' aria-label={intl.formatMessage(messages.publish)}><Icon id='pencil' /></Link>;
  154. const content = columnIndex !== -1 ? (
  155. <ReactSwipeableViews key='content' hysteresis={0.5} threshold={20} index={columnIndex} onChangeIndex={this.handleSwipe} onTransitionEnd={this.handleAnimationEnd} animateTransitions={shouldAnimate} springConfig={{ duration: '400ms', delay: '0s', easeFunction: 'ease' }} style={{ height: '100%' }}>
  156. {links.map(this.renderView)}
  157. </ReactSwipeableViews>
  158. ) : (
  159. <div key='content' className='columns-area columns-area--mobile'>{children}</div>
  160. );
  161. return (
  162. <div className='columns-area__panels'>
  163. <div className='columns-area__panels__pane columns-area__panels__pane--compositional'>
  164. <div className='columns-area__panels__pane__inner'>
  165. <ComposePanel />
  166. </div>
  167. </div>
  168. <div className='columns-area__panels__main'>
  169. <TabsBar key='tabs' />
  170. {content}
  171. </div>
  172. <div className='columns-area__panels__pane columns-area__panels__pane--start columns-area__panels__pane--navigational'>
  173. <div className='columns-area__panels__pane__inner'>
  174. <NavigationPanel />
  175. </div>
  176. </div>
  177. {floatingActionButton}
  178. </div>
  179. );
  180. }
  181. return (
  182. <div className={`columns-area ${ isModalOpen ? 'unscrollable' : '' }`} ref={this.setRef}>
  183. {columns.map(column => {
  184. const params = column.get('params', null) === null ? null : column.get('params').toJS();
  185. const other = params && params.other ? params.other : {};
  186. return (
  187. <BundleContainer key={column.get('uuid')} fetchComponent={componentMap[column.get('id')]} loading={this.renderLoading(column.get('id'))} error={this.renderError}>
  188. {SpecificComponent => <SpecificComponent columnId={column.get('uuid')} params={params} multiColumn {...other} />}
  189. </BundleContainer>
  190. );
  191. })}
  192. {React.Children.map(children, child => React.cloneElement(child, { multiColumn: true }))}
  193. </div>
  194. );
  195. }
  196. }