mastodon.jsx 2.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import { Provider }                   from 'react-redux';
import configureStore                 from '../store/configureStore';
import {
  refreshTimelineSuccess,
  updateTimeline,
  deleteFromTimelines,
  refreshTimeline
}                                     from '../actions/timelines';
import { setAccessToken }             from '../actions/meta';
import { setAccountSelf }             from '../actions/accounts';
import PureRenderMixin                from 'react-addons-pure-render-mixin';
import { Router, Route, hashHistory } from 'react-router';
import Account                        from '../features/account';
import Settings                       from '../features/settings';
import Status                         from '../features/status';
import Subscriptions                  from '../features/subscriptions';
import UI                             from '../features/ui';
18

19
const store = configureStore();
20

21
const Mastodon = React.createClass({
22

23 24
  propTypes: {
    token: React.PropTypes.string.isRequired,
25 26
    timelines: React.PropTypes.object,
    account: React.PropTypes.string
27 28 29 30
  },

  mixins: [PureRenderMixin],

31
  componentWillMount() {
32
    store.dispatch(setAccessToken(this.props.token));
33
    store.dispatch(setAccountSelf(JSON.parse(this.props.account)));
34

35 36
    for (var timelineType in this.props.timelines) {
      if (this.props.timelines.hasOwnProperty(timelineType)) {
37
        store.dispatch(refreshTimelineSuccess(timelineType, JSON.parse(this.props.timelines[timelineType])));
38 39 40 41 42 43 44 45 46 47
      }
    }

    if (typeof App !== 'undefined') {
      App.timeline = App.cable.subscriptions.create("TimelineChannel", {
        connected: function() {},

        disconnected: function() {},

        received: function(data) {
48 49 50 51 52 53 54 55
          switch(data.type) {
            case 'update':
              return store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
            case 'delete':
              return store.dispatch(deleteFromTimelines(data.id));
            case 'merge':
            case 'unmerge':
              return store.dispatch(refreshTimeline('home'));
56
          }
57 58 59 60 61
        }
      });
    }
  },

62
  render () {
63 64
    return (
      <Provider store={store}>
65
        <Router history={hashHistory}>
66
          <Route path='/' component={UI}>
67 68 69 70
            <Route path='/settings' component={Settings} />
            <Route path='/subscriptions' component={Subscriptions} />
            <Route path='/statuses/:statusId' component={Status} />
            <Route path='/accounts/:accountId' component={Account} />
71 72
          </Route>
        </Router>
73 74 75 76 77 78
      </Provider>
    );
  }

});

79
export default Mastodon;