mastodon.jsx 2.64 KB
Newer Older
1 2
import { Provider }       from 'react-redux';
import configureStore     from '../store/configureStore';
3 4 5 6 7
import {
  refreshTimelineSuccess,
  updateTimeline,
  deleteFromTimelines,
  refreshTimeline
8 9 10 11 12 13 14 15 16 17 18 19 20
}                         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,
  IndexRoute
}                         from 'react-router';
import Account            from '../features/account';
import Status             from '../features/status';
import GettingStarted     from '../features/getting_started';
Eugen Rochko's avatar
Eugen Rochko committed
21
import PublicTimeline     from '../features/public_timeline';
22
import UI                 from '../features/ui';
23

24
const store = configureStore();
25

26
const Mastodon = React.createClass({
27

28 29
  propTypes: {
    token: React.PropTypes.string.isRequired,
30 31
    timelines: React.PropTypes.object,
    account: React.PropTypes.string
32 33 34 35
  },

  mixins: [PureRenderMixin],

36
  componentWillMount() {
37
    store.dispatch(setAccessToken(this.props.token));
38
    store.dispatch(setAccountSelf(JSON.parse(this.props.account)));
39

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

    if (typeof App !== 'undefined') {
Eugen Rochko's avatar
Eugen Rochko committed
47
      this.subscription = App.cable.subscriptions.create('TimelineChannel', {
Eugen Rochko's avatar
Eugen Rochko committed
48 49

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

63 64 65 66
      });
    }
  },

Eugen Rochko's avatar
Eugen Rochko committed
67 68 69 70 71 72
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
      this.subscription.unsubscribe();
    }
  },

73
  render () {
74 75
    return (
      <Provider store={store}>
76
        <Router history={hashHistory}>
77
          <Route path='/' component={UI}>
78
            <IndexRoute component={GettingStarted} />
Eugen Rochko's avatar
Eugen Rochko committed
79
            <Route path='/statuses/all' component={PublicTimeline} />
80 81
            <Route path='/statuses/:statusId' component={Status} />
            <Route path='/accounts/:accountId' component={Account} />
82 83
          </Route>
        </Router>
84 85 86 87 88 89
      </Provider>
    );
  }

});

90
export default Mastodon;