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

14
const store = configureStore();
15

16
const Mastodon = React.createClass({
17

18 19
  propTypes: {
    token: React.PropTypes.string.isRequired,
20 21
    timelines: React.PropTypes.object,
    account: React.PropTypes.string
22 23 24 25
  },

  mixins: [PureRenderMixin],

26
  componentWillMount() {
27
    store.dispatch(setAccessToken(this.props.token));
28
    store.dispatch(setAccountSelf(JSON.parse(this.props.account)));
29

30 31 32 33 34 35 36 37 38 39 40 41 42
    for (var timelineType in this.props.timelines) {
      if (this.props.timelines.hasOwnProperty(timelineType)) {
        store.dispatch(setTimeline(timelineType, JSON.parse(this.props.timelines[timelineType])));
      }
    }

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

        disconnected: function() {},

        received: function(data) {
43 44 45 46 47 48 49 50
          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'));
51
          }
52 53 54 55 56
        }
      });
    }
  },

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

});

74
export default Mastodon;