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.

137 lines
4.7 KiB

  1. import { Provider } from 'react-redux';
  2. import configureStore from '../store/configureStore';
  3. import {
  4. refreshTimelineSuccess,
  5. updateTimeline,
  6. deleteFromTimelines,
  7. refreshTimeline
  8. } from '../actions/timelines';
  9. import { updateNotifications } from '../actions/notifications';
  10. import createBrowserHistory from 'history/lib/createBrowserHistory';
  11. import {
  12. applyRouterMiddleware,
  13. useRouterHistory,
  14. Router,
  15. Route,
  16. IndexRedirect,
  17. IndexRoute
  18. } from 'react-router';
  19. import { useScroll } from 'react-router-scroll';
  20. import UI from '../features/ui';
  21. import Status from '../features/status';
  22. import GettingStarted from '../features/getting_started';
  23. import PublicTimeline from '../features/public_timeline';
  24. import AccountTimeline from '../features/account_timeline';
  25. import HomeTimeline from '../features/home_timeline';
  26. import Compose from '../features/compose';
  27. import Followers from '../features/followers';
  28. import Following from '../features/following';
  29. import Reblogs from '../features/reblogs';
  30. import Favourites from '../features/favourites';
  31. import HashtagTimeline from '../features/hashtag_timeline';
  32. import Notifications from '../features/notifications';
  33. import FollowRequests from '../features/follow_requests';
  34. import GenericNotFound from '../features/generic_not_found';
  35. import FavouritedStatuses from '../features/favourited_statuses';
  36. import { IntlProvider, addLocaleData } from 'react-intl';
  37. import en from 'react-intl/locale-data/en';
  38. import de from 'react-intl/locale-data/de';
  39. import es from 'react-intl/locale-data/es';
  40. import fr from 'react-intl/locale-data/fr';
  41. import pt from 'react-intl/locale-data/pt';
  42. import hu from 'react-intl/locale-data/hu';
  43. import uk from 'react-intl/locale-data/uk';
  44. import getMessagesForLocale from '../locales';
  45. import { hydrateStore } from '../actions/store';
  46. import createStream from '../stream';
  47. const store = configureStore();
  48. store.dispatch(hydrateStore(window.INITIAL_STATE));
  49. const browserHistory = useRouterHistory(createBrowserHistory)({
  50. basename: '/web'
  51. });
  52. addLocaleData([...en, ...de, ...es, ...fr, ...pt, ...hu, ...uk]);
  53. const Mastodon = React.createClass({
  54. propTypes: {
  55. locale: React.PropTypes.string.isRequired
  56. },
  57. componentDidMount() {
  58. const { locale } = this.props;
  59. const accessToken = store.getState().getIn(['meta', 'access_token']);
  60. this.subscription = createStream(accessToken, 'user', {
  61. received (data) {
  62. switch(data.event) {
  63. case 'update':
  64. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  65. break;
  66. case 'delete':
  67. store.dispatch(deleteFromTimelines(data.payload));
  68. break;
  69. case 'notification':
  70. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  71. break;
  72. }
  73. }
  74. });
  75. // Desktop notifications
  76. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  77. Notification.requestPermission();
  78. }
  79. },
  80. componentWillUnmount () {
  81. if (typeof this.subscription !== 'undefined') {
  82. this.subscription.close();
  83. this.subscription = null;
  84. }
  85. },
  86. render () {
  87. const { locale } = this.props;
  88. return (
  89. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  90. <Provider store={store}>
  91. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  92. <Route path='/' component={UI}>
  93. <IndexRedirect to="/getting-started" />
  94. <Route path='getting-started' component={GettingStarted} />
  95. <Route path='timelines/home' component={HomeTimeline} />
  96. <Route path='timelines/public' component={PublicTimeline} />
  97. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  98. <Route path='notifications' component={Notifications} />
  99. <Route path='favourites' component={FavouritedStatuses} />
  100. <Route path='statuses/new' component={Compose} />
  101. <Route path='statuses/:statusId' component={Status} />
  102. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  103. <Route path='statuses/:statusId/favourites' component={Favourites} />
  104. <Route path='accounts/:accountId' component={AccountTimeline} />
  105. <Route path='accounts/:accountId/followers' component={Followers} />
  106. <Route path='accounts/:accountId/following' component={Following} />
  107. <Route path='follow_requests' component={FollowRequests} />
  108. <Route path='*' component={GenericNotFound} />
  109. </Route>
  110. </Router>
  111. </Provider>
  112. </IntlProvider>
  113. );
  114. }
  115. });
  116. export default Mastodon;