You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
7.2 KiB

  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import configureStore from '../store/configureStore';
  5. import {
  6. refreshTimelineSuccess,
  7. updateTimeline,
  8. deleteFromTimelines,
  9. refreshTimeline,
  10. connectTimeline,
  11. disconnectTimeline
  12. } from '../actions/timelines';
  13. import { showOnboardingOnce } from '../actions/onboarding';
  14. import { updateNotifications, refreshNotifications } from '../actions/notifications';
  15. import createBrowserHistory from 'history/lib/createBrowserHistory';
  16. import {
  17. applyRouterMiddleware,
  18. useRouterHistory,
  19. Router,
  20. Route,
  21. IndexRedirect,
  22. IndexRoute
  23. } from 'react-router';
  24. import { useScroll } from 'react-router-scroll';
  25. import UI from '../features/ui';
  26. import Status from '../features/status';
  27. import GettingStarted from '../features/getting_started';
  28. import PublicTimeline from '../features/public_timeline';
  29. import CommunityTimeline from '../features/community_timeline';
  30. import AccountTimeline from '../features/account_timeline';
  31. import HomeTimeline from '../features/home_timeline';
  32. import Compose from '../features/compose';
  33. import Followers from '../features/followers';
  34. import Following from '../features/following';
  35. import Reblogs from '../features/reblogs';
  36. import Favourites from '../features/favourites';
  37. import HashtagTimeline from '../features/hashtag_timeline';
  38. import Notifications from '../features/notifications';
  39. import FollowRequests from '../features/follow_requests';
  40. import GenericNotFound from '../features/generic_not_found';
  41. import FavouritedStatuses from '../features/favourited_statuses';
  42. import Blocks from '../features/blocks';
  43. import Mutes from '../features/mutes';
  44. import Report from '../features/report';
  45. import { IntlProvider, addLocaleData } from 'react-intl';
  46. import ar from 'react-intl/locale-data/ar';
  47. import bg from 'react-intl/locale-data/bg';
  48. import ca from 'react-intl/locale-data/ca';
  49. import de from 'react-intl/locale-data/de';
  50. import en from 'react-intl/locale-data/en';
  51. import eo from 'react-intl/locale-data/eo';
  52. import es from 'react-intl/locale-data/es';
  53. import fa from 'react-intl/locale-data/fa';
  54. import fi from 'react-intl/locale-data/fi';
  55. import fr from 'react-intl/locale-data/fr';
  56. import he from 'react-intl/locale-data/he';
  57. import hr from 'react-intl/locale-data/hr';
  58. import hu from 'react-intl/locale-data/hu';
  59. import id from 'react-intl/locale-data/id';
  60. import it from 'react-intl/locale-data/it';
  61. import ja from 'react-intl/locale-data/ja';
  62. import nl from 'react-intl/locale-data/nl';
  63. import no from 'react-intl/locale-data/no';
  64. import oc from '../locales/locale-data/oc';
  65. import pt from 'react-intl/locale-data/pt';
  66. import ru from 'react-intl/locale-data/ru';
  67. import uk from 'react-intl/locale-data/uk';
  68. import zh from 'react-intl/locale-data/zh';
  69. import tr from 'react-intl/locale-data/tr';
  70. import getMessagesForLocale from '../locales';
  71. import { hydrateStore } from '../actions/store';
  72. import createStream from '../stream';
  73. const store = configureStore();
  74. const initialState = JSON.parse(document.getElementById("initial-state").textContent);
  75. store.dispatch(hydrateStore(initialState));
  76. const browserHistory = useRouterHistory(createBrowserHistory)({
  77. basename: '/web'
  78. });
  79. addLocaleData([
  80. ...ar,
  81. ...bg,
  82. ...ca,
  83. ...de,
  84. ...en,
  85. ...eo,
  86. ...es,
  87. ...fa,
  88. ...fi,
  89. ...fr,
  90. ...he,
  91. ...hr,
  92. ...hu,
  93. ...id,
  94. ...it,
  95. ...ja,
  96. ...nl,
  97. ...no,
  98. ...oc,
  99. ...pt,
  100. ...ru,
  101. ...uk,
  102. ...zh,
  103. ...tr,
  104. ]);
  105. class Mastodon extends React.PureComponent {
  106. componentDidMount() {
  107. const { locale } = this.props;
  108. const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
  109. const accessToken = store.getState().getIn(['meta', 'access_token']);
  110. const setupPolling = () => {
  111. this.polling = setInterval(() => {
  112. store.dispatch(refreshTimeline('home'));
  113. store.dispatch(refreshNotifications());
  114. }, 20000);
  115. };
  116. const clearPolling = () => {
  117. clearInterval(this.polling);
  118. this.polling = undefined;
  119. };
  120. this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
  121. connected () {
  122. clearPolling();
  123. store.dispatch(connectTimeline('home'));
  124. },
  125. disconnected () {
  126. setupPolling();
  127. store.dispatch(disconnectTimeline('home'));
  128. },
  129. received (data) {
  130. switch(data.event) {
  131. case 'update':
  132. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  133. break;
  134. case 'delete':
  135. store.dispatch(deleteFromTimelines(data.payload));
  136. break;
  137. case 'notification':
  138. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  139. break;
  140. }
  141. },
  142. reconnected () {
  143. clearPolling();
  144. store.dispatch(connectTimeline('home'));
  145. store.dispatch(refreshTimeline('home'));
  146. store.dispatch(refreshNotifications());
  147. }
  148. });
  149. // Desktop notifications
  150. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  151. Notification.requestPermission();
  152. }
  153. store.dispatch(showOnboardingOnce());
  154. }
  155. componentWillUnmount () {
  156. if (typeof this.subscription !== 'undefined') {
  157. this.subscription.close();
  158. this.subscription = null;
  159. }
  160. if (typeof this.polling !== 'undefined') {
  161. clearInterval(this.polling);
  162. this.polling = null;
  163. }
  164. }
  165. render () {
  166. const { locale } = this.props;
  167. return (
  168. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  169. <Provider store={store}>
  170. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  171. <Route path='/' component={UI}>
  172. <IndexRedirect to='/getting-started' />
  173. <Route path='getting-started' component={GettingStarted} />
  174. <Route path='timelines/home' component={HomeTimeline} />
  175. <Route path='timelines/public' component={PublicTimeline} />
  176. <Route path='timelines/public/local' component={CommunityTimeline} />
  177. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  178. <Route path='notifications' component={Notifications} />
  179. <Route path='favourites' component={FavouritedStatuses} />
  180. <Route path='statuses/new' component={Compose} />
  181. <Route path='statuses/:statusId' component={Status} />
  182. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  183. <Route path='statuses/:statusId/favourites' component={Favourites} />
  184. <Route path='accounts/:accountId' component={AccountTimeline} />
  185. <Route path='accounts/:accountId/followers' component={Followers} />
  186. <Route path='accounts/:accountId/following' component={Following} />
  187. <Route path='follow_requests' component={FollowRequests} />
  188. <Route path='blocks' component={Blocks} />
  189. <Route path='mutes' component={Mutes} />
  190. <Route path='report' component={Report} />
  191. <Route path='*' component={GenericNotFound} />
  192. </Route>
  193. </Router>
  194. </Provider>
  195. </IntlProvider>
  196. );
  197. }
  198. }
  199. Mastodon.propTypes = {
  200. locale: PropTypes.string.isRequired
  201. };
  202. export default Mastodon;