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.

197 lines
6.5 KiB

  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import Redirect from 'react-router-dom/Redirect';
  4. import NotificationsContainer from './containers/notifications_container';
  5. import PropTypes from 'prop-types';
  6. import LoadingBarContainer from './containers/loading_bar_container';
  7. import TabsBar from './components/tabs_bar';
  8. import ModalContainer from './containers/modal_container';
  9. import { connect } from 'react-redux';
  10. import { isMobile } from '../../is_mobile';
  11. import { debounce } from 'lodash';
  12. import { uploadCompose } from '../../actions/compose';
  13. import { refreshHomeTimeline } from '../../actions/timelines';
  14. import { refreshNotifications } from '../../actions/notifications';
  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. systemFontUi: state.getIn(['meta', 'system_font_ui']),
  44. });
  45. @connect(mapStateToProps)
  46. export default class UI extends React.PureComponent {
  47. static propTypes = {
  48. dispatch: PropTypes.func.isRequired,
  49. children: PropTypes.node,
  50. systemFontUi: PropTypes.bool,
  51. };
  52. state = {
  53. width: window.innerWidth,
  54. draggingOver: false,
  55. };
  56. handleResize = debounce(() => {
  57. this.setState({ width: window.innerWidth });
  58. }, 500, {
  59. trailing: true,
  60. });
  61. handleDragEnter = (e) => {
  62. e.preventDefault();
  63. if (!this.dragTargets) {
  64. this.dragTargets = [];
  65. }
  66. if (this.dragTargets.indexOf(e.target) === -1) {
  67. this.dragTargets.push(e.target);
  68. }
  69. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  70. this.setState({ draggingOver: true });
  71. }
  72. }
  73. handleDragOver = (e) => {
  74. e.preventDefault();
  75. e.stopPropagation();
  76. try {
  77. e.dataTransfer.dropEffect = 'copy';
  78. } catch (err) {
  79. }
  80. return false;
  81. }
  82. handleDrop = (e) => {
  83. e.preventDefault();
  84. this.setState({ draggingOver: false });
  85. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  86. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  87. }
  88. }
  89. handleDragLeave = (e) => {
  90. e.preventDefault();
  91. e.stopPropagation();
  92. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  93. if (this.dragTargets.length > 0) {
  94. return;
  95. }
  96. this.setState({ draggingOver: false });
  97. }
  98. closeUploadModal = () => {
  99. this.setState({ draggingOver: false });
  100. }
  101. componentWillMount () {
  102. window.addEventListener('resize', this.handleResize, { passive: true });
  103. document.addEventListener('dragenter', this.handleDragEnter, false);
  104. document.addEventListener('dragover', this.handleDragOver, false);
  105. document.addEventListener('drop', this.handleDrop, false);
  106. document.addEventListener('dragleave', this.handleDragLeave, false);
  107. document.addEventListener('dragend', this.handleDragEnd, false);
  108. this.props.dispatch(refreshHomeTimeline());
  109. this.props.dispatch(refreshNotifications());
  110. }
  111. componentWillUnmount () {
  112. window.removeEventListener('resize', this.handleResize);
  113. document.removeEventListener('dragenter', this.handleDragEnter);
  114. document.removeEventListener('dragover', this.handleDragOver);
  115. document.removeEventListener('drop', this.handleDrop);
  116. document.removeEventListener('dragleave', this.handleDragLeave);
  117. document.removeEventListener('dragend', this.handleDragEnd);
  118. }
  119. setRef = (c) => {
  120. this.node = c;
  121. }
  122. render () {
  123. const { width, draggingOver } = this.state;
  124. const { children } = this.props;
  125. const className = classNames('ui', {
  126. 'system-font': this.props.systemFontUi,
  127. });
  128. return (
  129. <div className={className} ref={this.setRef}>
  130. <TabsBar />
  131. <ColumnsAreaContainer singleColumn={isMobile(width)}>
  132. <WrappedSwitch>
  133. <Redirect from='/' to='/getting-started' exact />
  134. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  135. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  136. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  137. <WrappedRoute path='/timelines/public/local' component={CommunityTimeline} content={children} />
  138. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  139. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  140. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  141. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  142. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  143. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  144. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  145. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  146. <WrappedRoute path='/accounts/:accountId/followers' component={Followers} content={children} />
  147. <WrappedRoute path='/accounts/:accountId/following' component={Following} content={children} />
  148. <WrappedRoute path='/accounts/:accountId/media' component={AccountGallery} content={children} />
  149. <WrappedRoute path='/follow_requests' component={FollowRequests} content={children} />
  150. <WrappedRoute path='/blocks' component={Blocks} content={children} />
  151. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  152. <WrappedRoute component={GenericNotFound} content={children} />
  153. </WrappedSwitch>
  154. </ColumnsAreaContainer>
  155. <NotificationsContainer />
  156. <LoadingBarContainer className='loading-bar' />
  157. <ModalContainer />
  158. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  159. </div>
  160. );
  161. }
  162. }