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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 { appNavigate, appWillMount, appWillUnmount } from '../actions';
  16. declare var APP: Object;
  17. /**
  18. * The default URL to open if no other was specified to {@code AbstractApp}
  19. * via props.
  20. */
  21. const DEFAULT_URL = 'https://meet.jit.si';
  22. /**
  23. * Base (abstract) class for main App component.
  24. *
  25. * @abstract
  26. */
  27. export class AbstractApp extends Component {
  28. /**
  29. * {@code AbstractApp} component's property types.
  30. *
  31. * @static
  32. */
  33. static propTypes = {
  34. /**
  35. * The default URL {@code AbstractApp} is to open when not in any
  36. * conference/room.
  37. */
  38. defaultURL: PropTypes.string,
  39. /**
  40. * (Optional) redux store for this app.
  41. */
  42. store: PropTypes.object,
  43. // XXX Refer to the implementation of loadURLObject: in
  44. // ios/sdk/src/JitsiMeetView.m for further information.
  45. timestamp: PropTypes.any,
  46. /**
  47. * The URL, if any, with which the app was launched.
  48. */
  49. url: PropTypes.oneOfType([
  50. PropTypes.object,
  51. PropTypes.string
  52. ])
  53. };
  54. /**
  55. * Initializes a new {@code AbstractApp} instance.
  56. *
  57. * @param {Object} props - The read-only React {@code Component} props with
  58. * which the new instance is to be initialized.
  59. */
  60. constructor(props) {
  61. super(props);
  62. this.state = {
  63. /**
  64. * The Route rendered by this {@code AbstractApp}.
  65. *
  66. * @type {Route}
  67. */
  68. route: undefined,
  69. /**
  70. * The redux store used by this {@code 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 {@code Component} that it will receive new
  106. * props. Makes sure that this {@code AbstractApp} has a redux store to use.
  107. *
  108. * @inheritdoc
  109. * @param {Object} nextProps - The read-only React {@code Component} props
  110. * that this 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. // XXX Refer to the implementation of loadURLObject: in
  132. // ios/sdk/src/JitsiMeetView.m for further information.
  133. || this.props.timestamp !== nextProps.timestamp) {
  134. this._openURL(url || this._getDefaultURL());
  135. }
  136. }
  137. /**
  138. * Dispose lib-jitsi-meet and remove local participant when component is
  139. * going to be unmounted.
  140. *
  141. * @inheritdoc
  142. */
  143. componentWillUnmount() {
  144. const dispatch = this._getStore().dispatch;
  145. dispatch(localParticipantLeft());
  146. dispatch(appWillUnmount(this));
  147. }
  148. /**
  149. * Gets a {@code Location} object from the window with information about the
  150. * current location of the document. Explicitly defined to allow extenders
  151. * to override because React Native does not usually have a location
  152. * property on its window unless debugging remotely in which case the
  153. * browser that is the remote debugger will provide a location property on
  154. * the window.
  155. *
  156. * @public
  157. * @returns {Location} A {@code Location} object with information about the
  158. * current location of the document.
  159. */
  160. getWindowLocation() {
  161. return undefined;
  162. }
  163. /**
  164. * Implements React's {@link Component#render()}.
  165. *
  166. * @inheritdoc
  167. * @returns {ReactElement}
  168. */
  169. render() {
  170. const { route } = this.state;
  171. if (route) {
  172. return (
  173. <I18nextProvider i18n = { i18next }>
  174. <Provider store = { this._getStore() }>
  175. {
  176. this._createElement(route.component)
  177. }
  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, provide its nextState.
  323. // (2) A replace function would be provided to the Route in case it
  324. // chose to redirect to another path.
  325. route && this._onRouteEnter(route, nextState, pathname => {
  326. this._openURL(pathname);
  327. // Do not proceed with the route because it chose to redirect to
  328. // another path.
  329. nextState = undefined;
  330. });
  331. // XXX React's setState is asynchronous which means that the value of
  332. // this.state.route above may not even be correct. If the check is
  333. // performed before setState completes, the app may not navigate to the
  334. // expected route. In order to mitigate the problem, _navigate was
  335. // changed to return a Promise.
  336. return new Promise(resolve => {
  337. if (nextState) {
  338. this.setState(nextState, resolve);
  339. } else {
  340. resolve();
  341. }
  342. });
  343. }
  344. /**
  345. * Notifies this {@code App} that a specific Route is about to be rendered.
  346. *
  347. * @param {Route} route - The Route that is about to be rendered.
  348. * @private
  349. * @returns {void}
  350. */
  351. _onRouteEnter(route, ...args) {
  352. // Notify the route that it is about to be entered.
  353. const { onEnter } = route;
  354. typeof onEnter === 'function' && onEnter(...args);
  355. }
  356. /**
  357. * Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
  358. *
  359. * @param {string|Object} url - The URL to navigate this {@code AbstractApp}
  360. * to (i.e. the URL to open).
  361. * @protected
  362. * @returns {void}
  363. */
  364. _openURL(url) {
  365. this._getStore().dispatch(appNavigate(toURLString(url)));
  366. }
  367. }