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

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