logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

index.js (13355B)


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