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

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