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.

162 lines
5.5 KiB

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