mastodon.jsx 2.92 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
}                         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';
18
import UI                 from '../features/ui';
19 20 21
import Account            from '../features/account';
import Status             from '../features/status';
import GettingStarted     from '../features/getting_started';
Eugen Rochko's avatar
Eugen Rochko committed
22
import PublicTimeline     from '../features/public_timeline';
23
import AccountTimeline    from '../features/account_timeline';
24 25 26
import HomeTimeline       from '../features/home_timeline';
import MentionsTimeline   from '../features/mentions_timeline';
import Compose            from '../features/compose';
27

28
const store = configureStore();
29

30
const Mastodon = React.createClass({
31

32 33
  propTypes: {
    token: React.PropTypes.string.isRequired,
34 35
    timelines: React.PropTypes.object,
    account: React.PropTypes.string
36 37 38 39
  },

  mixins: [PureRenderMixin],

40
  componentWillMount() {
41
    store.dispatch(setAccessToken(this.props.token));
42
    store.dispatch(setAccountSelf(JSON.parse(this.props.account)));
43

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

        received (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'));
Eugen Rochko's avatar
Eugen Rochko committed
56 57
            case 'block':
              return store.dispatch(refreshTimeline('mentions'));
58
          }
59
        }
Eugen Rochko's avatar
Eugen Rochko committed
60

61 62 63 64
      });
    }
  },

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

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

});

93
export default Mastodon;