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.

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