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

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