mastodon.jsx 3.73 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
}                         from '../actions/timelines';
import { setAccessToken } from '../actions/meta';
import { setAccountSelf } from '../actions/accounts';
import PureRenderMixin    from 'react-addons-pure-render-mixin';
import {
13
  applyRouterMiddleware,
14 15 16 17 18
  Router,
  Route,
  hashHistory,
  IndexRoute
}                         from 'react-router';
19
import { useScroll }      from 'react-router-scroll';
20
import UI                 from '../features/ui';
21 22 23
import Account            from '../features/account';
import Status             from '../features/status';
import GettingStarted     from '../features/getting_started';
Eugen Rochko's avatar
Eugen Rochko committed
24
import PublicTimeline     from '../features/public_timeline';
25
import AccountTimeline    from '../features/account_timeline';
26 27 28
import HomeTimeline       from '../features/home_timeline';
import MentionsTimeline   from '../features/mentions_timeline';
import Compose            from '../features/compose';
29 30
import Followers          from '../features/followers';
import Following          from '../features/following';
31
import Reblogs            from '../features/reblogs';
32
import Favourites         from '../features/favourites';
Eugen Rochko's avatar
Eugen Rochko committed
33
import HashtagTimeline    from '../features/hashtag_timeline';
34

35
const store = configureStore();
36

37
const Mastodon = React.createClass({
38

39 40
  propTypes: {
    token: React.PropTypes.string.isRequired,
41 42
    timelines: React.PropTypes.object,
    account: React.PropTypes.string
43 44 45 46
  },

  mixins: [PureRenderMixin],

47
  componentWillMount() {
48
    store.dispatch(setAccessToken(this.props.token));
49
    store.dispatch(setAccountSelf(JSON.parse(this.props.account)));
50

51
    if (typeof App !== 'undefined') {
Eugen Rochko's avatar
Eugen Rochko committed
52
      this.subscription = App.cable.subscriptions.create('TimelineChannel', {
Eugen Rochko's avatar
Eugen Rochko committed
53 54

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

68 69 70 71
      });
    }
  },

Eugen Rochko's avatar
Eugen Rochko committed
72 73 74 75 76 77
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
      this.subscription.unsubscribe();
    }
  },

78
  render () {
79 80
    return (
      <Provider store={store}>
81
        <Router history={hashHistory} render={applyRouterMiddleware(useScroll())}>
82
          <Route path='/' component={UI}>
83
            <IndexRoute component={GettingStarted} />
84
            <Route path='/statuses/new' component={Compose} />
85

86 87
            <Route path='/statuses/home' component={HomeTimeline} />
            <Route path='/statuses/mentions' component={MentionsTimeline} />
Eugen Rochko's avatar
Eugen Rochko committed
88
            <Route path='/statuses/all' component={PublicTimeline} />
Eugen Rochko's avatar
Eugen Rochko committed
89
            <Route path='/statuses/tag/:id' component={HashtagTimeline} />
90

91
            <Route path='/statuses/:statusId' component={Status} />
92
            <Route path='/statuses/:statusId/reblogs' component={Reblogs} />
93 94
            <Route path='/statuses/:statusId/favourites' component={Favourites} />

95 96
            <Route path='/accounts/:accountId' component={Account}>
              <IndexRoute component={AccountTimeline} />
97 98
              <Route path='/accounts/:accountId/followers' component={Followers} />
              <Route path='/accounts/:accountId/following' component={Following} />
99
            </Route>
100 101
          </Route>
        </Router>
102 103 104 105 106 107
      </Provider>
    );
  }

});

108
export default Mastodon;