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.

AbstractApp.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* global APP */
  2. import _ from 'lodash';
  3. import PropTypes from 'prop-types';
  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 '../../base/i18n';
  10. import {
  11. MiddlewareRegistry,
  12. ReducerRegistry,
  13. StateListenerRegistry
  14. } from '../../base/redux';
  15. import { SoundCollection } from '../../base/sounds';
  16. import { PersistenceRegistry } from '../../base/storage';
  17. import { toURLString } from '../../base/util';
  18. import { OverlayContainer } from '../../overlay';
  19. import { appNavigate, appWillMount, appWillUnmount } from '../actions';
  20. import { getDefaultURL } from '../functions';
  21. /**
  22. * Base (abstract) class for main App component.
  23. *
  24. * @abstract
  25. */
  26. export class AbstractApp extends Component {
  27. /**
  28. * {@code AbstractApp} component's property types.
  29. *
  30. * @static
  31. */
  32. static propTypes = {
  33. /**
  34. * The default URL {@code AbstractApp} is to open when not in any
  35. * conference/room.
  36. */
  37. defaultURL: PropTypes.string,
  38. // XXX Refer to the implementation of loadURLObject: in
  39. // ios/sdk/src/JitsiMeetView.m for further information.
  40. timestamp: PropTypes.any,
  41. /**
  42. * The URL, if any, with which the app was launched.
  43. */
  44. url: PropTypes.oneOfType([
  45. PropTypes.object,
  46. PropTypes.string
  47. ])
  48. };
  49. /**
  50. * Initializes a new {@code AbstractApp} instance.
  51. *
  52. * @param {Object} props - The read-only React {@code Component} props with
  53. * which the new instance is to be initialized.
  54. */
  55. constructor(props) {
  56. super(props);
  57. this.state = {
  58. /**
  59. * The state of the »possible« async initialization of the
  60. * {@code AbstractApp}.
  61. */
  62. appAsyncInitialized: false,
  63. /**
  64. * The Route rendered by this {@code AbstractApp}.
  65. *
  66. * @type {Route}
  67. */
  68. route: {},
  69. /**
  70. * The redux store used by this {@code AbstractApp}.
  71. *
  72. * @type {Store}
  73. */
  74. store: undefined
  75. };
  76. /**
  77. * Make the mobile {@code AbstractApp} wait until the
  78. * {@code AsyncStorage} implementation of {@code Storage} initializes
  79. * fully.
  80. *
  81. * @private
  82. * @see {@link #_initStorage}
  83. * @type {Promise}
  84. */
  85. this._init
  86. = this._initStorage()
  87. .catch(() => { /* AbstractApp should always initialize! */ })
  88. .then(() =>
  89. this.setState({
  90. store: this._createStore()
  91. }));
  92. }
  93. /**
  94. * Initializes the app.
  95. *
  96. * @inheritdoc
  97. */
  98. componentWillMount() {
  99. this._init.then(() => {
  100. const { dispatch } = this.state.store;
  101. dispatch(appWillMount(this));
  102. // We set the initialized state here and not in the constructor to
  103. // make sure that {@code componentWillMount} gets invoked before the
  104. // app tries to render the actual app content.
  105. this.setState({
  106. appAsyncInitialized: true
  107. });
  108. // If a URL was explicitly specified to this React Component, then
  109. // open it; otherwise, use a default.
  110. this._openURL(toURLString(this.props.url) || this._getDefaultURL());
  111. });
  112. }
  113. /**
  114. * Notifies this mounted React {@code Component} that it will receive new
  115. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  116. *
  117. * @inheritdoc
  118. * @param {Object} nextProps - The read-only React {@code Component} props
  119. * that this instance will receive.
  120. * @returns {void}
  121. */
  122. componentWillReceiveProps(nextProps) {
  123. const { props } = this;
  124. this._init.then(() => {
  125. // Deal with URL changes.
  126. let { url } = nextProps;
  127. url = toURLString(url);
  128. if (toURLString(props.url) !== url
  129. // XXX Refer to the implementation of loadURLObject: in
  130. // ios/sdk/src/JitsiMeetView.m for further information.
  131. || props.timestamp !== nextProps.timestamp) {
  132. this._openURL(url || this._getDefaultURL());
  133. }
  134. });
  135. }
  136. /**
  137. * De-initializes the app.
  138. *
  139. * @inheritdoc
  140. */
  141. componentWillUnmount() {
  142. this.state.store.dispatch(appWillUnmount(this));
  143. }
  144. /**
  145. * Gets a {@code Location} object from the window with information about the
  146. * current location of the document. Explicitly defined to allow extenders
  147. * to override because React Native does not usually have a location
  148. * property on its window unless debugging remotely in which case the
  149. * browser that is the remote debugger will provide a location property on
  150. * the window.
  151. *
  152. * @public
  153. * @returns {Location} A {@code Location} object with information about the
  154. * current location of the document.
  155. */
  156. getWindowLocation() {
  157. return undefined;
  158. }
  159. /**
  160. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  161. * implementation of {@code localStorage} initializes. While the
  162. * initialization is instantaneous on Web (with Web Storage API), it is
  163. * asynchronous on mobile/react-native.
  164. *
  165. * @private
  166. * @returns {Promise}
  167. */
  168. _initStorage() {
  169. const localStorageInitializing = window.localStorage._initializing;
  170. return (
  171. typeof localStorageInitializing === 'undefined'
  172. ? Promise.resolve()
  173. : localStorageInitializing);
  174. }
  175. /**
  176. * Implements React's {@link Component#render()}.
  177. *
  178. * @inheritdoc
  179. * @returns {ReactElement}
  180. */
  181. render() {
  182. const { appAsyncInitialized, route, store } = this.state;
  183. const { component } = route;
  184. if (appAsyncInitialized && component) {
  185. return (
  186. <I18nextProvider i18n = { i18next }>
  187. <Provider store = { store }>
  188. <Fragment>
  189. { this._createElement(component) }
  190. <SoundCollection />
  191. <OverlayContainer />
  192. </Fragment>
  193. </Provider>
  194. </I18nextProvider>
  195. );
  196. }
  197. return null;
  198. }
  199. /**
  200. * Creates a {@link ReactElement} from the specified component, the
  201. * specified props and the props of this {@code AbstractApp} which are
  202. * suitable for propagation to the children of this {@code Component}.
  203. *
  204. * @param {Component} component - The component from which the
  205. * {@code ReactElement} is to be created.
  206. * @param {Object} props - The read-only React {@code Component} props with
  207. * which the {@code ReactElement} is to be initialized.
  208. * @returns {ReactElement}
  209. * @protected
  210. */
  211. _createElement(component, props) {
  212. /* eslint-disable no-unused-vars */
  213. const {
  214. // The following props were introduced to be consumed entirely by
  215. // AbstractApp:
  216. defaultURL,
  217. timestamp,
  218. url,
  219. // The remaining props, if any, are considered suitable for
  220. // propagation to the children of this Component.
  221. ...thisProps
  222. } = this.props;
  223. /* eslint-enable no-unused-vars */
  224. return React.createElement(component, {
  225. ...thisProps,
  226. ...props
  227. });
  228. }
  229. /**
  230. * Initializes a new redux store instance suitable for use by this
  231. * {@code AbstractApp}.
  232. *
  233. * @private
  234. * @returns {Store} - A new redux store instance suitable for use by this
  235. * {@code AbstractApp}.
  236. */
  237. _createStore() {
  238. // Create combined reducer from all reducers in ReducerRegistry.
  239. const reducer = ReducerRegistry.combineReducers();
  240. // Apply all registered middleware from the MiddlewareRegistry and
  241. // additional 3rd party middleware:
  242. // - Thunk - allows us to dispatch async actions easily. For more info
  243. // @see https://github.com/gaearon/redux-thunk.
  244. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  245. // Try to enable Redux DevTools Chrome extension in order to make it
  246. // available for the purposes of facilitating development.
  247. let devToolsExtension;
  248. if (typeof window === 'object'
  249. && (devToolsExtension = window.devToolsExtension)) {
  250. middleware = compose(middleware, devToolsExtension());
  251. }
  252. const store
  253. = createStore(
  254. reducer,
  255. PersistenceRegistry.getPersistedState(),
  256. middleware);
  257. // StateListenerRegistry
  258. StateListenerRegistry.subscribe(store);
  259. // This is temporary workaround to be able to dispatch actions from
  260. // non-reactified parts of the code (conference.js for example).
  261. // Don't use in the react code!!!
  262. // FIXME: remove when the reactification is finished!
  263. if (typeof APP !== 'undefined') {
  264. APP.store = store;
  265. }
  266. return store;
  267. }
  268. /**
  269. * Gets the default URL to be opened when this {@code App} mounts.
  270. *
  271. * @protected
  272. * @returns {string} The default URL to be opened when this {@code App}
  273. * mounts.
  274. */
  275. _getDefaultURL() {
  276. return getDefaultURL(this.state.store);
  277. }
  278. /**
  279. * Navigates to a specific Route.
  280. *
  281. * @param {Route} route - The Route to which to navigate.
  282. * @returns {Promise}
  283. */
  284. _navigate(route) {
  285. if (_.isEqual(route, this.state.route)) {
  286. return Promise.resolve();
  287. }
  288. if (route.href) {
  289. // This navigation requires loading a new URL in the browser.
  290. window.location.href = route.href;
  291. return Promise.resolve();
  292. }
  293. // XXX React's setState is asynchronous which means that the value of
  294. // this.state.route above may not even be correct. If the check is
  295. // performed before setState completes, the app may not navigate to the
  296. // expected route. In order to mitigate the problem, _navigate was
  297. // changed to return a Promise.
  298. return new Promise(resolve => this.setState({ route }, resolve));
  299. }
  300. /**
  301. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  302. *
  303. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  304. * to (i.e. the URL to open).
  305. * @protected
  306. * @returns {void}
  307. */
  308. _openURL(url) {
  309. this.state.store.dispatch(appNavigate(toURLString(url)));
  310. }
  311. }