您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractApp.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 { localParticipantLeft } from '../../base/participants';
  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 { 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: {},
  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. store: this._maybeCreateStore(props)
  103. }));
  104. }
  105. /**
  106. * Init lib-jitsi-meet and create local participant when component is going
  107. * to be mounted.
  108. *
  109. * @inheritdoc
  110. */
  111. componentWillMount() {
  112. this._init.then(() => {
  113. const { dispatch } = this._getStore();
  114. dispatch(appWillMount(this));
  115. // We set the initialized state here and not in the constructor to
  116. // make sure that {@code componentWillMount} gets invoked before
  117. // the app tries to render the actual app content.
  118. this.setState({
  119. appAsyncInitialized: true
  120. });
  121. // If a URL was explicitly specified to this React Component,
  122. // then open it; otherwise, use a default.
  123. this._openURL(toURLString(this.props.url) || this._getDefaultURL());
  124. });
  125. }
  126. /**
  127. * Notifies this mounted React {@code Component} that it will receive new
  128. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  129. *
  130. * @inheritdoc
  131. * @param {Object} nextProps - The read-only React {@code Component} props
  132. * that this instance will receive.
  133. * @returns {void}
  134. */
  135. componentWillReceiveProps(nextProps) {
  136. const { props } = this;
  137. this._init.then(() => {
  138. // The consumer of this AbstractApp did not provide a redux store.
  139. if (typeof nextProps.store === 'undefined'
  140. // The consumer of this AbstractApp did provide a redux
  141. // store before. Which means that the consumer changed
  142. // their mind. In such a case this instance should create
  143. // its own internal redux store. If the consumer did not
  144. // provide a redux store before, then this instance is
  145. // using its own internal redux store already.
  146. && typeof props.store !== 'undefined') {
  147. this.setState({
  148. store: this._maybeCreateStore(nextProps)
  149. });
  150. }
  151. // Deal with URL changes.
  152. let { url } = nextProps;
  153. url = toURLString(url);
  154. if (toURLString(props.url) !== url
  155. // XXX Refer to the implementation of loadURLObject: in
  156. // ios/sdk/src/JitsiMeetView.m for further information.
  157. || props.timestamp !== nextProps.timestamp) {
  158. this._openURL(url || this._getDefaultURL());
  159. }
  160. });
  161. }
  162. /**
  163. * Dispose lib-jitsi-meet and remove local participant when component is
  164. * going to be unmounted.
  165. *
  166. * @inheritdoc
  167. */
  168. componentWillUnmount() {
  169. const { dispatch } = this._getStore();
  170. dispatch(localParticipantLeft());
  171. dispatch(appWillUnmount(this));
  172. }
  173. /**
  174. * Gets a {@code Location} object from the window with information about the
  175. * current location of the document. Explicitly defined to allow extenders
  176. * to override because React Native does not usually have a location
  177. * property on its window unless debugging remotely in which case the
  178. * browser that is the remote debugger will provide a location property on
  179. * the window.
  180. *
  181. * @public
  182. * @returns {Location} A {@code Location} object with information about the
  183. * current location of the document.
  184. */
  185. getWindowLocation() {
  186. return undefined;
  187. }
  188. /**
  189. * Delays this {@code AbstractApp}'s startup until the {@code Storage}
  190. * implementation of {@code localStorage} initializes. While the
  191. * initialization is instantaneous on Web (with Web Storage API), it is
  192. * asynchronous on mobile/react-native.
  193. *
  194. * @private
  195. * @returns {Promise}
  196. */
  197. _initStorage() {
  198. const localStorageInitializing = window.localStorage._initializing;
  199. return (
  200. typeof localStorageInitializing === 'undefined'
  201. ? Promise.resolve()
  202. : localStorageInitializing);
  203. }
  204. /**
  205. * Implements React's {@link Component#render()}.
  206. *
  207. * @inheritdoc
  208. * @returns {ReactElement}
  209. */
  210. render() {
  211. const { appAsyncInitialized, route } = this.state;
  212. const { component } = route;
  213. if (appAsyncInitialized && component) {
  214. return (
  215. <I18nextProvider i18n = { i18next }>
  216. <Provider store = { this._getStore() }>
  217. <Fragment>
  218. { this._createElement(component) }
  219. <SoundCollection />
  220. <OverlayContainer />
  221. </Fragment>
  222. </Provider>
  223. </I18nextProvider>
  224. );
  225. }
  226. return null;
  227. }
  228. /**
  229. * Creates a {@link ReactElement} from the specified component, the
  230. * specified props and the props of this {@code AbstractApp} which are
  231. * suitable for propagation to the children of this {@code Component}.
  232. *
  233. * @param {Component} component - The component from which the
  234. * {@code ReactElement} is to be created.
  235. * @param {Object} props - The read-only React {@code Component} props with
  236. * which the {@code ReactElement} is to be initialized.
  237. * @returns {ReactElement}
  238. * @protected
  239. */
  240. _createElement(component, props) {
  241. /* eslint-disable no-unused-vars */
  242. const {
  243. // Don't propagate the dispatch and store props because they usually
  244. // come from react-redux and programmers don't really expect them to
  245. // be inherited but rather explicitly connected.
  246. dispatch, // eslint-disable-line react/prop-types
  247. store,
  248. // The following props were introduced to be consumed entirely by
  249. // AbstractApp:
  250. defaultURL,
  251. url,
  252. // The remaining props, if any, are considered suitable for
  253. // propagation to the children of this Component.
  254. ...thisProps
  255. } = this.props;
  256. /* eslint-enable no-unused-vars */
  257. return React.createElement(component, {
  258. ...thisProps,
  259. ...props
  260. });
  261. }
  262. /**
  263. * Initializes a new redux store instance suitable for use by this
  264. * {@code AbstractApp}.
  265. *
  266. * @private
  267. * @returns {Store} - A new redux store instance suitable for use by
  268. * this {@code AbstractApp}.
  269. */
  270. _createStore() {
  271. // Create combined reducer from all reducers in ReducerRegistry.
  272. const reducer = ReducerRegistry.combineReducers();
  273. // Apply all registered middleware from the MiddlewareRegistry and
  274. // additional 3rd party middleware:
  275. // - Thunk - allows us to dispatch async actions easily. For more info
  276. // @see https://github.com/gaearon/redux-thunk.
  277. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  278. // Try to enable Redux DevTools Chrome extension in order to make it
  279. // available for the purposes of facilitating development.
  280. let devToolsExtension;
  281. if (typeof window === 'object'
  282. && (devToolsExtension = window.devToolsExtension)) {
  283. middleware = compose(middleware, devToolsExtension());
  284. }
  285. return (
  286. createStore(
  287. reducer,
  288. PersistenceRegistry.getPersistedState(),
  289. middleware));
  290. }
  291. /**
  292. * Gets the default URL to be opened when this {@code App} mounts.
  293. *
  294. * @protected
  295. * @returns {string} The default URL to be opened when this {@code App}
  296. * mounts.
  297. */
  298. _getDefaultURL() {
  299. // If the execution environment provides a Location abstraction, then
  300. // this App at already at that location but it must be made aware of the
  301. // fact.
  302. const windowLocation = this.getWindowLocation();
  303. if (windowLocation) {
  304. const href = windowLocation.toString();
  305. if (href) {
  306. return href;
  307. }
  308. }
  309. return (
  310. this.props.defaultURL
  311. || this._getStore().getState()['features/base/settings']
  312. .serverURL
  313. || DEFAULT_URL);
  314. }
  315. /**
  316. * Gets the redux store used by this {@code AbstractApp}.
  317. *
  318. * @protected
  319. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  320. */
  321. _getStore() {
  322. let store = this.state.store;
  323. if (typeof store === 'undefined') {
  324. store = this.props.store;
  325. }
  326. return store;
  327. }
  328. /**
  329. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  330. * store is not defined by the consumer of this {@code AbstractApp} through
  331. * its read-only React {@code Component} props.
  332. *
  333. * @param {Object} props - The read-only React {@code Component} props that
  334. * will eventually be received by this {@code AbstractApp}.
  335. * @private
  336. * @returns {Store} - The redux store to be used by this
  337. * {@code AbstractApp}.
  338. */
  339. _maybeCreateStore({ store }) {
  340. // The application Jitsi Meet is architected with redux. However, I do
  341. // not want consumers of the App React Component to be forced into
  342. // dealing with redux. If the consumer did not provide an external redux
  343. // store, utilize an internal redux store.
  344. if (typeof store === 'undefined') {
  345. // eslint-disable-next-line no-param-reassign
  346. store = this._createStore();
  347. // This is temporary workaround to be able to dispatch actions from
  348. // non-reactified parts of the code (conference.js for example).
  349. // Don't use in the react code!!!
  350. // FIXME: remove when the reactification is finished!
  351. if (typeof APP !== 'undefined') {
  352. APP.store = store;
  353. }
  354. }
  355. // StateListenerRegistry
  356. store && StateListenerRegistry.subscribe(store);
  357. return store;
  358. }
  359. /**
  360. * Navigates to a specific Route.
  361. *
  362. * @param {Route} route - The Route to which to navigate.
  363. * @returns {Promise}
  364. */
  365. _navigate(route) {
  366. if (_.isEqual(route, this.state.route)) {
  367. return Promise.resolve();
  368. }
  369. if (route.href) {
  370. // This navigation requires loading a new URL in the browser.
  371. window.location.href = route.href;
  372. return Promise.resolve();
  373. }
  374. // XXX React's setState is asynchronous which means that the value of
  375. // this.state.route above may not even be correct. If the check is
  376. // performed before setState completes, the app may not navigate to the
  377. // expected route. In order to mitigate the problem, _navigate was
  378. // changed to return a Promise.
  379. return new Promise(resolve => {
  380. this.setState({ route }, resolve);
  381. });
  382. }
  383. /**
  384. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  385. *
  386. * @param {Object|string} url - The URL to navigate this {@code AbstractApp}
  387. * to (i.e. the URL to open).
  388. * @protected
  389. * @returns {void}
  390. */
  391. _openURL(url) {
  392. this._getStore().dispatch(appNavigate(toURLString(url)));
  393. }
  394. }