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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. {
  178. this._createElement(component)
  179. }
  180. </Provider>
  181. </I18nextProvider>
  182. );
  183. }
  184. return null;
  185. }
  186. /**
  187. * Creates a {@link ReactElement} from the specified component, the
  188. * specified props and the props of this {@code AbstractApp} which are
  189. * suitable for propagation to the children of this {@code Component}.
  190. *
  191. * @param {Component} component - The component from which the
  192. * {@code ReactElement} is to be created.
  193. * @param {Object} props - The read-only React {@code Component} props with
  194. * which the {@code ReactElement} is to be initialized.
  195. * @returns {ReactElement}
  196. * @protected
  197. */
  198. _createElement(component, props) {
  199. /* eslint-disable no-unused-vars */
  200. const {
  201. // Don't propagate the dispatch and store props because they usually
  202. // come from react-redux and programmers don't really expect them to
  203. // be inherited but rather explicitly connected.
  204. dispatch, // eslint-disable-line react/prop-types
  205. store,
  206. // The following props were introduced to be consumed entirely by
  207. // AbstractApp:
  208. defaultURL,
  209. url,
  210. // The remaining props, if any, are considered suitable for
  211. // propagation to the children of this Component.
  212. ...thisProps
  213. } = this.props;
  214. /* eslint-enable no-unused-vars */
  215. return React.createElement(component, {
  216. ...thisProps,
  217. ...props
  218. });
  219. }
  220. /**
  221. * Initializes a new redux store instance suitable for use by this
  222. * {@code AbstractApp}.
  223. *
  224. * @private
  225. * @returns {Store} - A new redux store instance suitable for use by
  226. * this {@code AbstractApp}.
  227. */
  228. _createStore() {
  229. // Create combined reducer from all reducers in ReducerRegistry.
  230. const reducer = ReducerRegistry.combineReducers();
  231. // Apply all registered middleware from the MiddlewareRegistry and
  232. // additional 3rd party middleware:
  233. // - Thunk - allows us to dispatch async actions easily. For more info
  234. // @see https://github.com/gaearon/redux-thunk.
  235. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  236. // Try to enable Redux DevTools Chrome extension in order to make it
  237. // available for the purposes of facilitating development.
  238. let devToolsExtension;
  239. if (typeof window === 'object'
  240. && (devToolsExtension = window.devToolsExtension)) {
  241. middleware = compose(middleware, devToolsExtension());
  242. }
  243. return createStore(reducer, middleware);
  244. }
  245. /**
  246. * Gets the default URL to be opened when this {@code App} mounts.
  247. *
  248. * @protected
  249. * @returns {string} The default URL to be opened when this {@code App}
  250. * mounts.
  251. */
  252. _getDefaultURL() {
  253. // If the execution environment provides a Location abstraction, then
  254. // this App at already at that location but it must be made aware of the
  255. // fact.
  256. const windowLocation = this.getWindowLocation();
  257. if (windowLocation) {
  258. const href = windowLocation.toString();
  259. if (href) {
  260. return href;
  261. }
  262. }
  263. return this.props.defaultURL || DEFAULT_URL;
  264. }
  265. /**
  266. * Gets the redux store used by this {@code AbstractApp}.
  267. *
  268. * @protected
  269. * @returns {Store} - The redux store used by this {@code AbstractApp}.
  270. */
  271. _getStore() {
  272. let store = this.state.store;
  273. if (typeof store === 'undefined') {
  274. store = this.props.store;
  275. }
  276. return store;
  277. }
  278. /**
  279. * Creates a redux store to be used by this {@code AbstractApp} if such as a
  280. * store is not defined by the consumer of this {@code AbstractApp} through
  281. * its read-only React {@code Component} props.
  282. *
  283. * @param {Object} props - The read-only React {@code Component} props that
  284. * will eventually be received by this {@code AbstractApp}.
  285. * @private
  286. * @returns {Store} - The redux store to be used by this
  287. * {@code AbstractApp}.
  288. */
  289. _maybeCreateStore(props) {
  290. // The application Jitsi Meet is architected with redux. However, I do
  291. // not want consumers of the App React Component to be forced into
  292. // dealing with redux. If the consumer did not provide an external redux
  293. // store, utilize an internal redux store.
  294. let store = props.store;
  295. if (typeof store === 'undefined') {
  296. store = this._createStore();
  297. // This is temporary workaround to be able to dispatch actions from
  298. // non-reactified parts of the code (conference.js for example).
  299. // Don't use in the react code!!!
  300. // FIXME: remove when the reactification is finished!
  301. if (typeof APP !== 'undefined') {
  302. APP.store = store;
  303. }
  304. }
  305. return store;
  306. }
  307. /**
  308. * Navigates to a specific Route.
  309. *
  310. * @param {Route} route - The Route to which to navigate.
  311. * @returns {Promise}
  312. */
  313. _navigate(route) {
  314. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  315. return Promise.resolve();
  316. }
  317. let nextState = {
  318. route
  319. };
  320. // The Web App was using react-router so it utilized react-router's
  321. // onEnter. During the removal of react-router, modifications were
  322. // minimized by preserving the onEnter interface:
  323. // (1) Router would provide its nextState to the Route's onEnter. As the
  324. // role of Router is now this AbstractApp and we use redux, provide the
  325. // redux store instead.
  326. // (2) A replace function would be provided to the Route in case it
  327. // chose to redirect to another path.
  328. route && this._onRouteEnter(route, this._getStore(), pathname => {
  329. if (pathname) {
  330. this._openURL(pathname);
  331. // Do not proceed with the route because it chose to redirect to
  332. // another path.
  333. nextState = undefined;
  334. } else {
  335. nextState.route = undefined;
  336. }
  337. });
  338. // XXX React's setState is asynchronous which means that the value of
  339. // this.state.route above may not even be correct. If the check is
  340. // performed before setState completes, the app may not navigate to the
  341. // expected route. In order to mitigate the problem, _navigate was
  342. // changed to return a Promise.
  343. return new Promise(resolve => {
  344. if (nextState) {
  345. this.setState(nextState, resolve);
  346. } else {
  347. resolve();
  348. }
  349. });
  350. }
  351. /**
  352. * Notifies this {@code App} that a specific Route is about to be rendered.
  353. *
  354. * @param {Route} route - The Route that is about to be rendered.
  355. * @private
  356. * @returns {void}
  357. */
  358. _onRouteEnter(route, ...args) {
  359. // Notify the route that it is about to be entered.
  360. const { onEnter } = route;
  361. typeof onEnter === 'function' && onEnter(...args);
  362. }
  363. /**
  364. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  365. *
  366. * @param {string|Object} url - The URL to navigate this {@code AbstractApp}
  367. * to (i.e. the URL to open).
  368. * @protected
  369. * @returns {void}
  370. */
  371. _openURL(url) {
  372. this._getStore().dispatch(appNavigate(toURLString(url)));
  373. }
  374. }