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

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