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

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