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

68 lines
2.5 KiB

  1. import { Provider } from 'react-redux';
  2. import configureStore from '../store/configureStore';
  3. import Frontend from '../components/frontend';
  4. import { setTimeline, updateTimeline, deleteFromTimelines, refreshTimeline } from '../actions/timelines';
  5. import { setAccessToken } from '../actions/meta';
  6. import PureRenderMixin from 'react-addons-pure-render-mixin';
  7. import { Router, Route, createMemoryHistory } from 'react-router';
  8. import AccountRoute from '../routes/account_route';
  9. import StatusRoute from '../routes/status_route';
  10. const store = configureStore();
  11. const history = createMemoryHistory();
  12. const Root = React.createClass({
  13. propTypes: {
  14. token: React.PropTypes.string.isRequired,
  15. timelines: React.PropTypes.object
  16. },
  17. mixins: [PureRenderMixin],
  18. componentWillMount() {
  19. store.dispatch(setAccessToken(this.props.token));
  20. for (var timelineType in this.props.timelines) {
  21. if (this.props.timelines.hasOwnProperty(timelineType)) {
  22. store.dispatch(setTimeline(timelineType, JSON.parse(this.props.timelines[timelineType])));
  23. }
  24. }
  25. if (typeof App !== 'undefined') {
  26. App.timeline = App.cable.subscriptions.create("TimelineChannel", {
  27. connected: function() {},
  28. disconnected: function() {},
  29. received: function(data) {
  30. switch(data.type) {
  31. case 'update':
  32. return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
  33. case 'delete':
  34. return store.dispatch(deleteFromTimelines(data.id));
  35. case 'merge':
  36. case 'unmerge':
  37. return store.dispatch(refreshTimeline('home'));
  38. }
  39. }
  40. });
  41. }
  42. },
  43. render () {
  44. return (
  45. <Provider store={store}>
  46. <Router history={history}>
  47. <Route path="/" component={Frontend}>
  48. <Route path="/accounts/:account_id" component={AccountRoute} />
  49. <Route path="/statuses/:status_id" component={StatusRoute} />
  50. </Route>
  51. </Router>
  52. </Provider>
  53. );
  54. }
  55. });
  56. export default Root;