mastodon.jsx 2.67 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
      }
    }

    if (typeof App !== 'undefined') {
      App.timeline = App.cable.subscriptions.create("TimelineChannel", {
Eugen Rochko's avatar
Eugen Rochko committed
43
        connected () {
44

Eugen Rochko's avatar
Eugen Rochko committed
45
        },
46

Eugen Rochko's avatar
Eugen Rochko committed
47 48 49 50 51
        disconnected () {

        },

        received (data) {
52 53 54 55 56 57 58 59
          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'));
Eugen Rochko's avatar
Eugen Rochko committed
60 61
            case 'block':
              return store.dispatch(refreshTimeline('mentions'));
62
          }
63 64 65 66 67
        }
      });
    }
  },

68
  render () {
69 70
    return (
      <Provider store={store}>
71
        <Router history={hashHistory}>
72
          <Route path='/' component={UI}>
73 74 75 76
            <Route path='/settings' component={Settings} />
            <Route path='/subscriptions' component={Subscriptions} />
            <Route path='/statuses/:statusId' component={Status} />
            <Route path='/accounts/:accountId' component={Account} />
77 78
          </Route>
        </Router>
79 80 81 82 83 84
      </Provider>
    );
  }

});

85
export default Mastodon;