mastodon.jsx 2.45 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 21
}                         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';
import UI                 from '../features/ui';
22

23
const store = configureStore();
24

25
const Mastodon = React.createClass({
26

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

  mixins: [PureRenderMixin],

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

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

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

Eugen Rochko's avatar
Eugen Rochko committed
49
        },
50

Eugen Rochko's avatar
Eugen Rochko committed
51 52 53 54 55
        disconnected () {

        },

        received (data) {
56 57 58 59 60 61 62 63
          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
64 65
            case 'block':
              return store.dispatch(refreshTimeline('mentions'));
66
          }
67 68 69 70 71
        }
      });
    }
  },

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

});

88
export default Mastodon;