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. * Implements React's {@link Component#render()}.
  112. *
  113. * @inheritdoc
  114. * @returns {ReactElement}
  115. */
  116. render() {
  117. const route = this.state.route;
  118. if (route) {
  119. return (
  120. <I18nextProvider i18n = { i18next }>
  121. <Provider store = { this._getStore() }>
  122. {
  123. this._createElement(route.component)
  124. }
  125. </Provider>
  126. </I18nextProvider>
  127. );
  128. }
  129. return null;
  130. }
  131. /**
  132. * Create a ReactElement from the specified component, the specified props
  133. * and the props of this AbstractApp which are suitable for propagation to
  134. * the children of this Component.
  135. *
  136. * @param {Component} component - The component from which the ReactElement
  137. * is to be created.
  138. * @param {Object} props - The read-only React Component props with which
  139. * the ReactElement is to be initialized.
  140. * @returns {ReactElement}
  141. * @protected
  142. */
  143. _createElement(component, props) {
  144. /* eslint-disable no-unused-vars, lines-around-comment */
  145. const {
  146. // Don't propagate the config prop(erty) because the config is
  147. // stored inside the Redux state and, thus, is visible to the
  148. // children anyway.
  149. config,
  150. // Don't propagate the dispatch and store props because they usually
  151. // come from react-redux and programmers don't really expect them to
  152. // be inherited but rather explicitly connected.
  153. dispatch, // eslint-disable-line react/prop-types
  154. store,
  155. // The url property was introduced to be consumed entirely by
  156. // AbstractApp.
  157. url,
  158. // The remaining props, if any, are considered suitable for
  159. // propagation to the children of this Component.
  160. ...thisProps
  161. } = this.props;
  162. /* eslint-enable no-unused-vars, lines-around-comment */
  163. // eslint-disable-next-line object-property-newline
  164. return React.createElement(component, { ...thisProps, ...props });
  165. }
  166. /**
  167. * Initializes a new Redux store instance suitable for use by
  168. * this AbstractApp.
  169. *
  170. * @private
  171. * @returns {Store} - A new Redux store instance suitable for use by
  172. * this AbstractApp.
  173. */
  174. _createStore() {
  175. // Create combined reducer from all reducers in ReducerRegistry.
  176. const reducer = ReducerRegistry.combineReducers();
  177. // Apply all registered middleware from the MiddlewareRegistry and
  178. // additional 3rd party middleware:
  179. // - Thunk - allows us to dispatch async actions easily. For more info
  180. // @see https://github.com/gaearon/redux-thunk.
  181. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  182. // Try to enable Redux DevTools Chrome extension in order to make it
  183. // available for the purposes of facilitating development.
  184. let devToolsExtension;
  185. if (typeof window === 'object'
  186. && (devToolsExtension = window.devToolsExtension)) {
  187. middleware = compose(middleware, devToolsExtension());
  188. }
  189. return createStore(reducer, middleware);
  190. }
  191. /**
  192. * Gets the default URL to be opened when this App mounts.
  193. *
  194. * @protected
  195. * @returns {string} The default URL to be opened when this App mounts.
  196. */
  197. _getDefaultURL() {
  198. // If the execution environment provides a Location abstraction, then
  199. // this App at already at that location but it must be made aware of the
  200. // fact.
  201. const windowLocation = this._getWindowLocation();
  202. if (windowLocation) {
  203. const href = windowLocation.toString();
  204. if (href) {
  205. return href;
  206. }
  207. }
  208. // By default, open the domain configured in the configuration file
  209. // which may be the domain at which the whole server infrastructure is
  210. // deployed.
  211. const config = this.props.config;
  212. if (typeof config === 'object') {
  213. const hosts = config.hosts;
  214. if (typeof hosts === 'object') {
  215. const domain = hosts.domain;
  216. if (domain) {
  217. return `https://${domain}`;
  218. }
  219. }
  220. }
  221. return 'https://meet.jit.si';
  222. }
  223. /**
  224. * Gets the Redux store used by this AbstractApp.
  225. *
  226. * @protected
  227. * @returns {Store} - The Redux store used by this AbstractApp.
  228. */
  229. _getStore() {
  230. let store = this.state.store;
  231. if (typeof store === 'undefined') {
  232. store = this.props.store;
  233. }
  234. return store;
  235. }
  236. /**
  237. * Gets a Location object from the window with information about the current
  238. * location of the document. Explicitly defined to allow extenders to
  239. * override because React Native does not usually have a location property
  240. * on its window unless debugging remotely in which case the browser that is
  241. * the remote debugger will provide a location property on the window.
  242. *
  243. * @protected
  244. * @returns {Location} A Location object with information about the current
  245. * location of the document.
  246. */
  247. _getWindowLocation() {
  248. return undefined;
  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. }