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.

188 lines
6.0 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 Mutes from '../features/mutes';
  41. import Report from '../features/report';
  42. import { IntlProvider, addLocaleData } from 'react-intl';
  43. import en from 'react-intl/locale-data/en';
  44. import de from 'react-intl/locale-data/de';
  45. import eo from 'react-intl/locale-data/eo';
  46. import es from 'react-intl/locale-data/es';
  47. import fi from 'react-intl/locale-data/fi';
  48. import fr from 'react-intl/locale-data/fr';
  49. import hu from 'react-intl/locale-data/hu';
  50. import ja from 'react-intl/locale-data/ja';
  51. import pt from 'react-intl/locale-data/pt';
  52. import nl from 'react-intl/locale-data/nl';
  53. import no from 'react-intl/locale-data/no';
  54. import ru from 'react-intl/locale-data/ru';
  55. import uk from 'react-intl/locale-data/uk';
  56. import zh from 'react-intl/locale-data/zh';
  57. import bg from 'react-intl/locale-data/bg';
  58. import { localeData as zh_hk } from '../locales/zh-hk';
  59. import getMessagesForLocale from '../locales';
  60. import { hydrateStore } from '../actions/store';
  61. import createStream from '../stream';
  62. const store = configureStore();
  63. store.dispatch(hydrateStore(window.INITIAL_STATE));
  64. const browserHistory = useRouterHistory(createBrowserHistory)({
  65. basename: '/web'
  66. });
  67. addLocaleData([
  68. ...en,
  69. ...de,
  70. ...eo,
  71. ...es,
  72. ...fi,
  73. ...fr,
  74. ...hu,
  75. ...ja,
  76. ...pt,
  77. ...nl,
  78. ...no,
  79. ...ru,
  80. ...uk,
  81. ...zh,
  82. ...zh_hk,
  83. ...bg,
  84. ]);
  85. const Mastodon = React.createClass({
  86. propTypes: {
  87. locale: React.PropTypes.string.isRequired
  88. },
  89. componentDidMount() {
  90. const { locale } = this.props;
  91. const accessToken = store.getState().getIn(['meta', 'access_token']);
  92. this.subscription = createStream(accessToken, 'user', {
  93. connected () {
  94. store.dispatch(connectTimeline('home'));
  95. },
  96. disconnected () {
  97. store.dispatch(disconnectTimeline('home'));
  98. },
  99. received (data) {
  100. switch(data.event) {
  101. case 'update':
  102. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  103. break;
  104. case 'delete':
  105. store.dispatch(deleteFromTimelines(data.payload));
  106. break;
  107. case 'notification':
  108. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  109. break;
  110. }
  111. },
  112. reconnected () {
  113. store.dispatch(connectTimeline('home'));
  114. store.dispatch(refreshTimeline('home'));
  115. store.dispatch(refreshNotifications());
  116. }
  117. });
  118. // Desktop notifications
  119. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  120. Notification.requestPermission();
  121. }
  122. },
  123. componentWillUnmount () {
  124. if (typeof this.subscription !== 'undefined') {
  125. this.subscription.close();
  126. this.subscription = null;
  127. }
  128. },
  129. render () {
  130. const { locale } = this.props;
  131. return (
  132. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  133. <Provider store={store}>
  134. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  135. <Route path='/' component={UI}>
  136. <IndexRedirect to="/getting-started" />
  137. <Route path='getting-started' component={GettingStarted} />
  138. <Route path='timelines/home' component={HomeTimeline} />
  139. <Route path='timelines/public' component={PublicTimeline} />
  140. <Route path='timelines/public/local' component={CommunityTimeline} />
  141. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  142. <Route path='notifications' component={Notifications} />
  143. <Route path='favourites' component={FavouritedStatuses} />
  144. <Route path='statuses/new' component={Compose} />
  145. <Route path='statuses/:statusId' component={Status} />
  146. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  147. <Route path='statuses/:statusId/favourites' component={Favourites} />
  148. <Route path='accounts/:accountId' component={AccountTimeline} />
  149. <Route path='accounts/:accountId/followers' component={Followers} />
  150. <Route path='accounts/:accountId/following' component={Following} />
  151. <Route path='follow_requests' component={FollowRequests} />
  152. <Route path='blocks' component={Blocks} />
  153. <Route path='mutes' component={Mutes} />
  154. <Route path='report' component={Report} />
  155. <Route path='*' component={GenericNotFound} />
  156. </Route>
  157. </Router>
  158. </Provider>
  159. </IntlProvider>
  160. );
  161. }
  162. });
  163. export default Mastodon;