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

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