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

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