Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AbstractApp.js 13KB

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