mastodon.jsx 4.65 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
} from '../actions/timelines';
Eugen Rochko's avatar
Eugen Rochko committed
9
import { updateNotifications } from '../actions/notifications';
10 11
import { setAccessToken } from '../actions/meta';
import { setAccountSelf } from '../actions/accounts';
12 13
import PureRenderMixin from 'react-addons-pure-render-mixin';
import createBrowserHistory from 'history/lib/createBrowserHistory';
14
import {
15
  applyRouterMiddleware,
16
  useRouterHistory,
17 18 19
  Router,
  Route,
  IndexRoute
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
} from 'react-router';
import { useScroll } from 'react-router-scroll';
import UI from '../features/ui';
import Account from '../features/account';
import Status from '../features/status';
import GettingStarted from '../features/getting_started';
import PublicTimeline from '../features/public_timeline';
import AccountTimeline from '../features/account_timeline';
import HomeTimeline from '../features/home_timeline';
import MentionsTimeline from '../features/mentions_timeline';
import Compose from '../features/compose';
import Followers from '../features/followers';
import Following from '../features/following';
import Reblogs from '../features/reblogs';
import Favourites from '../features/favourites';
import HashtagTimeline from '../features/hashtag_timeline';
Eugen Rochko's avatar
Eugen Rochko committed
36
import Notifications from '../features/notifications';
Eugen Rochko's avatar
Eugen Rochko committed
37 38
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
39
import de from 'react-intl/locale-data/de';
Eugen Rochko's avatar
Eugen Rochko committed
40
import es from 'react-intl/locale-data/es';
Eugen Rochko's avatar
Eugen Rochko committed
41
import getMessagesForLocale from '../locales';
42

43
const store = configureStore();
44

45 46 47 48
const browserHistory = useRouterHistory(createBrowserHistory)({
  basename: '/web'
});

Eugen Rochko's avatar
Eugen Rochko committed
49
addLocaleData([...en, ...de, ...es]);
Eugen Rochko's avatar
Eugen Rochko committed
50

51
const Mastodon = React.createClass({
52

53 54
  propTypes: {
    token: React.PropTypes.string.isRequired,
55
    timelines: React.PropTypes.object,
56 57
    account: React.PropTypes.string,
    locale: React.PropTypes.string.isRequired
58 59 60 61
  },

  mixins: [PureRenderMixin],

62
  componentWillMount() {
Eugen Rochko's avatar
Eugen Rochko committed
63 64 65 66
    const { token, account, locale } = this.props;

    store.dispatch(setAccessToken(token));
    store.dispatch(setAccountSelf(JSON.parse(account)));
67

68
    if (typeof App !== 'undefined') {
Eugen Rochko's avatar
Eugen Rochko committed
69
      this.subscription = App.cable.subscriptions.create('TimelineChannel', {
Eugen Rochko's avatar
Eugen Rochko committed
70 71

        received (data) {
72 73 74 75 76 77 78
          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':
79
              return store.dispatch(refreshTimeline('home', true));
Eugen Rochko's avatar
Eugen Rochko committed
80
            case 'block':
81
              return store.dispatch(refreshTimeline('mentions', true));
Eugen Rochko's avatar
Eugen Rochko committed
82
            case 'notification':
Eugen Rochko's avatar
Eugen Rochko committed
83
              return store.dispatch(updateNotifications(JSON.parse(data.message), getMessagesForLocale(locale), locale));
84
          }
85
        }
Eugen Rochko's avatar
Eugen Rochko committed
86

87 88
      });
    }
Eugen Rochko's avatar
Eugen Rochko committed
89 90 91 92 93

    // Desktop notifications
    if (Notification.permission === 'default') {
      Notification.requestPermission();
    }
94 95
  },

Eugen Rochko's avatar
Eugen Rochko committed
96 97 98 99 100 101
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
      this.subscription.unsubscribe();
    }
  },

102
  render () {
103 104
    const { locale } = this.props;

105
    return (
Eugen Rochko's avatar
Eugen Rochko committed
106
      <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
107 108 109 110 111 112 113 114 115 116
        <Provider store={store}>
          <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
            <Route path='/' component={UI}>
              <IndexRoute component={GettingStarted} />

              <Route path='timelines/home' component={HomeTimeline} />
              <Route path='timelines/mentions' component={MentionsTimeline} />
              <Route path='timelines/public' component={PublicTimeline} />
              <Route path='timelines/tag/:id' component={HashtagTimeline} />

Eugen Rochko's avatar
Eugen Rochko committed
117 118
              <Route path='notifications' component={Notifications} />

119 120 121 122 123 124 125 126 127 128
              <Route path='statuses/new' component={Compose} />
              <Route path='statuses/:statusId' component={Status} />
              <Route path='statuses/:statusId/reblogs' component={Reblogs} />
              <Route path='statuses/:statusId/favourites' component={Favourites} />

              <Route path='accounts/:accountId' component={Account}>
                <IndexRoute component={AccountTimeline} />
                <Route path='followers' component={Followers} />
                <Route path='following' component={Following} />
              </Route>
129
            </Route>
130 131 132
          </Router>
        </Provider>
      </IntlProvider>
133 134 135 136 137
    );
  }

});

138
export default Mastodon;