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.

229 lines
8.1 KiB

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