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

AbstractApp.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. const currentProps = this.props;
  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 currentProps.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(currentProps.url) !== url
  177. // XXX Refer to the implementation of loadURLObject: in
  178. // ios/sdk/src/JitsiMeetView.m for further information.
  179. || currentProps.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 app start until the {@code Storage} implementation initialises.
  212. * This is instantaneous on web, but is async on mobile.
  213. *
  214. * @private
  215. * @returns {ReactElement}
  216. */
  217. _initStorage() {
  218. return new Promise(resolve => {
  219. if (window.localStorage._initializing) {
  220. window.localStorage._inited.then(resolve);
  221. } else {
  222. resolve();
  223. }
  224. });
  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. <OverlayContainer />
  242. </Fragment>
  243. </Provider>
  244. </I18nextProvider>
  245. );
  246. }
  247. return null;
  248. }
  249. /**
  250. * Creates a {@link ReactElement} from the specified component, the
  251. * specified props and the props of this {@code AbstractApp} which are
  252. * suitable for propagation to the children of this {@code Component}.
  253. *
  254. * @param {Component} component - The component from which the
  255. * {@code ReactElement} is to be created.
  256. * @param {Object} props - The read-only React {@code Component} props with
  257. * which the {@code ReactElement} is to be initialized.
  258. * @returns {ReactElement}
  259. * @protected
  260. */
  261. _createElement(component, props) {
  262. /* eslint-disable no-unused-vars */
  263. const {
  264. // Don't propagate the dispatch and store props because they usually
  265. // come from react-redux and programmers don't really expect them to
  266. // be inherited but rather explicitly connected.
  267. dispatch, // eslint-disable-line react/prop-types
  268. store,
  269. // The following props were introduced to be consumed entirely by
  270. // AbstractApp:
  271. defaultURL,
  272. url,
  273. // The remaining props, if any, are considered suitable for
  274. // propagation to the children of this Component.
  275. ...thisProps
  276. } = this.props;
  277. /* eslint-enable no-unused-vars */
  278. return React.createElement(component, {
  279. ...thisProps,
  280. ...props
  281. });
  282. }
  283. /**
  284. * Initializes a new redux store instance suitable for use by this
  285. * {@code AbstractApp}.
  286. *
  287. * @private
  288. * @returns {Store} - A new redux store instance suitable for use by
  289. * this {@code AbstractApp}.
  290. */
  291. _createStore() {
  292. // Create combined reducer from all reducers in ReducerRegistry.
  293. const reducer = ReducerRegistry.combineReducers();
  294. // Apply all registered middleware from the MiddlewareRegistry and
  295. // additional 3rd party middleware:
  296. // - Thunk - allows us to dispatch async actions easily. For more info
  297. // @see https://github.com/gaearon/redux-thunk.
  298. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  299. // Try to enable Redux DevTools Chrome extension in order to make it
  300. // available for the purposes of facilitating development.
  301. let devToolsExtension;
  302. if (typeof window === 'object'
  303. && (devToolsExtension = window.devToolsExtension)) {
  304. middleware = compose(middleware, devToolsExtension());
  305. }
  306. return createStore(reducer, getPersistedState(), middleware);
  307. }
  308. /**
  309. * Gets the default URL to be opened when this {@code App} mounts.
  310. *
  311. * @protected
  312. * @returns {string} The default URL to be opened when this {@code App}
  313. * mounts.
  314. */
  315. _getDefaultURL() {
  316. // If the execution environment provides a Location abstraction, then
  317. // this App at already at that location but it must be made aware of the
  318. // fact.
  319. const windowLocation = this.getWindowLocation();
  320. if (windowLocation) {
  321. const href = windowLocation.toString();
  322. if (href) {
  323. return href;
  324. }
  325. }
  326. const profileServerURL = getProfile(
  327. this._getStore().getState()
  328. ).serverURL;
  329. return this.props.defaultURL || profileServerURL || DEFAULT_URL;
  330. }
  331. /**
  332. * Gets the redux store used by this {@code AbstractApp}.
  333. *
  334. * @protected
  335. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  336. */
  337. _getStore() {
  338. let store = this.state.store;
  339. if (typeof store === 'undefined') {
  340. store = this.props.store;
  341. }
  342. return store;
  343. }
  344. /**
  345. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  346. * store is not defined by the consumer of this {@code AbstractApp} through
  347. * its read-only React {@code Component} props.
  348. *
  349. * @param {Object} props - The read-only React {@code Component} props that
  350. * will eventually be received by this {@code AbstractApp}.
  351. * @private
  352. * @returns {Store} - The redux store to be used by this
  353. * {@code AbstractApp}.
  354. */
  355. _maybeCreateStore(props) {
  356. // The application Jitsi Meet is architected with redux. However, I do
  357. // not want consumers of the App React Component to be forced into
  358. // dealing with redux. If the consumer did not provide an external redux
  359. // store, utilize an internal redux store.
  360. let store = props.store;
  361. if (typeof store === 'undefined') {
  362. store = this._createStore();
  363. // This is temporary workaround to be able to dispatch actions from
  364. // non-reactified parts of the code (conference.js for example).
  365. // Don't use in the react code!!!
  366. // FIXME: remove when the reactification is finished!
  367. if (typeof APP !== 'undefined') {
  368. APP.store = store;
  369. }
  370. }
  371. return store;
  372. }
  373. /**
  374. * Navigates to a specific Route.
  375. *
  376. * @param {Route} route - The Route to which to navigate.
  377. * @returns {Promise}
  378. */
  379. _navigate(route) {
  380. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  381. return Promise.resolve();
  382. }
  383. let nextState = {
  384. route
  385. };
  386. // The Web App was using react-router so it utilized react-router's
  387. // onEnter. During the removal of react-router, modifications were
  388. // minimized by preserving the onEnter interface:
  389. // (1) Router would provide its nextState to the Route's onEnter. As the
  390. // role of Router is now this AbstractApp and we use redux, provide the
  391. // redux store instead.
  392. // (2) A replace function would be provided to the Route in case it
  393. // chose to redirect to another path.
  394. route && this._onRouteEnter(route, this._getStore(), pathname => {
  395. if (pathname) {
  396. this._openURL(pathname);
  397. // Do not proceed with the route because it chose to redirect to
  398. // another path.
  399. nextState = undefined;
  400. } else {
  401. nextState.route = undefined;
  402. }
  403. });
  404. // XXX React's setState is asynchronous which means that the value of
  405. // this.state.route above may not even be correct. If the check is
  406. // performed before setState completes, the app may not navigate to the
  407. // expected route. In order to mitigate the problem, _navigate was
  408. // changed to return a Promise.
  409. return new Promise(resolve => {
  410. if (nextState) {
  411. this.setState(nextState, resolve);
  412. } else {
  413. resolve();
  414. }
  415. });
  416. }
  417. /**
  418. * Notifies this {@code App} that a specific Route is about to be rendered.
  419. *
  420. * @param {Route} route - The Route that is about to be rendered.
  421. * @private
  422. * @returns {void}
  423. */
  424. _onRouteEnter(route, ...args) {
  425. // Notify the route that it is about to be entered.
  426. const { onEnter } = route;
  427. typeof onEnter === 'function' && onEnter(...args);
  428. }
  429. /**
  430. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  431. *
  432. * @param {string|Object} url - The URL to navigate this {@code AbstractApp}
  433. * to (i.e. the URL to open).
  434. * @protected
  435. * @returns {void}
  436. */
  437. _openURL(url) {
  438. this._getStore().dispatch(appNavigate(toURLString(url)));
  439. }
  440. }