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 13KB

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