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

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