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.
 
 
 
 

238 lines
7.7 KiB

  1. import React from 'react';
  2. import NotificationsContainer from './containers/notifications_container';
  3. import PropTypes from 'prop-types';
  4. import LoadingBarContainer from './containers/loading_bar_container';
  5. import TabsBar from './components/tabs_bar';
  6. import ModalContainer from './containers/modal_container';
  7. import { connect } from 'react-redux';
  8. import { Redirect, withRouter } from 'react-router-dom';
  9. import { isMobile } from '../../is_mobile';
  10. import { debounce } from 'lodash';
  11. import { uploadCompose } from '../../actions/compose';
  12. import { refreshHomeTimeline } from '../../actions/timelines';
  13. import { refreshNotifications } from '../../actions/notifications';
  14. import { clearStatusesHeight } from '../../actions/statuses';
  15. import { WrappedSwitch, WrappedRoute } from './util/react_router_helpers';
  16. import UploadArea from './components/upload_area';
  17. import ColumnsAreaContainer from './containers/columns_area_container';
  18. import {
  19. Compose,
  20. Status,
  21. GettingStarted,
  22. PublicTimeline,
  23. CommunityTimeline,
  24. AccountTimeline,
  25. AccountGallery,
  26. HomeTimeline,
  27. Followers,
  28. Following,
  29. Reblogs,
  30. Favourites,
  31. HashtagTimeline,
  32. Notifications,
  33. FollowRequests,
  34. GenericNotFound,
  35. FavouritedStatuses,
  36. Blocks,
  37. Mutes,
  38. } from './util/async-components';
  39. // Dummy import, to make sure that <Status /> ends up in the application bundle.
  40. // Without this it ends up in ~8 very commonly used bundles.
  41. import '../../components/status';
  42. const mapStateToProps = state => ({
  43. isComposing: state.getIn(['compose', 'is_composing']),
  44. });
  45. @connect(mapStateToProps)
  46. @withRouter
  47. export default class UI extends React.PureComponent {
  48. static contextTypes = {
  49. router: PropTypes.object.isRequired,
  50. }
  51. static propTypes = {
  52. dispatch: PropTypes.func.isRequired,
  53. children: PropTypes.node,
  54. isComposing: PropTypes.bool,
  55. location: PropTypes.object,
  56. };
  57. state = {
  58. width: window.innerWidth,
  59. draggingOver: false,
  60. };
  61. handleResize = debounce(() => {
  62. // The cached heights are no longer accurate, invalidate
  63. this.props.dispatch(clearStatusesHeight());
  64. this.setState({ width: window.innerWidth });
  65. }, 500, {
  66. trailing: true,
  67. });
  68. handleDragEnter = (e) => {
  69. e.preventDefault();
  70. if (!this.dragTargets) {
  71. this.dragTargets = [];
  72. }
  73. if (this.dragTargets.indexOf(e.target) === -1) {
  74. this.dragTargets.push(e.target);
  75. }
  76. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  77. this.setState({ draggingOver: true });
  78. }
  79. }
  80. handleDragOver = (e) => {
  81. e.preventDefault();
  82. e.stopPropagation();
  83. try {
  84. e.dataTransfer.dropEffect = 'copy';
  85. } catch (err) {
  86. }
  87. return false;
  88. }
  89. handleDrop = (e) => {
  90. e.preventDefault();
  91. this.setState({ draggingOver: false });
  92. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  93. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  94. }
  95. }
  96. handleDragLeave = (e) => {
  97. e.preventDefault();
  98. e.stopPropagation();
  99. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  100. if (this.dragTargets.length > 0) {
  101. return;
  102. }
  103. this.setState({ draggingOver: false });
  104. }
  105. closeUploadModal = () => {
  106. this.setState({ draggingOver: false });
  107. }
  108. handleServiceWorkerPostMessage = ({ data }) => {
  109. if (data.type === 'navigate') {
  110. this.context.router.history.push(data.path);
  111. } else {
  112. console.warn('Unknown message type:', data.type);
  113. }
  114. }
  115. componentWillMount () {
  116. window.addEventListener('resize', this.handleResize, { passive: true });
  117. document.addEventListener('dragenter', this.handleDragEnter, false);
  118. document.addEventListener('dragover', this.handleDragOver, false);
  119. document.addEventListener('drop', this.handleDrop, false);
  120. document.addEventListener('dragleave', this.handleDragLeave, false);
  121. document.addEventListener('dragend', this.handleDragEnd, false);
  122. if ('serviceWorker' in navigator) {
  123. navigator.serviceWorker.addEventListener('message', this.handleServiceWorkerPostMessage);
  124. }
  125. this.props.dispatch(refreshHomeTimeline());
  126. this.props.dispatch(refreshNotifications());
  127. }
  128. shouldComponentUpdate (nextProps) {
  129. if (nextProps.isComposing !== this.props.isComposing) {
  130. // Avoid expensive update just to toggle a class
  131. this.node.classList.toggle('is-composing', nextProps.isComposing);
  132. return false;
  133. }
  134. // Why isn't this working?!?
  135. // return super.shouldComponentUpdate(nextProps, nextState);
  136. return true;
  137. }
  138. componentDidUpdate (prevProps) {
  139. if (![this.props.location.pathname, '/'].includes(prevProps.location.pathname)) {
  140. this.columnsAreaNode.handleChildrenContentChange();
  141. }
  142. }
  143. componentWillUnmount () {
  144. window.removeEventListener('resize', this.handleResize);
  145. document.removeEventListener('dragenter', this.handleDragEnter);
  146. document.removeEventListener('dragover', this.handleDragOver);
  147. document.removeEventListener('drop', this.handleDrop);
  148. document.removeEventListener('dragleave', this.handleDragLeave);
  149. document.removeEventListener('dragend', this.handleDragEnd);
  150. }
  151. setRef = (c) => {
  152. this.node = c;
  153. }
  154. setColumnsAreaRef = (c) => {
  155. this.columnsAreaNode = c.getWrappedInstance().getWrappedInstance();
  156. }
  157. render () {
  158. const { width, draggingOver } = this.state;
  159. const { children } = this.props;
  160. return (
  161. <div className='ui' ref={this.setRef}>
  162. <TabsBar />
  163. <ColumnsAreaContainer ref={this.setColumnsAreaRef} singleColumn={isMobile(width)}>
  164. <WrappedSwitch>
  165. <Redirect from='/' to='/getting-started' exact />
  166. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  167. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  168. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  169. <WrappedRoute path='/timelines/public/local' component={CommunityTimeline} content={children} />
  170. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  171. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  172. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  173. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  174. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  175. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  176. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  177. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  178. <WrappedRoute path='/accounts/:accountId/followers' component={Followers} content={children} />
  179. <WrappedRoute path='/accounts/:accountId/following' component={Following} content={children} />
  180. <WrappedRoute path='/accounts/:accountId/media' component={AccountGallery} content={children} />
  181. <WrappedRoute path='/follow_requests' component={FollowRequests} content={children} />
  182. <WrappedRoute path='/blocks' component={Blocks} content={children} />
  183. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  184. <WrappedRoute component={GenericNotFound} content={children} />
  185. </WrappedSwitch>
  186. </ColumnsAreaContainer>
  187. <NotificationsContainer />
  188. <LoadingBarContainer className='loading-bar' />
  189. <ModalContainer />
  190. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  191. </div>
  192. );
  193. }
  194. }