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.7KB

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