mastodon.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import configureStore from '../store/configureStore';
  5. import { BrowserRouter, Route } from 'react-router-dom';
  6. import { ScrollContext } from 'react-router-scroll-4';
  7. import UI from '../features/ui';
  8. import { fetchCustomEmojis } from '../actions/custom_emojis';
  9. import { hydrateStore } from '../actions/store';
  10. import { connectUserStream } from '../actions/streaming';
  11. import { IntlProvider, addLocaleData } from 'react-intl';
  12. import { getLocale } from '../locales';
  13. import initialState from '../initial_state';
  14. import ErrorBoundary from '../components/error_boundary';
  15. const { localeData, messages } = getLocale();
  16. addLocaleData(localeData);
  17. export const store = configureStore();
  18. const hydrateAction = hydrateStore(initialState);
  19. store.dispatch(hydrateAction);
  20. store.dispatch(fetchCustomEmojis());
  21. const createIdentityContext = state => ({
  22. signedIn: !!state.meta.me,
  23. accountId: state.meta.me,
  24. accessToken: state.meta.access_token,
  25. permissions: state.role.permissions,
  26. });
  27. export default class Mastodon extends React.PureComponent {
  28. static propTypes = {
  29. locale: PropTypes.string.isRequired,
  30. };
  31. static childContextTypes = {
  32. identity: PropTypes.shape({
  33. signedIn: PropTypes.bool.isRequired,
  34. accountId: PropTypes.string,
  35. accessToken: PropTypes.string,
  36. }).isRequired,
  37. };
  38. identity = createIdentityContext(initialState);
  39. getChildContext() {
  40. return {
  41. identity: this.identity,
  42. };
  43. }
  44. componentDidMount() {
  45. if (this.identity.signedIn) {
  46. this.disconnect = store.dispatch(connectUserStream());
  47. }
  48. }
  49. componentWillUnmount () {
  50. if (this.disconnect) {
  51. this.disconnect();
  52. this.disconnect = null;
  53. }
  54. }
  55. shouldUpdateScroll (prevRouterProps, { location }) {
  56. return !(location.state?.mastodonModalKey && location.state?.mastodonModalKey !== prevRouterProps?.location?.state?.mastodonModalKey);
  57. }
  58. render () {
  59. const { locale } = this.props;
  60. return (
  61. <IntlProvider locale={locale} messages={messages}>
  62. <Provider store={store}>
  63. <ErrorBoundary>
  64. <BrowserRouter basename='/web'>
  65. <ScrollContext shouldUpdateScroll={this.shouldUpdateScroll}>
  66. <Route path='/' component={UI} />
  67. </ScrollContext>
  68. </BrowserRouter>
  69. </ErrorBoundary>
  70. </Provider>
  71. </IntlProvider>
  72. );
  73. }
  74. }