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

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