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.

134 lines
4.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. } from '../actions/timelines';
  9. import { updateNotifications } from '../actions/notifications';
  10. import { setAccessToken } from '../actions/meta';
  11. import { setAccountSelf } from '../actions/accounts';
  12. import PureRenderMixin from 'react-addons-pure-render-mixin';
  13. import createBrowserHistory from 'history/lib/createBrowserHistory';
  14. import {
  15. applyRouterMiddleware,
  16. useRouterHistory,
  17. Router,
  18. Route,
  19. IndexRoute
  20. } from 'react-router';
  21. import { useScroll } from 'react-router-scroll';
  22. import UI from '../features/ui';
  23. import Account from '../features/account';
  24. import Status from '../features/status';
  25. import GettingStarted from '../features/getting_started';
  26. import PublicTimeline from '../features/public_timeline';
  27. import AccountTimeline from '../features/account_timeline';
  28. import HomeTimeline from '../features/home_timeline';
  29. import MentionsTimeline from '../features/mentions_timeline';
  30. import Compose from '../features/compose';
  31. import Followers from '../features/followers';
  32. import Following from '../features/following';
  33. import Reblogs from '../features/reblogs';
  34. import Favourites from '../features/favourites';
  35. import HashtagTimeline from '../features/hashtag_timeline';
  36. import Notifications from '../features/notifications';
  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 getMessagesForLocale from '../locales';
  43. const store = configureStore();
  44. const browserHistory = useRouterHistory(createBrowserHistory)({
  45. basename: '/web'
  46. });
  47. addLocaleData([...en, ...de, ...es, ...fr]);
  48. const Mastodon = React.createClass({
  49. propTypes: {
  50. token: React.PropTypes.string.isRequired,
  51. timelines: React.PropTypes.object,
  52. account: React.PropTypes.string,
  53. locale: React.PropTypes.string.isRequired
  54. },
  55. mixins: [PureRenderMixin],
  56. componentWillMount() {
  57. const { token, account, locale } = this.props;
  58. store.dispatch(setAccessToken(token));
  59. store.dispatch(setAccountSelf(JSON.parse(account)));
  60. if (typeof App !== 'undefined') {
  61. this.subscription = App.cable.subscriptions.create('TimelineChannel', {
  62. received (data) {
  63. switch(data.type) {
  64. case 'update':
  65. return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
  66. case 'delete':
  67. return store.dispatch(deleteFromTimelines(data.id));
  68. case 'notification':
  69. return store.dispatch(updateNotifications(JSON.parse(data.message), getMessagesForLocale(locale), locale));
  70. }
  71. }
  72. });
  73. }
  74. // Desktop notifications
  75. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  76. Notification.requestPermission();
  77. }
  78. },
  79. componentWillUnmount () {
  80. if (typeof this.subscription !== 'undefined') {
  81. this.subscription.unsubscribe();
  82. }
  83. },
  84. render () {
  85. const { locale } = this.props;
  86. return (
  87. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  88. <Provider store={store}>
  89. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  90. <Route path='/' component={UI}>
  91. <IndexRoute component={GettingStarted} />
  92. <Route path='timelines/home' component={HomeTimeline} />
  93. <Route path='timelines/mentions' component={MentionsTimeline} />
  94. <Route path='timelines/public' component={PublicTimeline} />
  95. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  96. <Route path='notifications' component={Notifications} />
  97. <Route path='statuses/new' component={Compose} />
  98. <Route path='statuses/:statusId' component={Status} />
  99. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  100. <Route path='statuses/:statusId/favourites' component={Favourites} />
  101. <Route path='accounts/:accountId' component={Account}>
  102. <IndexRoute component={AccountTimeline} />
  103. <Route path='followers' component={Followers} />
  104. <Route path='following' component={Following} />
  105. </Route>
  106. </Route>
  107. </Router>
  108. </Provider>
  109. </IntlProvider>
  110. );
  111. }
  112. });
  113. export default Mastodon;