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.

BaseApp.js 7.6KB

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