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.

93 lines
2.6 KiB

  1. import React from 'react';
  2. import { Provider, connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import configureStore from '../store/configureStore';
  5. import { INTRODUCTION_VERSION } from '../actions/onboarding';
  6. import { BrowserRouter, Route } from 'react-router-dom';
  7. import { ScrollContext } from 'react-router-scroll-4';
  8. import UI from '../features/ui';
  9. import Introduction from '../features/introduction';
  10. import { fetchCustomEmojis } from '../actions/custom_emojis';
  11. import { hydrateStore } from '../actions/store';
  12. import { connectUserStream } from '../actions/streaming';
  13. import { IntlProvider, addLocaleData } from 'react-intl';
  14. import { getLocale } from '../locales';
  15. import { previewState as previewMediaState } from 'mastodon/features/ui/components/media_modal';
  16. import { previewState as previewVideoState } from 'mastodon/features/ui/components/video_modal';
  17. import initialState from '../initial_state';
  18. import ErrorBoundary from '../components/error_boundary';
  19. const { localeData, messages } = getLocale();
  20. addLocaleData(localeData);
  21. export const store = configureStore();
  22. const hydrateAction = hydrateStore(initialState);
  23. store.dispatch(hydrateAction);
  24. store.dispatch(fetchCustomEmojis());
  25. const mapStateToProps = state => ({
  26. showIntroduction: state.getIn(['settings', 'introductionVersion'], 0) < INTRODUCTION_VERSION,
  27. });
  28. @connect(mapStateToProps)
  29. class MastodonMount extends React.PureComponent {
  30. static propTypes = {
  31. showIntroduction: PropTypes.bool,
  32. };
  33. shouldUpdateScroll (_, { location }) {
  34. return location.state !== previewMediaState && location.state !== previewVideoState;
  35. }
  36. render () {
  37. const { showIntroduction } = this.props;
  38. if (showIntroduction) {
  39. return <Introduction />;
  40. }
  41. return (
  42. <BrowserRouter basename='/web'>
  43. <ScrollContext shouldUpdateScroll={this.shouldUpdateScroll}>
  44. <Route path='/' component={UI} />
  45. </ScrollContext>
  46. </BrowserRouter>
  47. );
  48. }
  49. }
  50. export default class Mastodon extends React.PureComponent {
  51. static propTypes = {
  52. locale: PropTypes.string.isRequired,
  53. };
  54. componentDidMount() {
  55. this.disconnect = store.dispatch(connectUserStream());
  56. }
  57. componentWillUnmount () {
  58. if (this.disconnect) {
  59. this.disconnect();
  60. this.disconnect = null;
  61. }
  62. }
  63. render () {
  64. const { locale } = this.props;
  65. return (
  66. <IntlProvider locale={locale} messages={messages}>
  67. <Provider store={store}>
  68. <ErrorBoundary>
  69. <MastodonMount />
  70. </ErrorBoundary>
  71. </Provider>
  72. </IntlProvider>
  73. );
  74. }
  75. }