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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. }
  124. /**
  125. * Dispose lib-jitsi-meet and remove local participant when component is
  126. * going to be unmounted.
  127. *
  128. * @inheritdoc
  129. */
  130. componentWillUnmount() {
  131. const dispatch = this._getStore().dispatch;
  132. dispatch(localParticipantLeft());
  133. dispatch(appWillUnmount(this));
  134. }
  135. /**
  136. * Gets a Location object from the window with information about the current
  137. * location of the document. Explicitly defined to allow extenders to
  138. * override because React Native does not usually have a location property
  139. * on its window unless debugging remotely in which case the browser that is
  140. * the remote debugger will provide a location property on the window.
  141. *
  142. * @public
  143. * @returns {Location} A Location object with information about the current
  144. * location of the document.
  145. */
  146. getWindowLocation() {
  147. return undefined;
  148. }
  149. /**
  150. * Implements React's {@link Component#render()}.
  151. *
  152. * @inheritdoc
  153. * @returns {ReactElement}
  154. */
  155. render() {
  156. const route = this.state.route;
  157. if (route) {
  158. return (
  159. <I18nextProvider i18n = { i18next }>
  160. <Provider store = { this._getStore() }>
  161. {
  162. this._createElement(route.component)
  163. }
  164. </Provider>
  165. </I18nextProvider>
  166. );
  167. }
  168. return null;
  169. }
  170. /**
  171. * Create a ReactElement from the specified component, the specified props
  172. * and the props of this AbstractApp which are suitable for propagation to
  173. * the children of this Component.
  174. *
  175. * @param {Component} component - The component from which the ReactElement
  176. * is to be created.
  177. * @param {Object} props - The read-only React Component props with which
  178. * the ReactElement is to be initialized.
  179. * @returns {ReactElement}
  180. * @protected
  181. */
  182. _createElement(component, props) {
  183. /* eslint-disable no-unused-vars */
  184. const {
  185. // Don't propagate the dispatch and store props because they usually
  186. // come from react-redux and programmers don't really expect them to
  187. // be inherited but rather explicitly connected.
  188. dispatch, // eslint-disable-line react/prop-types
  189. store,
  190. // The following props were introduced to be consumed entirely by
  191. // AbstractApp:
  192. defaultURL,
  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 */
  199. return React.createElement(component, {
  200. ...thisProps,
  201. ...props
  202. });
  203. }
  204. /**
  205. * Initializes a new Redux store instance suitable for use by
  206. * this AbstractApp.
  207. *
  208. * @private
  209. * @returns {Store} - A new Redux store instance suitable for use by
  210. * this AbstractApp.
  211. */
  212. _createStore() {
  213. // Create combined reducer from all reducers in ReducerRegistry.
  214. const reducer = ReducerRegistry.combineReducers();
  215. // Apply all registered middleware from the MiddlewareRegistry and
  216. // additional 3rd party middleware:
  217. // - Thunk - allows us to dispatch async actions easily. For more info
  218. // @see https://github.com/gaearon/redux-thunk.
  219. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  220. // Try to enable Redux DevTools Chrome extension in order to make it
  221. // available for the purposes of facilitating development.
  222. let devToolsExtension;
  223. if (typeof window === 'object'
  224. && (devToolsExtension = window.devToolsExtension)) {
  225. middleware = compose(middleware, devToolsExtension());
  226. }
  227. return createStore(reducer, middleware);
  228. }
  229. /**
  230. * Gets the default URL to be opened when this App mounts.
  231. *
  232. * @protected
  233. * @returns {string} The default URL to be opened when this App mounts.
  234. */
  235. _getDefaultURL() {
  236. // If the execution environment provides a Location abstraction, then
  237. // this App at already at that location but it must be made aware of the
  238. // fact.
  239. const windowLocation = this.getWindowLocation();
  240. if (windowLocation) {
  241. const href = windowLocation.toString();
  242. if (href) {
  243. return href;
  244. }
  245. }
  246. return this.props.defaultURL || DEFAULT_URL;
  247. }
  248. /**
  249. * Gets the Redux store used by this AbstractApp.
  250. *
  251. * @protected
  252. * @returns {Store} - The Redux store used by this AbstractApp.
  253. */
  254. _getStore() {
  255. let store = this.state.store;
  256. if (typeof store === 'undefined') {
  257. store = this.props.store;
  258. }
  259. return store;
  260. }
  261. /**
  262. * Creates a Redux store to be used by this AbstractApp if such as store is
  263. * not defined by the consumer of this AbstractApp through its
  264. * read-only React Component props.
  265. *
  266. * @param {Object} props - The read-only React Component props that will
  267. * eventually be received by this AbstractApp.
  268. * @private
  269. * @returns {Store} - The Redux store to be used by this AbstractApp.
  270. */
  271. _maybeCreateStore(props) {
  272. // The application Jitsi Meet is architected with Redux. However, I do
  273. // not want consumers of the App React Component to be forced into
  274. // dealing with Redux. If the consumer did not provide an external Redux
  275. // store, utilize an internal Redux store.
  276. let store = props.store;
  277. if (typeof store === 'undefined') {
  278. store = this._createStore();
  279. // This is temporary workaround to be able to dispatch actions from
  280. // non-reactified parts of the code (conference.js for example).
  281. // Don't use in the react code!!!
  282. // FIXME: remove when the reactification is finished!
  283. if (typeof APP !== 'undefined') {
  284. APP.store = store;
  285. }
  286. }
  287. return store;
  288. }
  289. /**
  290. * Navigates to a specific Route.
  291. *
  292. * @param {Route} route - The Route to which to navigate.
  293. * @returns {void}
  294. */
  295. _navigate(route) {
  296. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  297. return;
  298. }
  299. let nextState = {
  300. ...this.state,
  301. route
  302. };
  303. // The Web App was using react-router so it utilized react-router's
  304. // onEnter. During the removal of react-router, modifications were
  305. // minimized by preserving the onEnter interface:
  306. // (1) Router would provide its nextState to the Route's onEnter. As the
  307. // role of Router is now this AbstractApp, provide its nextState.
  308. // (2) A replace function would be provided to the Route in case it
  309. // chose to redirect to another path.
  310. route && this._onRouteEnter(route, nextState, pathname => {
  311. this._openURL(pathname);
  312. // Do not proceed with the route because it chose to redirect to
  313. // another path.
  314. nextState = undefined;
  315. });
  316. nextState && this.setState(nextState);
  317. }
  318. /**
  319. * Notifies this App that a specific Route is about to be rendered.
  320. *
  321. * @param {Route} route - The Route that is about to be rendered.
  322. * @private
  323. * @returns {void}
  324. */
  325. _onRouteEnter(route, ...args) {
  326. // Notify the route that it is about to be entered.
  327. const { onEnter } = route;
  328. typeof onEnter === 'function' && onEnter(...args);
  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. }