Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractApp.js 15KB

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