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

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