闭社主体 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.

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