mastodon.jsx 5.62 KB
Newer Older
1 2
import { Provider } from 'react-redux';
import configureStore from '../store/configureStore';
3 4 5 6
import {
  refreshTimelineSuccess,
  updateTimeline,
  deleteFromTimelines,
7 8 9
  refreshTimeline,
  connectTimeline,
  disconnectTimeline
10
} from '../actions/timelines';
11
import { updateNotifications, refreshNotifications } from '../actions/notifications';
12
import createBrowserHistory from 'history/lib/createBrowserHistory';
13
import {
14
  applyRouterMiddleware,
15
  useRouterHistory,
16 17
  Router,
  Route,
18
  IndexRedirect,
19
  IndexRoute
20 21 22 23 24 25
} 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';
26
import CommunityTimeline from '../features/community_timeline';
27 28 29 30 31 32 33 34
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
35
import Notifications from '../features/notifications';
36
import FollowRequests from '../features/follow_requests';
Eugen Rochko's avatar
Eugen Rochko committed
37
import GenericNotFound from '../features/generic_not_found';
38
import FavouritedStatuses from '../features/favourited_statuses';
39
import Blocks from '../features/blocks';
40
import Report from '../features/report';
Eugen Rochko's avatar
Eugen Rochko committed
41 42
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
43
import de from 'react-intl/locale-data/de';
Eugen Rochko's avatar
Eugen Rochko committed
44
import es from 'react-intl/locale-data/es';
nicolas's avatar
nicolas committed
45
import fr from 'react-intl/locale-data/fr';
46
import pt from 'react-intl/locale-data/pt';
47
import hu from 'react-intl/locale-data/hu';
Stanislav Makar's avatar
Stanislav Makar committed
48
import uk from 'react-intl/locale-data/uk';
JantsoP's avatar
JantsoP committed
49
import fi from 'react-intl/locale-data/fi';
50
import eo from 'react-intl/locale-data/eo';
51
import ru from 'react-intl/locale-data/ru';
pinfort's avatar
pinfort committed
52
import ja from 'react-intl/locale-data/ja';
53

Eugen Rochko's avatar
Eugen Rochko committed
54
import getMessagesForLocale from '../locales';
Eugen Rochko's avatar
Eugen Rochko committed
55
import { hydrateStore } from '../actions/store';
56
import createStream from '../stream';
57

58
const store = configureStore();
59

Eugen Rochko's avatar
Eugen Rochko committed
60 61
store.dispatch(hydrateStore(window.INITIAL_STATE));

62 63 64 65
const browserHistory = useRouterHistory(createBrowserHistory)({
  basename: '/web'
});

66

pinfort's avatar
pinfort committed
67
addLocaleData([...en, ...de, ...es, ...fr, ...pt, ...hu, ...uk, ...fi, ...eo, ...ru, ...ja]);
68

Eugen Rochko's avatar
Eugen Rochko committed
69

70
const Mastodon = React.createClass({
71

72
  propTypes: {
73
    locale: React.PropTypes.string.isRequired
74 75
  },

76 77 78
  componentDidMount() {
    const { locale }  = this.props;
    const accessToken = store.getState().getIn(['meta', 'access_token']);
79

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

82 83 84 85 86 87 88 89
      connected () {
        store.dispatch(connectTimeline('home'));
      },

      disconnected () {
        store.dispatch(disconnectTimeline('home'));
      },

90 91 92 93 94 95 96 97 98 99 100
      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;
101
        }
102 103 104
      },

      reconnected () {
105
        store.dispatch(connectTimeline('home'));
106 107
        store.dispatch(refreshTimeline('home'));
        store.dispatch(refreshNotifications());
108
      }
Eugen Rochko's avatar
Eugen Rochko committed
109

110
    });
Eugen Rochko's avatar
Eugen Rochko committed
111 112

    // Desktop notifications
113
    if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
Eugen Rochko's avatar
Eugen Rochko committed
114 115
      Notification.requestPermission();
    }
116 117
  },

Eugen Rochko's avatar
Eugen Rochko committed
118 119
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
120 121
      this.subscription.close();
      this.subscription = null;
Eugen Rochko's avatar
Eugen Rochko committed
122 123 124
    }
  },

125
  render () {
126 127
    const { locale } = this.props;

128
    return (
Eugen Rochko's avatar
Eugen Rochko committed
129
      <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
130 131 132
        <Provider store={store}>
          <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
            <Route path='/' component={UI}>
133
              <IndexRedirect to="/getting-started" />
134

135
              <Route path='getting-started' component={GettingStarted} />
136 137
              <Route path='timelines/home' component={HomeTimeline} />
              <Route path='timelines/public' component={PublicTimeline} />
138
              <Route path='timelines/public/local' component={CommunityTimeline} />
139 140
              <Route path='timelines/tag/:id' component={HashtagTimeline} />

Eugen Rochko's avatar
Eugen Rochko committed
141
              <Route path='notifications' component={Notifications} />
142
              <Route path='favourites' component={FavouritedStatuses} />
Eugen Rochko's avatar
Eugen Rochko committed
143

144 145 146 147 148
              <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} />

149 150 151
              <Route path='accounts/:accountId' component={AccountTimeline} />
              <Route path='accounts/:accountId/followers' component={Followers} />
              <Route path='accounts/:accountId/following' component={Following} />
152 153

              <Route path='follow_requests' component={FollowRequests} />
154
              <Route path='blocks' component={Blocks} />
155
              <Route path='report' component={Report} />
156

Eugen Rochko's avatar
Eugen Rochko committed
157
              <Route path='*' component={GenericNotFound} />
158
            </Route>
159 160 161
          </Router>
        </Provider>
      </IntlProvider>
162 163 164 165 166
    );
  }

});

167
export default Mastodon;