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.
 
 
 
 

466 lines
14 KiB

  1. import classNames from 'classnames';
  2. import React from 'react';
  3. import NotificationsContainer from './containers/notifications_container';
  4. import PropTypes from 'prop-types';
  5. import LoadingBarContainer from './containers/loading_bar_container';
  6. import TabsBar from './components/tabs_bar';
  7. import ModalContainer from './containers/modal_container';
  8. import { connect } from 'react-redux';
  9. import { Redirect, withRouter } from 'react-router-dom';
  10. import { isMobile } from '../../is_mobile';
  11. import { debounce } from 'lodash';
  12. import { uploadCompose, resetCompose } from '../../actions/compose';
  13. import { expandHomeTimeline } from '../../actions/timelines';
  14. import { expandNotifications } from '../../actions/notifications';
  15. import { clearHeight } from '../../actions/height_cache';
  16. import { WrappedSwitch, WrappedRoute } from './util/react_router_helpers';
  17. import UploadArea from './components/upload_area';
  18. import ColumnsAreaContainer from './containers/columns_area_container';
  19. import {
  20. Compose,
  21. Status,
  22. GettingStarted,
  23. KeyboardShortcuts,
  24. PublicTimeline,
  25. CommunityTimeline,
  26. AccountTimeline,
  27. AccountGallery,
  28. HomeTimeline,
  29. Followers,
  30. Following,
  31. Reblogs,
  32. Favourites,
  33. DirectTimeline,
  34. HashtagTimeline,
  35. Notifications,
  36. FollowRequests,
  37. GenericNotFound,
  38. FavouritedStatuses,
  39. ListTimeline,
  40. Blocks,
  41. DomainBlocks,
  42. Mutes,
  43. PinnedStatuses,
  44. Lists,
  45. } from './util/async-components';
  46. import { HotKeys } from 'react-hotkeys';
  47. import { me } from '../../initial_state';
  48. import { defineMessages, injectIntl } from 'react-intl';
  49. // Dummy import, to make sure that <Status /> ends up in the application bundle.
  50. // Without this it ends up in ~8 very commonly used bundles.
  51. import '../../components/status';
  52. const messages = defineMessages({
  53. beforeUnload: { id: 'ui.beforeunload', defaultMessage: 'Your draft will be lost if you leave Mastodon.' },
  54. });
  55. const mapStateToProps = state => ({
  56. isComposing: state.getIn(['compose', 'is_composing']),
  57. hasComposingText: state.getIn(['compose', 'text']) !== '',
  58. dropdownMenuIsOpen: state.getIn(['dropdown_menu', 'openId']) !== null,
  59. });
  60. const keyMap = {
  61. help: '?',
  62. new: 'n',
  63. search: 's',
  64. forceNew: 'option+n',
  65. focusColumn: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
  66. reply: 'r',
  67. favourite: 'f',
  68. boost: 'b',
  69. mention: 'm',
  70. open: ['enter', 'o'],
  71. openProfile: 'p',
  72. moveDown: ['down', 'j'],
  73. moveUp: ['up', 'k'],
  74. back: 'backspace',
  75. goToHome: 'g h',
  76. goToNotifications: 'g n',
  77. goToLocal: 'g l',
  78. goToFederated: 'g t',
  79. goToDirect: 'g d',
  80. goToStart: 'g s',
  81. goToFavourites: 'g f',
  82. goToPinned: 'g p',
  83. goToProfile: 'g u',
  84. goToBlocked: 'g b',
  85. goToMuted: 'g m',
  86. toggleHidden: 'x',
  87. };
  88. class SwitchingColumnsArea extends React.PureComponent {
  89. static propTypes = {
  90. children: PropTypes.node,
  91. location: PropTypes.object,
  92. onLayoutChange: PropTypes.func.isRequired,
  93. };
  94. state = {
  95. mobile: isMobile(window.innerWidth),
  96. };
  97. componentWillMount () {
  98. window.addEventListener('resize', this.handleResize, { passive: true });
  99. }
  100. componentDidUpdate (prevProps) {
  101. if (![this.props.location.pathname, '/'].includes(prevProps.location.pathname)) {
  102. this.node.handleChildrenContentChange();
  103. }
  104. }
  105. componentWillUnmount () {
  106. window.removeEventListener('resize', this.handleResize);
  107. }
  108. handleResize = debounce(() => {
  109. // The cached heights are no longer accurate, invalidate
  110. this.props.onLayoutChange();
  111. this.setState({ mobile: isMobile(window.innerWidth) });
  112. }, 500, {
  113. trailing: true,
  114. });
  115. setRef = c => {
  116. this.node = c.getWrappedInstance().getWrappedInstance();
  117. }
  118. render () {
  119. const { children } = this.props;
  120. const { mobile } = this.state;
  121. const redirect = mobile ? <Redirect from='/' to='/timelines/home' exact /> : <Redirect from='/' to='/getting-started' exact />;
  122. return (
  123. <ColumnsAreaContainer ref={this.setRef} singleColumn={mobile}>
  124. <WrappedSwitch>
  125. {redirect}
  126. <WrappedRoute path='/getting-started' component={GettingStarted} content={children} />
  127. <WrappedRoute path='/keyboard-shortcuts' component={KeyboardShortcuts} content={children} />
  128. <WrappedRoute path='/timelines/home' component={HomeTimeline} content={children} />
  129. <WrappedRoute path='/timelines/public' exact component={PublicTimeline} content={children} />
  130. <WrappedRoute path='/timelines/public/media' component={PublicTimeline} content={children} componentParams={{ onlyMedia: true }} />
  131. <WrappedRoute path='/timelines/public/local' exact component={CommunityTimeline} content={children} />
  132. <WrappedRoute path='/timelines/public/local/media' component={CommunityTimeline} content={children} componentParams={{ onlyMedia: true }} />
  133. <WrappedRoute path='/timelines/direct' component={DirectTimeline} content={children} />
  134. <WrappedRoute path='/timelines/tag/:id' component={HashtagTimeline} content={children} />
  135. <WrappedRoute path='/timelines/list/:id' component={ListTimeline} content={children} />
  136. <WrappedRoute path='/notifications' component={Notifications} content={children} />
  137. <WrappedRoute path='/favourites' component={FavouritedStatuses} content={children} />
  138. <WrappedRoute path='/pinned' component={PinnedStatuses} content={children} />
  139. <WrappedRoute path='/search' component={Compose} content={children} componentParams={{ isSearchPage: true }} />
  140. <WrappedRoute path='/statuses/new' component={Compose} content={children} />
  141. <WrappedRoute path='/statuses/:statusId' exact component={Status} content={children} />
  142. <WrappedRoute path='/statuses/:statusId/reblogs' component={Reblogs} content={children} />
  143. <WrappedRoute path='/statuses/:statusId/favourites' component={Favourites} content={children} />
  144. <WrappedRoute path='/accounts/:accountId' exact component={AccountTimeline} content={children} />
  145. <WrappedRoute path='/accounts/:accountId/with_replies' component={AccountTimeline} content={children} componentParams={{ withReplies: true }} />
  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='/domain_blocks' component={DomainBlocks} content={children} />
  152. <WrappedRoute path='/mutes' component={Mutes} content={children} />
  153. <WrappedRoute path='/lists' component={Lists} content={children} />
  154. <WrappedRoute component={GenericNotFound} content={children} />
  155. </WrappedSwitch>
  156. </ColumnsAreaContainer>
  157. );
  158. }
  159. }
  160. @connect(mapStateToProps)
  161. @injectIntl
  162. @withRouter
  163. export default class UI extends React.PureComponent {
  164. static contextTypes = {
  165. router: PropTypes.object.isRequired,
  166. };
  167. static propTypes = {
  168. dispatch: PropTypes.func.isRequired,
  169. children: PropTypes.node,
  170. isComposing: PropTypes.bool,
  171. hasComposingText: PropTypes.bool,
  172. location: PropTypes.object,
  173. intl: PropTypes.object.isRequired,
  174. dropdownMenuIsOpen: PropTypes.bool,
  175. };
  176. state = {
  177. draggingOver: false,
  178. };
  179. handleBeforeUnload = (e) => {
  180. const { intl, isComposing, hasComposingText } = this.props;
  181. if (isComposing && hasComposingText) {
  182. // Setting returnValue to any string causes confirmation dialog.
  183. // Many browsers no longer display this text to users,
  184. // but we set user-friendly message for other browsers, e.g. Edge.
  185. e.returnValue = intl.formatMessage(messages.beforeUnload);
  186. }
  187. }
  188. handleLayoutChange = () => {
  189. // The cached heights are no longer accurate, invalidate
  190. this.props.dispatch(clearHeight());
  191. }
  192. handleDragEnter = (e) => {
  193. e.preventDefault();
  194. if (!this.dragTargets) {
  195. this.dragTargets = [];
  196. }
  197. if (this.dragTargets.indexOf(e.target) === -1) {
  198. this.dragTargets.push(e.target);
  199. }
  200. if (e.dataTransfer && e.dataTransfer.types.includes('Files')) {
  201. this.setState({ draggingOver: true });
  202. }
  203. }
  204. handleDragOver = (e) => {
  205. e.preventDefault();
  206. e.stopPropagation();
  207. try {
  208. e.dataTransfer.dropEffect = 'copy';
  209. } catch (err) {
  210. }
  211. return false;
  212. }
  213. handleDrop = (e) => {
  214. e.preventDefault();
  215. this.setState({ draggingOver: false });
  216. if (e.dataTransfer && e.dataTransfer.files.length === 1) {
  217. this.props.dispatch(uploadCompose(e.dataTransfer.files));
  218. }
  219. }
  220. handleDragLeave = (e) => {
  221. e.preventDefault();
  222. e.stopPropagation();
  223. this.dragTargets = this.dragTargets.filter(el => el !== e.target && this.node.contains(el));
  224. if (this.dragTargets.length > 0) {
  225. return;
  226. }
  227. this.setState({ draggingOver: false });
  228. }
  229. closeUploadModal = () => {
  230. this.setState({ draggingOver: false });
  231. }
  232. handleServiceWorkerPostMessage = ({ data }) => {
  233. if (data.type === 'navigate') {
  234. this.context.router.history.push(data.path);
  235. } else {
  236. console.warn('Unknown message type:', data.type);
  237. }
  238. }
  239. componentWillMount () {
  240. window.addEventListener('beforeunload', this.handleBeforeUnload, false);
  241. document.addEventListener('dragenter', this.handleDragEnter, false);
  242. document.addEventListener('dragover', this.handleDragOver, false);
  243. document.addEventListener('drop', this.handleDrop, false);
  244. document.addEventListener('dragleave', this.handleDragLeave, false);
  245. document.addEventListener('dragend', this.handleDragEnd, false);
  246. if ('serviceWorker' in navigator) {
  247. navigator.serviceWorker.addEventListener('message', this.handleServiceWorkerPostMessage);
  248. }
  249. this.props.dispatch(expandHomeTimeline());
  250. this.props.dispatch(expandNotifications());
  251. }
  252. componentDidMount () {
  253. this.hotkeys.__mousetrap__.stopCallback = (e, element) => {
  254. return ['TEXTAREA', 'SELECT', 'INPUT'].includes(element.tagName);
  255. };
  256. }
  257. componentWillUnmount () {
  258. window.removeEventListener('beforeunload', this.handleBeforeUnload);
  259. document.removeEventListener('dragenter', this.handleDragEnter);
  260. document.removeEventListener('dragover', this.handleDragOver);
  261. document.removeEventListener('drop', this.handleDrop);
  262. document.removeEventListener('dragleave', this.handleDragLeave);
  263. document.removeEventListener('dragend', this.handleDragEnd);
  264. }
  265. setRef = c => {
  266. this.node = c;
  267. }
  268. handleHotkeyNew = e => {
  269. e.preventDefault();
  270. const element = this.node.querySelector('.compose-form__autosuggest-wrapper textarea');
  271. if (element) {
  272. element.focus();
  273. }
  274. }
  275. handleHotkeySearch = e => {
  276. e.preventDefault();
  277. const element = this.node.querySelector('.search__input');
  278. if (element) {
  279. element.focus();
  280. }
  281. }
  282. handleHotkeyForceNew = e => {
  283. this.handleHotkeyNew(e);
  284. this.props.dispatch(resetCompose());
  285. }
  286. handleHotkeyFocusColumn = e => {
  287. const index = (e.key * 1) + 1; // First child is drawer, skip that
  288. const column = this.node.querySelector(`.column:nth-child(${index})`);
  289. if (column) {
  290. const status = column.querySelector('.focusable');
  291. if (status) {
  292. status.focus();
  293. }
  294. }
  295. }
  296. handleHotkeyBack = () => {
  297. if (window.history && window.history.length === 1) {
  298. this.context.router.history.push('/');
  299. } else {
  300. this.context.router.history.goBack();
  301. }
  302. }
  303. setHotkeysRef = c => {
  304. this.hotkeys = c;
  305. }
  306. handleHotkeyToggleHelp = () => {
  307. if (this.props.location.pathname === '/keyboard-shortcuts') {
  308. this.context.router.history.goBack();
  309. } else {
  310. this.context.router.history.push('/keyboard-shortcuts');
  311. }
  312. }
  313. handleHotkeyGoToHome = () => {
  314. this.context.router.history.push('/timelines/home');
  315. }
  316. handleHotkeyGoToNotifications = () => {
  317. this.context.router.history.push('/notifications');
  318. }
  319. handleHotkeyGoToLocal = () => {
  320. this.context.router.history.push('/timelines/public/local');
  321. }
  322. handleHotkeyGoToFederated = () => {
  323. this.context.router.history.push('/timelines/public');
  324. }
  325. handleHotkeyGoToDirect = () => {
  326. this.context.router.history.push('/timelines/direct');
  327. }
  328. handleHotkeyGoToStart = () => {
  329. this.context.router.history.push('/getting-started');
  330. }
  331. handleHotkeyGoToFavourites = () => {
  332. this.context.router.history.push('/favourites');
  333. }
  334. handleHotkeyGoToPinned = () => {
  335. this.context.router.history.push('/pinned');
  336. }
  337. handleHotkeyGoToProfile = () => {
  338. this.context.router.history.push(`/accounts/${me}`);
  339. }
  340. handleHotkeyGoToBlocked = () => {
  341. this.context.router.history.push('/blocks');
  342. }
  343. handleHotkeyGoToMuted = () => {
  344. this.context.router.history.push('/mutes');
  345. }
  346. render () {
  347. const { draggingOver } = this.state;
  348. const { children, isComposing, location, dropdownMenuIsOpen } = this.props;
  349. const handlers = {
  350. help: this.handleHotkeyToggleHelp,
  351. new: this.handleHotkeyNew,
  352. search: this.handleHotkeySearch,
  353. forceNew: this.handleHotkeyForceNew,
  354. focusColumn: this.handleHotkeyFocusColumn,
  355. back: this.handleHotkeyBack,
  356. goToHome: this.handleHotkeyGoToHome,
  357. goToNotifications: this.handleHotkeyGoToNotifications,
  358. goToLocal: this.handleHotkeyGoToLocal,
  359. goToFederated: this.handleHotkeyGoToFederated,
  360. goToDirect: this.handleHotkeyGoToDirect,
  361. goToStart: this.handleHotkeyGoToStart,
  362. goToFavourites: this.handleHotkeyGoToFavourites,
  363. goToPinned: this.handleHotkeyGoToPinned,
  364. goToProfile: this.handleHotkeyGoToProfile,
  365. goToBlocked: this.handleHotkeyGoToBlocked,
  366. goToMuted: this.handleHotkeyGoToMuted,
  367. };
  368. return (
  369. <HotKeys keyMap={keyMap} handlers={handlers} ref={this.setHotkeysRef}>
  370. <div className={classNames('ui', { 'is-composing': isComposing })} ref={this.setRef} style={{ pointerEvents: dropdownMenuIsOpen ? 'none' : null }}>
  371. <TabsBar />
  372. <SwitchingColumnsArea location={location} onLayoutChange={this.handleLayoutChange}>
  373. {children}
  374. </SwitchingColumnsArea>
  375. <NotificationsContainer />
  376. <LoadingBarContainer className='loading-bar' />
  377. <ModalContainer />
  378. <UploadArea active={draggingOver} onClose={this.closeUploadModal} />
  379. </div>
  380. </HotKeys>
  381. );
  382. }
  383. }