Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractApp.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 {
  15. getPersistedState,
  16. MiddlewareRegistry,
  17. ReducerRegistry
  18. } from '../../base/redux';
  19. import { getProfile } from '../../base/profile';
  20. import { toURLString } from '../../base/util';
  21. import { OverlayContainer } from '../../overlay';
  22. import { BlankPage } from '../../welcome';
  23. import { appNavigate, appWillMount, appWillUnmount } from '../actions';
  24. /**
  25. * The default URL to open if no other was specified to {@code AbstractApp}
  26. * via props.
  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. * This way we make the mobile version wait until the
  90. * {@code AsyncStorage} implementation of {@code Storage}
  91. * properly initializes. On web it does actually nothing, see
  92. * {@link #_initStorage}.
  93. */
  94. this.init = new Promise(resolve => {
  95. this._initStorage().then(() => {
  96. this.setState({
  97. route: undefined,
  98. store: this._maybeCreateStore(props)
  99. });
  100. resolve();
  101. });
  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 } = 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 = getProfile(this._getStore().getState());
  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. 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 this.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(this.props.url) !== url
  176. // XXX Refer to the implementation of loadURLObject: in
  177. // ios/sdk/src/JitsiMeetView.m for further information.
  178. || this.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 app start until the {@code Storage} implementation initialises.
  211. * This is instantaneous on web, but is async on mobile.
  212. *
  213. * @private
  214. * @returns {ReactElement}
  215. */
  216. _initStorage() {
  217. return new Promise(resolve => {
  218. if (window.localStorage._initializing) {
  219. window.localStorage._inited.then(resolve);
  220. } else {
  221. resolve();
  222. }
  223. });
  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 createStore(reducer, getPersistedState(), middleware);
  306. }
  307. /**
  308. * Gets the default URL to be opened when this {@code App} mounts.
  309. *
  310. * @protected
  311. * @returns {string} The default URL to be opened when this {@code App}
  312. * mounts.
  313. */
  314. _getDefaultURL() {
  315. // If the execution environment provides a Location abstraction, then
  316. // this App at already at that location but it must be made aware of the
  317. // fact.
  318. const windowLocation = this.getWindowLocation();
  319. if (windowLocation) {
  320. const href = windowLocation.toString();
  321. if (href) {
  322. return href;
  323. }
  324. }
  325. const profileDefaultURL = getProfile(
  326. this._getStore().getState()
  327. ).defaultURL;
  328. return this.props.defaultURL || profileDefaultURL || DEFAULT_URL;
  329. }
  330. /**
  331. * Gets the redux store used by this {@code AbstractApp}.
  332. *
  333. * @protected
  334. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  335. */
  336. _getStore() {
  337. let store = this.state.store;
  338. if (typeof store === 'undefined') {
  339. store = this.props.store;
  340. }
  341. return store;
  342. }
  343. /**
  344. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  345. * store is not defined by the consumer of this {@code AbstractApp} through
  346. * its read-only React {@code Component} props.
  347. *
  348. * @param {Object} props - The read-only React {@code Component} props that
  349. * will eventually be received by this {@code AbstractApp}.
  350. * @private
  351. * @returns {Store} - The redux store to be used by this
  352. * {@code AbstractApp}.
  353. */
  354. _maybeCreateStore(props) {
  355. // The application Jitsi Meet is architected with redux. However, I do
  356. // not want consumers of the App React Component to be forced into
  357. // dealing with redux. If the consumer did not provide an external redux
  358. // store, utilize an internal redux store.
  359. let store = props.store;
  360. if (typeof store === 'undefined') {
  361. store = this._createStore();
  362. // This is temporary workaround to be able to dispatch actions from
  363. // non-reactified parts of the code (conference.js for example).
  364. // Don't use in the react code!!!
  365. // FIXME: remove when the reactification is finished!
  366. if (typeof APP !== 'undefined') {
  367. APP.store = store;
  368. }
  369. }
  370. return store;
  371. }
  372. /**
  373. * Navigates to a specific Route.
  374. *
  375. * @param {Route} route - The Route to which to navigate.
  376. * @returns {Promise}
  377. */
  378. _navigate(route) {
  379. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  380. return Promise.resolve();
  381. }
  382. let nextState = {
  383. route
  384. };
  385. // The Web App was using react-router so it utilized react-router's
  386. // onEnter. During the removal of react-router, modifications were
  387. // minimized by preserving the onEnter interface:
  388. // (1) Router would provide its nextState to the Route's onEnter. As the
  389. // role of Router is now this AbstractApp and we use redux, provide the
  390. // redux store instead.
  391. // (2) A replace function would be provided to the Route in case it
  392. // chose to redirect to another path.
  393. route && this._onRouteEnter(route, this._getStore(), pathname => {
  394. if (pathname) {
  395. this._openURL(pathname);
  396. // Do not proceed with the route because it chose to redirect to
  397. // another path.
  398. nextState = undefined;
  399. } else {
  400. nextState.route = undefined;
  401. }
  402. });
  403. // XXX React's setState is asynchronous which means that the value of
  404. // this.state.route above may not even be correct. If the check is
  405. // performed before setState completes, the app may not navigate to the
  406. // expected route. In order to mitigate the problem, _navigate was
  407. // changed to return a Promise.
  408. return new Promise(resolve => {
  409. if (nextState) {
  410. this.setState(nextState, resolve);
  411. } else {
  412. resolve();
  413. }
  414. });
  415. }
  416. /**
  417. * Notifies this {@code App} that a specific Route is about to be rendered.
  418. *
  419. * @param {Route} route - The Route that is about to be rendered.
  420. * @private
  421. * @returns {void}
  422. */
  423. _onRouteEnter(route, ...args) {
  424. // Notify the route that it is about to be entered.
  425. const { onEnter } = route;
  426. typeof onEnter === 'function' && onEnter(...args);
  427. }
  428. /**
  429. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  430. *
  431. * @param {string|Object} url - The URL to navigate this {@code AbstractApp}
  432. * to (i.e. the URL to open).
  433. * @protected
  434. * @returns {void}
  435. */
  436. _openURL(url) {
  437. this._getStore().dispatch(appNavigate(toURLString(url)));
  438. }
  439. }