The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

224 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. class UI extends React.PureComponent {
  67. static propTypes = {
  68. dispatch: PropTypes.func.isRequired,
  69. children: PropTypes.node,
  70. };
  71. state = {
  72. width: window.innerWidth,
  73. draggingOver: false,
  74. };
  75. handleResize = debounce(() => {
  76. this.setState({ width: window.innerWidth });
  77. }, 500, {
  78. trailing: true,
  79. });
  80. handleDragEnter = (e) => {
  81. e.preventDefault();
  82. if (!this.dragTargets) {
  83. this.dragTargets = [];
  84. }
  85. if (this.dragTargets.indexOf(e.target) === -1) {
  86. this.dragTargets.push(e.target);
  87. }
  88. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  89. this.setState({ draggingOver: true });
  90. }
  91. }
  92. handleDragOver = (e) => {
  93. e.preventDefault();
  94. e.stopPropagation();
  95. try {
  96. e.dataTransfer.dropEffect = 'copy';
  97. } catch (err) {
  98. }
  99. return false;
  100. }
  101. handleDrop = (e) => {
  102. e.preventDefault();
  103. this.setState({ draggingOver: false });
  104. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  105. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  106. }
  107. }
  108. handleDragLeave = (e) => {
  109. e.preventDefault();
  110. e.stopPropagation();
  111. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  112. if (this.dragTargets.length > 0) {
  113. return;
  114. }
  115. this.setState({ draggingOver: false });
  116. }
  117. closeUploadModal = () => {
  118. this.setState({ draggingOver: false });
  119. }
  120. componentWillMount () {
  121. window.addEventListener('resize', this.handleResize, { passive: true });
  122. document.addEventListener('dragenter', this.handleDragEnter, false);
  123. document.addEventListener('dragover', this.handleDragOver, false);
  124. document.addEventListener('drop', this.handleDrop, false);
  125. document.addEventListener('dragleave', this.handleDragLeave, false);
  126. document.addEventListener('dragend', this.handleDragEnd, false);
  127. this.props.dispatch(refreshHomeTimeline());
  128. this.props.dispatch(refreshNotifications());
  129. }
  130. componentWillUnmount () {
  131. window.removeEventListener('resize', this.handleResize);
  132. document.removeEventListener('dragenter', this.handleDragEnter);
  133. document.removeEventListener('dragover', this.handleDragOver);
  134. document.removeEventListener('drop', this.handleDrop);
  135. document.removeEventListener('dragleave', this.handleDragLeave);
  136. document.removeEventListener('dragend', this.handleDragEnd);
  137. }
  138. setRef = (c) => {
  139. this.node = c;
  140. }
  141. render () {
  142. const { width, draggingOver } = this.state;
  143. const { children } = this.props;
  144. return (
  145. <div className='ui' ref={this.setRef}>
  146. <TabsBar />
  147. <ColumnsAreaContainer singleColumn={isMobile(width)}>
  148. <WrappedSwitch>
  149. <Redirect from='/' to='/getting-started' exact />
  150. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  151. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  152. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  153. <WrappedRoute path='/timelines/public/local' component={CommunityTimeline} content={children} />
  154. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  155. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  156. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  157. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  158. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  159. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  160. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  161. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  162. <WrappedRoute path='/accounts/:accountId/followers' component={Followers} content={children} />
  163. <WrappedRoute path='/accounts/:accountId/following' component={Following} content={children} />
  164. <WrappedRoute path='/accounts/:accountId/media' component={AccountGallery} content={children} />
  165. <WrappedRoute path='/follow_requests' component={FollowRequests} content={children} />
  166. <WrappedRoute path='/blocks' component={Blocks} content={children} />
  167. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  168. <WrappedRoute path='/report' component={Report} content={children} />
  169. <WrappedRoute component={GenericNotFound} content={children} />
  170. </WrappedSwitch>
  171. </ColumnsAreaContainer>
  172. <NotificationsContainer />
  173. <LoadingBarContainer className='loading-bar' />
  174. <ModalContainer />
  175. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  176. </div>
  177. );
  178. }
  179. }
  180. export default connect()(UI);