闭社主体 forked from https://github.com/tootsuite/mastodon
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.

219 lines
7.8 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. // Small wrapper to pass multiColumn to the route components
  38. const WrappedSwitch = ({ multiColumn, children }) => (
  39. <Switch>
  40. {React.Children.map(children, child => React.cloneElement(child, { multiColumn }))}
  41. </Switch>
  42. );
  43. WrappedSwitch.propTypes = {
  44. multiColumn: PropTypes.bool,
  45. children: PropTypes.node,
  46. };
  47. // Small Wraper to extract the params from the route and pass
  48. // them to the rendered component, together with the content to
  49. // be rendered inside (the children)
  50. class WrappedRoute extends React.Component {
  51. static propTypes = {
  52. component: PropTypes.func.isRequired,
  53. content: PropTypes.node,
  54. multiColumn: PropTypes.bool,
  55. }
  56. renderComponent = ({ match: { params } }) => {
  57. const { component: Component, content, multiColumn } = this.props;
  58. return <Component params={params} multiColumn={multiColumn}>{content}</Component>;
  59. }
  60. render () {
  61. const { component: Component, content, ...rest } = this.props;
  62. return <Route {...rest} render={this.renderComponent} />;
  63. }
  64. }
  65. @connect()
  66. export default 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 component={GenericNotFound} content={children} />
  169. </WrappedSwitch>
  170. </ColumnsAreaContainer>
  171. <NotificationsContainer />
  172. <LoadingBarContainer className='loading-bar' />
  173. <ModalContainer />
  174. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  175. </div>
  176. );
  177. }
  178. }