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

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