mastodon.jsx 6.19 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 Mutes from '../features/mutes';
41
import Report from '../features/report';
Eugen Rochko's avatar
Eugen Rochko committed
42 43
import { IntlProvider, addLocaleData } from 'react-intl';
import en from 'react-intl/locale-data/en';
Eugen Rochko's avatar
Eugen Rochko committed
44
import de from 'react-intl/locale-data/de';
45
import eo from 'react-intl/locale-data/eo';
Eugen Rochko's avatar
Eugen Rochko committed
46
import es from 'react-intl/locale-data/es';
47
import fi from 'react-intl/locale-data/fi';
nicolas's avatar
nicolas committed
48
import fr from 'react-intl/locale-data/fr';
49
import hu from 'react-intl/locale-data/hu';
pinfort's avatar
pinfort committed
50
import ja from 'react-intl/locale-data/ja';
51
import pt from 'react-intl/locale-data/pt';
52
import nl from 'react-intl/locale-data/nl';
53
import no from 'react-intl/locale-data/no';
54 55 56
import ru from 'react-intl/locale-data/ru';
import uk from 'react-intl/locale-data/uk';
import zh from 'react-intl/locale-data/zh';
57
import bg from 'react-intl/locale-data/bg';
58
import { localeData as zh_hk } from '../locales/zh-hk';
Eugen Rochko's avatar
Eugen Rochko committed
59
import getMessagesForLocale from '../locales';
Eugen Rochko's avatar
Eugen Rochko committed
60
import { hydrateStore } from '../actions/store';
61
import createStream from '../stream';
62

63
const store = configureStore();
64 65
const initialState = JSON.parse(document.getElementById("initial-state").textContent);
store.dispatch(hydrateStore(initialState));
Eugen Rochko's avatar
Eugen Rochko committed
66

67 68 69 70
const browserHistory = useRouterHistory(createBrowserHistory)({
  basename: '/web'
});

71 72 73 74 75 76 77 78 79 80
addLocaleData([
  ...en,
  ...de,
  ...eo,
  ...es,
  ...fi,
  ...fr,
  ...hu,
  ...ja,
  ...pt,
81
  ...nl,
82
  ...no,
83 84 85 86
  ...ru,
  ...uk,
  ...zh,
  ...zh_hk,
87
  ...bg,
88
]);
89

90
const Mastodon = React.createClass({
91

92
  propTypes: {
93
    locale: React.PropTypes.string.isRequired
94 95
  },

96 97
  componentDidMount() {
    const { locale }  = this.props;
98
    const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
99
    const accessToken = store.getState().getIn(['meta', 'access_token']);
100

101
    this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
Eugen Rochko's avatar
Eugen Rochko committed
102

103 104 105 106 107 108 109 110
      connected () {
        store.dispatch(connectTimeline('home'));
      },

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

111 112 113 114 115 116 117 118 119 120 121
      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;
122
        }
123 124 125
      },

      reconnected () {
126
        store.dispatch(connectTimeline('home'));
127 128
        store.dispatch(refreshTimeline('home'));
        store.dispatch(refreshNotifications());
129
      }
Eugen Rochko's avatar
Eugen Rochko committed
130

131
    });
Eugen Rochko's avatar
Eugen Rochko committed
132 133

    // Desktop notifications
134
    if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
Eugen Rochko's avatar
Eugen Rochko committed
135 136
      Notification.requestPermission();
    }
137 138
  },

Eugen Rochko's avatar
Eugen Rochko committed
139 140
  componentWillUnmount () {
    if (typeof this.subscription !== 'undefined') {
141 142
      this.subscription.close();
      this.subscription = null;
Eugen Rochko's avatar
Eugen Rochko committed
143 144 145
    }
  },

146
  render () {
147 148
    const { locale } = this.props;

149
    return (
Eugen Rochko's avatar
Eugen Rochko committed
150
      <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
151 152 153
        <Provider store={store}>
          <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
            <Route path='/' component={UI}>
154
              <IndexRedirect to="/getting-started" />
155

156
              <Route path='getting-started' component={GettingStarted} />
157 158
              <Route path='timelines/home' component={HomeTimeline} />
              <Route path='timelines/public' component={PublicTimeline} />
159
              <Route path='timelines/public/local' component={CommunityTimeline} />
160 161
              <Route path='timelines/tag/:id' component={HashtagTimeline} />

Eugen Rochko's avatar
Eugen Rochko committed
162
              <Route path='notifications' component={Notifications} />
163
              <Route path='favourites' component={FavouritedStatuses} />
Eugen Rochko's avatar
Eugen Rochko committed
164

165 166 167 168 169
              <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} />

170 171 172
              <Route path='accounts/:accountId' component={AccountTimeline} />
              <Route path='accounts/:accountId/followers' component={Followers} />
              <Route path='accounts/:accountId/following' component={Following} />
173 174

              <Route path='follow_requests' component={FollowRequests} />
175
              <Route path='blocks' component={Blocks} />
176
              <Route path='mutes' component={Mutes} />
177
              <Route path='report' component={Report} />
178

Eugen Rochko's avatar
Eugen Rochko committed
179
              <Route path='*' component={GenericNotFound} />
180
            </Route>
181 182 183
          </Router>
        </Provider>
      </IntlProvider>
184 185 186 187 188
    );
  }

});

189
export default Mastodon;