mastodon.jsx 4.64 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
} from 'react-router';
import { useScroll } from 'react-router-scroll';
import UI from '../features/ui';
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 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
32
import Notifications from '../features/notifications';
33
import FollowRequests from '../features/follow_requests';
Eugen Rochko's avatar
Eugen Rochko committed
34
import GenericNotFound from '../features/generic_not_found';
35
import FavouritedStatuses from '../features/favourited_statuses';
Eugen Rochko's avatar
Eugen Rochko committed
36 37
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
38
import de from 'react-intl/locale-data/de';
Eugen Rochko's avatar
Eugen Rochko committed
39
import es from 'react-intl/locale-data/es';
nicolas's avatar
nicolas committed
40
import fr from 'react-intl/locale-data/fr';
41
import pt from 'react-intl/locale-data/pt';
42
import hu from 'react-intl/locale-data/hu';
Stanislav Makar's avatar
Stanislav Makar committed
43
import uk from 'react-intl/locale-data/uk';
Eugen Rochko's avatar
Eugen Rochko committed
44
import getMessagesForLocale from '../locales';
Eugen Rochko's avatar
Eugen Rochko committed
45
import { hydrateStore } from '../actions/store';
46

47
const store = configureStore();
48

Eugen Rochko's avatar
Eugen Rochko committed
49 50
store.dispatch(hydrateStore(window.INITIAL_STATE));

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

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

57
const Mastodon = React.createClass({
58

59
  propTypes: {
60
    locale: React.PropTypes.string.isRequired
61 62
  },

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

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

        received (data) {
70
          switch(data.event) {
Eugen Rochko's avatar
Eugen Rochko committed
71
          case 'update':
72
            store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
Eugen Rochko's avatar
Eugen Rochko committed
73 74
            break;
          case 'delete':
75
            store.dispatch(deleteFromTimelines(data.payload));
Eugen Rochko's avatar
Eugen Rochko committed
76 77
            break;
          case 'notification':
78
            store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
Eugen Rochko's avatar
Eugen Rochko committed
79
            break;
80
          }
81
        }
Eugen Rochko's avatar
Eugen Rochko committed
82

83 84
      });
    }
Eugen Rochko's avatar
Eugen Rochko committed
85 86

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

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

98
  render () {
99 100
    const { locale } = this.props;

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

108
              <Route path='getting-started' component={GettingStarted} />
109 110 111 112
              <Route path='timelines/home' component={HomeTimeline} />
              <Route path='timelines/public' component={PublicTimeline} />
              <Route path='timelines/tag/:id' component={HashtagTimeline} />

Eugen Rochko's avatar
Eugen Rochko committed
113
              <Route path='notifications' component={Notifications} />
114
              <Route path='favourites' component={FavouritedStatuses} />
Eugen Rochko's avatar
Eugen Rochko committed
115

116 117 118 119 120
              <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} />

121 122 123
              <Route path='accounts/:accountId' component={AccountTimeline} />
              <Route path='accounts/:accountId/followers' component={Followers} />
              <Route path='accounts/:accountId/following' component={Following} />
124 125

              <Route path='follow_requests' component={FollowRequests} />
Eugen Rochko's avatar
Eugen Rochko committed
126
              <Route path='*' component={GenericNotFound} />
127
            </Route>
128 129 130
          </Router>
        </Provider>
      </IntlProvider>
131 132 133 134 135
    );
  }

});

136
export default Mastodon;