mastodon.jsx 5.17 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';
9
import { updateNotifications, refreshNotifications } 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
} 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';
24
import CommunityTimeline from '../features/community_timeline';
25 26 27 28 29 30 31 32
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
33
import Notifications from '../features/notifications';
34
import FollowRequests from '../features/follow_requests';
Eugen Rochko's avatar
Eugen Rochko committed
35
import GenericNotFound from '../features/generic_not_found';
36
import FavouritedStatuses from '../features/favourited_statuses';
37
import Blocks from '../features/blocks';
38
import Report from '../features/report';
Eugen Rochko's avatar
Eugen Rochko committed
39 40
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
41
import de from 'react-intl/locale-data/de';
Eugen Rochko's avatar
Eugen Rochko committed
42
import es from 'react-intl/locale-data/es';
nicolas's avatar
nicolas committed
43
import fr from 'react-intl/locale-data/fr';
44
import pt from 'react-intl/locale-data/pt';
45
import hu from 'react-intl/locale-data/hu';
Stanislav Makar's avatar
Stanislav Makar committed
46
import uk from 'react-intl/locale-data/uk';
Eugen Rochko's avatar
Eugen Rochko committed
47
import getMessagesForLocale from '../locales';
Eugen Rochko's avatar
Eugen Rochko committed
48
import { hydrateStore } from '../actions/store';
49
import createStream from '../stream';
50

51
const store = configureStore();
52

Eugen Rochko's avatar
Eugen Rochko committed
53 54
store.dispatch(hydrateStore(window.INITIAL_STATE));

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

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

61
const Mastodon = React.createClass({
62

63
  propTypes: {
64
    locale: React.PropTypes.string.isRequired
65 66
  },

67 68 69
  componentDidMount() {
    const { locale }  = this.props;
    const accessToken = store.getState().getIn(['meta', 'access_token']);
70

71
    this.subscription = createStream(accessToken, 'user', {
Eugen Rochko's avatar
Eugen Rochko committed
72

73 74 75 76 77 78 79 80 81 82 83
      received (data) {
        switch(data.event) {
        case 'update':
          store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
          break;
        case 'delete':
          store.dispatch(deleteFromTimelines(data.payload));
          break;
        case 'notification':
          store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
          break;
84
        }
85 86 87 88 89
      },

      reconnected () {
        store.dispatch(refreshTimeline('home'));
        store.dispatch(refreshNotifications());
90
      }
Eugen Rochko's avatar
Eugen Rochko committed
91

92
    });
Eugen Rochko's avatar
Eugen Rochko committed
93 94

    // Desktop notifications
95
    if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
Eugen Rochko's avatar
Eugen Rochko committed
96 97
      Notification.requestPermission();
    }
98 99
  },

Eugen Rochko's avatar
Eugen Rochko committed
100 101
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
102 103
      this.subscription.close();
      this.subscription = null;
Eugen Rochko's avatar
Eugen Rochko committed
104 105 106
    }
  },

107
  render () {
108 109
    const { locale } = this.props;

110
    return (
Eugen Rochko's avatar
Eugen Rochko committed
111
      <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
112 113 114
        <Provider store={store}>
          <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
            <Route path='/' component={UI}>
115
              <IndexRedirect to="/getting-started" />
116

117
              <Route path='getting-started' component={GettingStarted} />
118 119
              <Route path='timelines/home' component={HomeTimeline} />
              <Route path='timelines/public' component={PublicTimeline} />
120
              <Route path='timelines/public/local' component={CommunityTimeline} />
121 122
              <Route path='timelines/tag/:id' component={HashtagTimeline} />

Eugen Rochko's avatar
Eugen Rochko committed
123
              <Route path='notifications' component={Notifications} />
124
              <Route path='favourites' component={FavouritedStatuses} />
Eugen Rochko's avatar
Eugen Rochko committed
125

126 127 128 129 130
              <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} />

131 132 133
              <Route path='accounts/:accountId' component={AccountTimeline} />
              <Route path='accounts/:accountId/followers' component={Followers} />
              <Route path='accounts/:accountId/following' component={Following} />
134 135

              <Route path='follow_requests' component={FollowRequests} />
136
              <Route path='blocks' component={Blocks} />
137
              <Route path='report' component={Report} />
138

Eugen Rochko's avatar
Eugen Rochko committed
139
              <Route path='*' component={GenericNotFound} />
140
            </Route>
141 142 143
          </Router>
        </Provider>
      </IntlProvider>
144 145 146 147 148
    );
  }

});

149
export default Mastodon;