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

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