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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* global APP */
  2. import PropTypes from 'prop-types';
  3. import React, { Component } 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 '../../base/i18n';
  9. import { localParticipantLeft } from '../../base/participants';
  10. import { Fragment, RouteRegistry } from '../../base/react';
  11. import {
  12. MiddlewareRegistry,
  13. ReducerRegistry,
  14. StateListenerRegistry
  15. } from '../../base/redux';
  16. import { SoundCollection } from '../../base/sounds';
  17. import { PersistenceRegistry } from '../../base/storage';
  18. import { toURLString } from '../../base/util';
  19. import { OverlayContainer } from '../../overlay';
  20. import { BlankPage } from '../../welcome';
  21. import { appNavigate, appWillMount, appWillUnmount } from '../actions';
  22. /**
  23. * The default URL to open if no other was specified to {@code AbstractApp}
  24. * via props.
  25. *
  26. * FIXME: This is not at the best place here. This should be either in the
  27. * base/settings feature or a default in base/config.
  28. */
  29. const DEFAULT_URL = 'https://meet.jit.si';
  30. /**
  31. * Base (abstract) class for main App component.
  32. *
  33. * @abstract
  34. */
  35. export class AbstractApp extends Component {
  36. /**
  37. * {@code AbstractApp} component's property types.
  38. *
  39. * @static
  40. */
  41. static propTypes = {
  42. /**
  43. * The default URL {@code AbstractApp} is to open when not in any
  44. * conference/room.
  45. */
  46. defaultURL: PropTypes.string,
  47. /**
  48. * (Optional) redux store for this app.
  49. */
  50. store: PropTypes.object,
  51. // XXX Refer to the implementation of loadURLObject: in
  52. // ios/sdk/src/JitsiMeetView.m for further information.
  53. timestamp: PropTypes.any,
  54. /**
  55. * The URL, if any, with which the app was launched.
  56. */
  57. url: PropTypes.oneOfType([
  58. PropTypes.object,
  59. PropTypes.string
  60. ])
  61. };
  62. /**
  63. * Initializes a new {@code AbstractApp} instance.
  64. *
  65. * @param {Object} props - The read-only React {@code Component} props with
  66. * which the new instance is to be initialized.
  67. */
  68. constructor(props) {
  69. super(props);
  70. this.state = {
  71. /**
  72. * The Route rendered by this {@code AbstractApp}.
  73. *
  74. * @type {Route}
  75. */
  76. route: undefined,
  77. /**
  78. * The state of the »possible« async initialization of
  79. * the {@code AbstractApp}.
  80. */
  81. appAsyncInitialized: false,
  82. /**
  83. * The redux store used by this {@code AbstractApp}.
  84. *
  85. * @type {Store}
  86. */
  87. store: undefined
  88. };
  89. /**
  90. * Make the mobile {@code AbstractApp} wait until the
  91. * {@code AsyncStorage} implementation of {@code Storage} initializes
  92. * fully.
  93. *
  94. * @private
  95. * @see {@link #_initStorage}
  96. * @type {Promise}
  97. */
  98. this._init
  99. = this._initStorage()
  100. .catch(() => { /* AbstractApp should always initialize! */ })
  101. .then(() =>
  102. this.setState({
  103. route: undefined,
  104. store: this._maybeCreateStore(props)
  105. }));
  106. }
  107. /**
  108. * Init lib-jitsi-meet and create local participant when component is going
  109. * to be mounted.
  110. *
  111. * @inheritdoc
  112. */
  113. componentWillMount() {
  114. this._init.then(() => {
  115. const { dispatch } = this._getStore();
  116. dispatch(appWillMount(this));
  117. // We set the initialized state here and not in the contructor to
  118. // make sure that {@code componentWillMount} gets invoked before
  119. // the app tries to render the actual app content.
  120. this.setState({
  121. appAsyncInitialized: true
  122. });
  123. // If a URL was explicitly specified to this React Component,
  124. // then open it; otherwise, use a default.
  125. this._openURL(toURLString(this.props.url) || this._getDefaultURL());
  126. });
  127. }
  128. /**
  129. * Notifies this mounted React {@code Component} that it will receive new
  130. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  131. *
  132. * @inheritdoc
  133. * @param {Object} nextProps - The read-only React {@code Component} props
  134. * that this instance will receive.
  135. * @returns {void}
  136. */
  137. componentWillReceiveProps(nextProps) {
  138. const { props } = this;
  139. this._init.then(() => {
  140. // The consumer of this AbstractApp did not provide a redux store.
  141. if (typeof nextProps.store === 'undefined'
  142. // The consumer of this AbstractApp did provide a redux
  143. // store before. Which means that the consumer changed
  144. // their mind. In such a case this instance should create
  145. // its own internal redux store. If the consumer did not
  146. // provide a redux store before, then this instance is
  147. // using its own internal redux store already.
  148. && typeof props.store !== 'undefined') {
  149. this.setState({
  150. store: this._maybeCreateStore(nextProps)
  151. });
  152. }
  153. // Deal with URL changes.
  154. let { url } = nextProps;
  155. url = toURLString(url);
  156. if (toURLString(props.url) !== url
  157. // XXX Refer to the implementation of loadURLObject: in
  158. // ios/sdk/src/JitsiMeetView.m for further information.
  159. || props.timestamp !== nextProps.timestamp) {
  160. this._openURL(url || this._getDefaultURL());
  161. }
  162. });
  163. }
  164. /**
  165. * Dispose lib-jitsi-meet and remove local participant when component is
  166. * going to be unmounted.
  167. *
  168. * @inheritdoc
  169. */
  170. componentWillUnmount() {
  171. const { dispatch } = this._getStore();
  172. dispatch(localParticipantLeft());
  173. dispatch(appWillUnmount(this));
  174. }
  175. /**
  176. * Gets a {@code Location} object from the window with information about the
  177. * current location of the document. Explicitly defined to allow extenders
  178. * to override because React Native does not usually have a location
  179. * property on its window unless debugging remotely in which case the
  180. * browser that is the remote debugger will provide a location property on
  181. * the window.
  182. *
  183. * @public
  184. * @returns {Location} A {@code Location} object with information about the
  185. * current location of the document.
  186. */
  187. getWindowLocation() {
  188. return undefined;
  189. }
  190. /**
  191. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  192. * implementation of {@code localStorage} initializes. While the
  193. * initialization is instantaneous on Web (with Web Storage API), it is
  194. * asynchronous on mobile/react-native.
  195. *
  196. * @private
  197. * @returns {Promise}
  198. */
  199. _initStorage() {
  200. const localStorageInitializing = window.localStorage._initializing;
  201. return (
  202. typeof localStorageInitializing === 'undefined'
  203. ? Promise.resolve()
  204. : localStorageInitializing);
  205. }
  206. /**
  207. * Implements React's {@link Component#render()}.
  208. *
  209. * @inheritdoc
  210. * @returns {ReactElement}
  211. */
  212. render() {
  213. const { appAsyncInitialized, route } = this.state;
  214. const component = (route && route.component) || BlankPage;
  215. if (appAsyncInitialized && component) {
  216. return (
  217. <I18nextProvider i18n = { i18next }>
  218. <Provider store = { this._getStore() }>
  219. <Fragment>
  220. { this._createElement(component) }
  221. <SoundCollection />
  222. <OverlayContainer />
  223. </Fragment>
  224. </Provider>
  225. </I18nextProvider>
  226. );
  227. }
  228. return null;
  229. }
  230. /**
  231. * Creates a {@link ReactElement} from the specified component, the
  232. * specified props and the props of this {@code AbstractApp} which are
  233. * suitable for propagation to the children of this {@code Component}.
  234. *
  235. * @param {Component} component - The component from which the
  236. * {@code ReactElement} is to be created.
  237. * @param {Object} props - The read-only React {@code Component} props with
  238. * which the {@code ReactElement} is to be initialized.
  239. * @returns {ReactElement}
  240. * @protected
  241. */
  242. _createElement(component, props) {
  243. /* eslint-disable no-unused-vars */
  244. const {
  245. // Don't propagate the dispatch and store props because they usually
  246. // come from react-redux and programmers don't really expect them to
  247. // be inherited but rather explicitly connected.
  248. dispatch, // eslint-disable-line react/prop-types
  249. store,
  250. // The following props were introduced to be consumed entirely by
  251. // AbstractApp:
  252. defaultURL,
  253. url,
  254. // The remaining props, if any, are considered suitable for
  255. // propagation to the children of this Component.
  256. ...thisProps
  257. } = this.props;
  258. /* eslint-enable no-unused-vars */
  259. return React.createElement(component, {
  260. ...thisProps,
  261. ...props
  262. });
  263. }
  264. /**
  265. * Initializes a new redux store instance suitable for use by this
  266. * {@code AbstractApp}.
  267. *
  268. * @private
  269. * @returns {Store} - A new redux store instance suitable for use by
  270. * this {@code AbstractApp}.
  271. */
  272. _createStore() {
  273. // Create combined reducer from all reducers in ReducerRegistry.
  274. const reducer = ReducerRegistry.combineReducers();
  275. // Apply all registered middleware from the MiddlewareRegistry and
  276. // additional 3rd party middleware:
  277. // - Thunk - allows us to dispatch async actions easily. For more info
  278. // @see https://github.com/gaearon/redux-thunk.
  279. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  280. // Try to enable Redux DevTools Chrome extension in order to make it
  281. // available for the purposes of facilitating development.
  282. let devToolsExtension;
  283. if (typeof window === 'object'
  284. && (devToolsExtension = window.devToolsExtension)) {
  285. middleware = compose(middleware, devToolsExtension());
  286. }
  287. return (
  288. createStore(
  289. reducer,
  290. PersistenceRegistry.getPersistedState(),
  291. middleware));
  292. }
  293. /**
  294. * Gets the default URL to be opened when this {@code App} mounts.
  295. *
  296. * @protected
  297. * @returns {string} The default URL to be opened when this {@code App}
  298. * mounts.
  299. */
  300. _getDefaultURL() {
  301. // If the execution environment provides a Location abstraction, then
  302. // this App at already at that location but it must be made aware of the
  303. // fact.
  304. const windowLocation = this.getWindowLocation();
  305. if (windowLocation) {
  306. const href = windowLocation.toString();
  307. if (href) {
  308. return href;
  309. }
  310. }
  311. return (
  312. this.props.defaultURL
  313. || this._getStore().getState()['features/base/settings']
  314. .serverURL
  315. || DEFAULT_URL);
  316. }
  317. /**
  318. * Gets the redux store used by this {@code AbstractApp}.
  319. *
  320. * @protected
  321. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  322. */
  323. _getStore() {
  324. let store = this.state.store;
  325. if (typeof store === 'undefined') {
  326. store = this.props.store;
  327. }
  328. return store;
  329. }
  330. /**
  331. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  332. * store is not defined by the consumer of this {@code AbstractApp} through
  333. * its read-only React {@code Component} props.
  334. *
  335. * @param {Object} props - The read-only React {@code Component} props that
  336. * will eventually be received by this {@code AbstractApp}.
  337. * @private
  338. * @returns {Store} - The redux store to be used by this
  339. * {@code AbstractApp}.
  340. */
  341. _maybeCreateStore({ store }) {
  342. // The application Jitsi Meet is architected with redux. However, I do
  343. // not want consumers of the App React Component to be forced into
  344. // dealing with redux. If the consumer did not provide an external redux
  345. // store, utilize an internal redux store.
  346. if (typeof store === 'undefined') {
  347. // eslint-disable-next-line no-param-reassign
  348. store = this._createStore();
  349. // This is temporary workaround to be able to dispatch actions from
  350. // non-reactified parts of the code (conference.js for example).
  351. // Don't use in the react code!!!
  352. // FIXME: remove when the reactification is finished!
  353. if (typeof APP !== 'undefined') {
  354. APP.store = store;
  355. }
  356. }
  357. // StateListenerRegistry
  358. store && StateListenerRegistry.subscribe(store);
  359. return store;
  360. }
  361. /**
  362. * Navigates to a specific Route.
  363. *
  364. * @param {Route} route - The Route to which to navigate.
  365. * @returns {Promise}
  366. */
  367. _navigate(route) {
  368. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  369. return Promise.resolve();
  370. }
  371. let nextState = {
  372. route
  373. };
  374. // The Web App was using react-router so it utilized react-router's
  375. // onEnter. During the removal of react-router, modifications were
  376. // minimized by preserving the onEnter interface:
  377. // (1) Router would provide its nextState to the Route's onEnter. As the
  378. // role of Router is now this AbstractApp and we use redux, provide the
  379. // redux store instead.
  380. // (2) A replace function would be provided to the Route in case it
  381. // chose to redirect to another path.
  382. route && this._onRouteEnter(route, this._getStore(), pathname => {
  383. if (pathname) {
  384. this._openURL(pathname);
  385. // Do not proceed with the route because it chose to redirect to
  386. // another path.
  387. nextState = undefined;
  388. } else {
  389. nextState.route = undefined;
  390. }
  391. });
  392. // XXX React's setState is asynchronous which means that the value of
  393. // this.state.route above may not even be correct. If the check is
  394. // performed before setState completes, the app may not navigate to the
  395. // expected route. In order to mitigate the problem, _navigate was
  396. // changed to return a Promise.
  397. return new Promise(resolve => {
  398. if (nextState) {
  399. this.setState(nextState, resolve);
  400. } else {
  401. resolve();
  402. }
  403. });
  404. }
  405. /**
  406. * Notifies this {@code App} that a specific Route is about to be rendered.
  407. *
  408. * @param {Route} route - The Route that is about to be rendered.
  409. * @private
  410. * @returns {void}
  411. */
  412. _onRouteEnter(route, ...args) {
  413. // Notify the route that it is about to be entered.
  414. const { onEnter } = route;
  415. typeof onEnter === 'function' && onEnter(...args);
  416. }
  417. /**
  418. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  419. *
  420. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  421. * to (i.e. the URL to open).
  422. * @protected
  423. * @returns {void}
  424. */
  425. _openURL(url) {
  426. this._getStore().dispatch(appNavigate(toURLString(url)));
  427. }
  428. }