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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // @flow
  2. import _ from 'lodash';
  3. import React, { Component, Fragment } from 'react';
  4. import { I18nextProvider } from 'react-i18next';
  5. import { Provider } from 'react-redux';
  6. import { compose, createStore } from 'redux';
  7. import Thunk from 'redux-thunk';
  8. import { i18next } from '../../i18n';
  9. import {
  10. MiddlewareRegistry,
  11. ReducerRegistry,
  12. StateListenerRegistry
  13. } from '../../redux';
  14. import { SoundCollection } from '../../sounds';
  15. import { PersistenceRegistry } from '../../storage';
  16. import { appWillMount, appWillUnmount } from '../actions';
  17. declare var APP: Object;
  18. /**
  19. * The type of the React {@code Component} state of {@link BaseApp}.
  20. */
  21. type State = {
  22. /**
  23. * The {@code Route} rendered by the {@code BaseApp}.
  24. */
  25. route: Object,
  26. /**
  27. * The redux store used by the {@code BaseApp}.
  28. */
  29. store: Object
  30. };
  31. /**
  32. * Base (abstract) class for main App component.
  33. *
  34. * @abstract
  35. */
  36. export default class BaseApp extends Component<*, State> {
  37. _init: Promise<*>;
  38. /**
  39. * Initializes a new {@code BaseApp} instance.
  40. *
  41. * @param {Object} props - The read-only React {@code Component} props with
  42. * which the new instance is to be initialized.
  43. */
  44. constructor(props: Object) {
  45. super(props);
  46. this.state = {
  47. route: {},
  48. store: undefined
  49. };
  50. }
  51. /**
  52. * Initializes the app.
  53. *
  54. * @inheritdoc
  55. */
  56. componentDidMount() {
  57. /**
  58. * Make the mobile {@code BaseApp} wait until the {@code AsyncStorage}
  59. * implementation of {@code Storage} initializes fully.
  60. *
  61. * @private
  62. * @see {@link #_initStorage}
  63. * @type {Promise}
  64. */
  65. this._init = this._initStorage()
  66. .catch(() => { /* BaseApp should always initialize! */ })
  67. .then(() => new Promise(resolve => {
  68. this.setState({
  69. store: this._createStore()
  70. }, resolve);
  71. }))
  72. .then(() => this.state.store.dispatch(appWillMount(this)))
  73. .catch(() => { /* BaseApp should always initialize! */ });
  74. }
  75. /**
  76. * De-initializes the app.
  77. *
  78. * @inheritdoc
  79. */
  80. componentWillUnmount() {
  81. this.state.store.dispatch(appWillUnmount(this));
  82. }
  83. /**
  84. * Delays this {@code BaseApp}'s startup until the {@code Storage}
  85. * implementation of {@code localStorage} initializes. While the
  86. * initialization is instantaneous on Web (with Web Storage API), it is
  87. * asynchronous on mobile/react-native.
  88. *
  89. * @private
  90. * @returns {Promise}
  91. */
  92. _initStorage(): Promise<*> {
  93. const { _initializing } = window.localStorage;
  94. return _initializing || Promise.resolve();
  95. }
  96. /**
  97. * Implements React's {@link Component#render()}.
  98. *
  99. * @inheritdoc
  100. * @returns {ReactElement}
  101. */
  102. render() {
  103. const { route: { component }, store } = this.state;
  104. if (store && component) {
  105. return (
  106. <I18nextProvider i18n = { i18next }>
  107. <Provider store = { store }>
  108. <Fragment>
  109. { this._createMainElement(component) }
  110. <SoundCollection />
  111. { this._createExtraElement() }
  112. { this._renderDialogContainer() }
  113. </Fragment>
  114. </Provider>
  115. </I18nextProvider>
  116. );
  117. }
  118. return null;
  119. }
  120. /**
  121. * Creates an extra {@link ReactElement}s to be added (unconditionaly)
  122. * alongside the main element.
  123. *
  124. * @returns {ReactElement}
  125. * @abstract
  126. * @protected
  127. */
  128. _createExtraElement() {
  129. return null;
  130. }
  131. /**
  132. * Creates a {@link ReactElement} from the specified component, the
  133. * specified props and the props of this {@code AbstractApp} which are
  134. * suitable for propagation to the children of this {@code Component}.
  135. *
  136. * @param {Component} component - The component from which the
  137. * {@code ReactElement} is to be created.
  138. * @param {Object} props - The read-only React {@code Component} props with
  139. * which the {@code ReactElement} is to be initialized.
  140. * @returns {ReactElement}
  141. * @protected
  142. */
  143. _createMainElement(component, props) {
  144. return React.createElement(component, props || {});
  145. }
  146. /**
  147. * Initializes a new redux store instance suitable for use by this
  148. * {@code AbstractApp}.
  149. *
  150. * @private
  151. * @returns {Store} - A new redux store instance suitable for use by
  152. * this {@code AbstractApp}.
  153. */
  154. _createStore() {
  155. // Create combined reducer from all reducers in ReducerRegistry.
  156. const reducer = ReducerRegistry.combineReducers();
  157. // Apply all registered middleware from the MiddlewareRegistry and
  158. // additional 3rd party middleware:
  159. // - Thunk - allows us to dispatch async actions easily. For more info
  160. // @see https://github.com/gaearon/redux-thunk.
  161. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  162. // Try to enable Redux DevTools Chrome extension in order to make it
  163. // available for the purposes of facilitating development.
  164. let devToolsExtension;
  165. if (typeof window === 'object'
  166. && (devToolsExtension = window.devToolsExtension)) {
  167. middleware = compose(middleware, devToolsExtension());
  168. }
  169. const store = createStore(
  170. reducer, PersistenceRegistry.getPersistedState(), middleware);
  171. // StateListenerRegistry
  172. StateListenerRegistry.subscribe(store);
  173. // This is temporary workaround to be able to dispatch actions from
  174. // non-reactified parts of the code (conference.js for example).
  175. // Don't use in the react code!!!
  176. // FIXME: remove when the reactification is finished!
  177. if (typeof APP !== 'undefined') {
  178. APP.store = store;
  179. }
  180. return store;
  181. }
  182. /**
  183. * Navigates to a specific Route.
  184. *
  185. * @param {Route} route - The Route to which to navigate.
  186. * @returns {Promise}
  187. */
  188. _navigate(route): Promise<*> {
  189. if (_.isEqual(route, this.state.route)) {
  190. return Promise.resolve();
  191. }
  192. if (route.href) {
  193. // This navigation requires loading a new URL in the browser.
  194. window.location.href = route.href;
  195. return Promise.resolve();
  196. }
  197. // XXX React's setState is asynchronous which means that the value of
  198. // this.state.route above may not even be correct. If the check is
  199. // performed before setState completes, the app may not navigate to the
  200. // expected route. In order to mitigate the problem, _navigate was
  201. // changed to return a Promise.
  202. return new Promise(resolve => {
  203. this.setState({ route }, resolve);
  204. });
  205. }
  206. /**
  207. * Renders the platform specific dialog container.
  208. *
  209. * @returns {React$Element}
  210. */
  211. _renderDialogContainer: () => React$Element<*>
  212. }