mastodon.jsx 4.85 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
import createBrowserHistory from 'history/lib/createBrowserHistory';
11
import {
12
  applyRouterMiddleware,
13
  useRouterHistory,
14 15
  Router,
  Route,
16
  IndexRedirect,
17
  IndexRoute
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
} 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
34
import Notifications from '../features/notifications';
35
import FollowRequests from '../features/follow_requests';
Eugen Rochko's avatar
Eugen Rochko committed
36
import GenericNotFound from '../features/generic_not_found';
37
import FavouritedStatuses from '../features/favourited_statuses';
Eugen Rochko's avatar
Eugen Rochko committed
38 39
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
40
import de from 'react-intl/locale-data/de';
Eugen Rochko's avatar
Eugen Rochko committed
41
import es from 'react-intl/locale-data/es';
nicolas's avatar
nicolas committed
42
import fr from 'react-intl/locale-data/fr';
43
import pt from 'react-intl/locale-data/pt';
44
import hu from 'react-intl/locale-data/hu';
Stanislav Makar's avatar
Stanislav Makar committed
45
import uk from 'react-intl/locale-data/uk';
Eugen Rochko's avatar
Eugen Rochko committed
46
import getMessagesForLocale from '../locales';
Eugen Rochko's avatar
Eugen Rochko committed
47
import { hydrateStore } from '../actions/store';
48

49
const store = configureStore();
50

Eugen Rochko's avatar
Eugen Rochko committed
51 52
store.dispatch(hydrateStore(window.INITIAL_STATE));

53 54 55 56
const browserHistory = useRouterHistory(createBrowserHistory)({
  basename: '/web'
});

Stanislav Makar's avatar
Stanislav Makar committed
57
addLocaleData([...en, ...de, ...es, ...fr, ...pt, ...hu, ...uk]);
Eugen Rochko's avatar
Eugen Rochko committed
58

59
const Mastodon = React.createClass({
60

61
  propTypes: {
62
    locale: React.PropTypes.string.isRequired
63 64
  },

65
  componentWillMount() {
Eugen Rochko's avatar
Eugen Rochko committed
66
    const { locale } = this.props;
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
          switch(data.type) {
Eugen Rochko's avatar
Eugen Rochko committed
73 74 75 76 77 78 79 80 81
          case 'update':
            store.dispatch(updateTimeline(data.timeline, JSON.parse(data.message)));
            break;
          case 'delete':
            store.dispatch(deleteFromTimelines(data.id));
            break;
          case 'notification':
            store.dispatch(updateNotifications(JSON.parse(data.message), getMessagesForLocale(locale), locale));
            break;
82
          }
83
        }
Eugen Rochko's avatar
Eugen Rochko committed
84

85 86
      });
    }
Eugen Rochko's avatar
Eugen Rochko committed
87 88

    // Desktop notifications
89
    if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
Eugen Rochko's avatar
Eugen Rochko committed
90 91
      Notification.requestPermission();
    }
92 93
  },

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

100
  render () {
101 102
    const { locale } = this.props;

103
    return (
Eugen Rochko's avatar
Eugen Rochko committed
104
      <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
105 106 107
        <Provider store={store}>
          <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
            <Route path='/' component={UI}>
108
              <IndexRedirect to="/getting-started" />
109

110
              <Route path='getting-started' component={GettingStarted} />
111 112 113 114 115
              <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
116
              <Route path='notifications' component={Notifications} />
117
              <Route path='favourites' component={FavouritedStatuses} />
Eugen Rochko's avatar
Eugen Rochko committed
118

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 130

              <Route path='follow_requests' component={FollowRequests} />
Eugen Rochko's avatar
Eugen Rochko committed
131
              <Route path='*' component={GenericNotFound} />
132
            </Route>
133 134 135
          </Router>
        </Provider>
      </IntlProvider>
136 137 138 139 140
    );
  }

});

141
export default Mastodon;