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

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