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

69 lines
2.2 KiB

  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 { showOnboardingOnce } from '../actions/onboarding';
  6. import BrowserRouter from 'react-router-dom/BrowserRouter';
  7. import Route from 'react-router-dom/Route';
  8. import ScrollContext from 'react-router-scroll/lib/ScrollBehaviorContext';
  9. import UI from '../features/ui';
  10. import { hydrateStore } from '../actions/store';
  11. import { connectUserStream } from '../actions/streaming';
  12. import { IntlProvider, addLocaleData } from 'react-intl';
  13. import { getLocale } from '../locales';
  14. const { localeData, messages } = getLocale();
  15. addLocaleData(localeData);
  16. export const store = configureStore();
  17. const hydrateAction = hydrateStore(JSON.parse(document.getElementById('initial-state').textContent));
  18. store.dispatch(hydrateAction);
  19. export default class Mastodon extends React.PureComponent {
  20. static propTypes = {
  21. locale: PropTypes.string.isRequired,
  22. };
  23. componentDidMount() {
  24. this.disconnect = store.dispatch(connectUserStream());
  25. // Desktop notifications
  26. // Ask after 1 minute
  27. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  28. window.setTimeout(() => Notification.requestPermission(), 60 * 1000);
  29. }
  30. // Protocol handler
  31. // Ask after 5 minutes
  32. if (typeof navigator.registerProtocolHandler !== 'undefined') {
  33. const handlerUrl = window.location.protocol + '//' + window.location.host + '/intent?uri=%s';
  34. window.setTimeout(() => navigator.registerProtocolHandler('web+mastodon', handlerUrl, 'Mastodon'), 5 * 60 * 1000);
  35. }
  36. store.dispatch(showOnboardingOnce());
  37. }
  38. componentWillUnmount () {
  39. if (this.disconnect) {
  40. this.disconnect();
  41. this.disconnect = null;
  42. }
  43. }
  44. render () {
  45. const { locale } = this.props;
  46. return (
  47. <IntlProvider locale={locale} messages={messages}>
  48. <Provider store={store}>
  49. <BrowserRouter basename='/web'>
  50. <ScrollContext>
  51. <Route path='/' component={UI} />
  52. </ScrollContext>
  53. </BrowserRouter>
  54. </Provider>
  55. </IntlProvider>
  56. );
  57. }
  58. }