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.

140 lines
4.8 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 FollowRequests from '../features/follow_requests';
  38. import { IntlProvider, addLocaleData } from 'react-intl';
  39. import en from 'react-intl/locale-data/en';
  40. import de from 'react-intl/locale-data/de';
  41. import es from 'react-intl/locale-data/es';
  42. import fr from 'react-intl/locale-data/fr';
  43. import pt from 'react-intl/locale-data/pt';
  44. import hu from 'react-intl/locale-data/hu';
  45. import uk from 'react-intl/locale-data/uk';
  46. import getMessagesForLocale from '../locales';
  47. const store = configureStore();
  48. const browserHistory = useRouterHistory(createBrowserHistory)({
  49. basename: '/web'
  50. });
  51. addLocaleData([...en, ...de, ...es, ...fr, ...pt, ...hu, ...uk]);
  52. const Mastodon = React.createClass({
  53. propTypes: {
  54. token: React.PropTypes.string.isRequired,
  55. timelines: React.PropTypes.object,
  56. account: React.PropTypes.string,
  57. locale: React.PropTypes.string.isRequired
  58. },
  59. mixins: [PureRenderMixin],
  60. componentWillMount() {
  61. const { token, account, locale } = this.props;
  62. store.dispatch(setAccessToken(token));
  63. store.dispatch(setAccountSelf(JSON.parse(account)));
  64. if (typeof App !== 'undefined') {
  65. this.subscription = App.cable.subscriptions.create('TimelineChannel', {
  66. received (data) {
  67. switch(data.type) {
  68. case 'update':
  69. return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
  70. case 'delete':
  71. return store.dispatch(deleteFromTimelines(data.id));
  72. case 'notification':
  73. return store.dispatch(updateNotifications(JSON.parse(data.message), getMessagesForLocale(locale), locale));
  74. }
  75. }
  76. });
  77. }
  78. // Desktop notifications
  79. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  80. Notification.requestPermission();
  81. }
  82. },
  83. componentWillUnmount () {
  84. if (typeof this.subscription !== 'undefined') {
  85. this.subscription.unsubscribe();
  86. }
  87. },
  88. render () {
  89. const { locale } = this.props;
  90. return (
  91. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  92. <Provider store={store}>
  93. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  94. <Route path='/' component={UI}>
  95. <IndexRoute component={GettingStarted} />
  96. <Route path='timelines/home' component={HomeTimeline} />
  97. <Route path='timelines/mentions' component={MentionsTimeline} />
  98. <Route path='timelines/public' component={PublicTimeline} />
  99. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  100. <Route path='notifications' component={Notifications} />
  101. <Route path='statuses/new' component={Compose} />
  102. <Route path='statuses/:statusId' component={Status} />
  103. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  104. <Route path='statuses/:statusId/favourites' component={Favourites} />
  105. <Route path='accounts/:accountId' component={Account}>
  106. <IndexRoute component={AccountTimeline} />
  107. <Route path='followers' component={Followers} />
  108. <Route path='following' component={Following} />
  109. </Route>
  110. <Route path='follow_requests' component={FollowRequests} />
  111. </Route>
  112. </Router>
  113. </Provider>
  114. </IntlProvider>
  115. );
  116. }
  117. });
  118. export default Mastodon;