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

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