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.

226 lines
7.9 KiB

  1. import React from 'react';
  2. import Switch from 'react-router-dom/Switch';
  3. import Route from 'react-router-dom/Route';
  4. import Redirect from 'react-router-dom/Redirect';
  5. import NotificationsContainer from './containers/notifications_container';
  6. import PropTypes from 'prop-types';
  7. import LoadingBarContainer from './containers/loading_bar_container';
  8. import TabsBar from './components/tabs_bar';
  9. import ModalContainer from './containers/modal_container';
  10. import { connect } from 'react-redux';
  11. import { isMobile } from '../../is_mobile';
  12. import { debounce } from 'lodash';
  13. import { uploadCompose } from '../../actions/compose';
  14. import { refreshHomeTimeline } from '../../actions/timelines';
  15. import { refreshNotifications } from '../../actions/notifications';
  16. import UploadArea from './components/upload_area';
  17. import ColumnsAreaContainer from './containers/columns_area_container';
  18. import Status from '../../features/status';
  19. import GettingStarted from '../../features/getting_started';
  20. import PublicTimeline from '../../features/public_timeline';
  21. import CommunityTimeline from '../../features/community_timeline';
  22. import AccountTimeline from '../../features/account_timeline';
  23. import AccountGallery from '../../features/account_gallery';
  24. import HomeTimeline from '../../features/home_timeline';
  25. import Compose from '../../features/compose';
  26. import Followers from '../../features/followers';
  27. import Following from '../../features/following';
  28. import Reblogs from '../../features/reblogs';
  29. import Favourites from '../../features/favourites';
  30. import HashtagTimeline from '../../features/hashtag_timeline';
  31. import Notifications from '../../features/notifications';
  32. import FollowRequests from '../../features/follow_requests';
  33. import GenericNotFound from '../../features/generic_not_found';
  34. import FavouritedStatuses from '../../features/favourited_statuses';
  35. import Blocks from '../../features/blocks';
  36. import Mutes from '../../features/mutes';
  37. import Report from '../../features/report';
  38. // Small wrapper to pass multiColumn to the route components
  39. const WrappedSwitch = ({ multiColumn, children }) => (
  40. <Switch>
  41. {React.Children.map(children, child => React.cloneElement(child, { multiColumn }))}
  42. </Switch>
  43. );
  44. WrappedSwitch.propTypes = {
  45. multiColumn: PropTypes.bool,
  46. children: PropTypes.node,
  47. };
  48. // Small Wraper to extract the params from the route and pass
  49. // them to the rendered component, together with the content to
  50. // be rendered inside (the children)
  51. class WrappedRoute extends React.Component {
  52. static propTypes = {
  53. component: PropTypes.func.isRequired,
  54. content: PropTypes.node,
  55. multiColumn: PropTypes.bool,
  56. }
  57. renderComponent = ({ match: { params } }) => {
  58. const { component: Component, content, multiColumn } = this.props;
  59. return <Component params={params} multiColumn={multiColumn}>{content}</Component>;
  60. }
  61. render () {
  62. const { component: Component, content, ...rest } = this.props;
  63. return <Route {...rest} render={this.renderComponent} />;
  64. }
  65. }
  66. const noOp = () => false;
  67. class UI extends React.PureComponent {
  68. static propTypes = {
  69. dispatch: PropTypes.func.isRequired,
  70. children: PropTypes.node,
  71. };
  72. state = {
  73. width: window.innerWidth,
  74. draggingOver: false,
  75. };
  76. handleResize = debounce(() => {
  77. this.setState({ width: window.innerWidth });
  78. }, 500, {
  79. trailing: true,
  80. });
  81. handleDragEnter = (e) => {
  82. e.preventDefault();
  83. if (!this.dragTargets) {
  84. this.dragTargets = [];
  85. }
  86. if (this.dragTargets.indexOf(e.target) === -1) {
  87. this.dragTargets.push(e.target);
  88. }
  89. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  90. this.setState({ draggingOver: true });
  91. }
  92. }
  93. handleDragOver = (e) => {
  94. e.preventDefault();
  95. e.stopPropagation();
  96. try {
  97. e.dataTransfer.dropEffect = 'copy';
  98. } catch (err) {
  99. }
  100. return false;
  101. }
  102. handleDrop = (e) => {
  103. e.preventDefault();
  104. this.setState({ draggingOver: false });
  105. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  106. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  107. }
  108. }
  109. handleDragLeave = (e) => {
  110. e.preventDefault();
  111. e.stopPropagation();
  112. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  113. if (this.dragTargets.length > 0) {
  114. return;
  115. }
  116. this.setState({ draggingOver: false });
  117. }
  118. closeUploadModal = () => {
  119. this.setState({ draggingOver: false });
  120. }
  121. componentWillMount () {
  122. window.addEventListener('resize', this.handleResize, { passive: true });
  123. document.addEventListener('dragenter', this.handleDragEnter, false);
  124. document.addEventListener('dragover', this.handleDragOver, false);
  125. document.addEventListener('drop', this.handleDrop, false);
  126. document.addEventListener('dragleave', this.handleDragLeave, false);
  127. document.addEventListener('dragend', this.handleDragEnd, false);
  128. this.props.dispatch(refreshHomeTimeline());
  129. this.props.dispatch(refreshNotifications());
  130. }
  131. componentWillUnmount () {
  132. window.removeEventListener('resize', this.handleResize);
  133. document.removeEventListener('dragenter', this.handleDragEnter);
  134. document.removeEventListener('dragover', this.handleDragOver);
  135. document.removeEventListener('drop', this.handleDrop);
  136. document.removeEventListener('dragleave', this.handleDragLeave);
  137. document.removeEventListener('dragend', this.handleDragEnd);
  138. }
  139. setRef = (c) => {
  140. this.node = c;
  141. }
  142. render () {
  143. const { width, draggingOver } = this.state;
  144. const { children } = this.props;
  145. return (
  146. <div className='ui' ref={this.setRef}>
  147. <TabsBar />
  148. <ColumnsAreaContainer singleColumn={isMobile(width)}>
  149. <WrappedSwitch>
  150. <Redirect from='/' to='/getting-started' exact />
  151. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  152. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  153. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  154. <WrappedRoute path='/timelines/public/local' component={CommunityTimeline} content={children} />
  155. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  156. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  157. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  158. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  159. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  160. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  161. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  162. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  163. <WrappedRoute path='/accounts/:accountId/followers' component={Followers} content={children} />
  164. <WrappedRoute path='/accounts/:accountId/following' component={Following} content={children} />
  165. <WrappedRoute path='/accounts/:accountId/media' component={AccountGallery} content={children} />
  166. <WrappedRoute path='/follow_requests' component={FollowRequests} content={children} />
  167. <WrappedRoute path='/blocks' component={Blocks} content={children} />
  168. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  169. <WrappedRoute path='/report' component={Report} content={children} />
  170. <WrappedRoute component={GenericNotFound} content={children} />
  171. </WrappedSwitch>
  172. </ColumnsAreaContainer>
  173. <NotificationsContainer />
  174. <LoadingBarContainer className='loading-bar' />
  175. <ModalContainer />
  176. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  177. </div>
  178. );
  179. }
  180. }
  181. export default connect()(UI);