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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import React, { Component } from 'react';
  2. import { Provider } from 'react-redux';
  3. import {
  4. localParticipantJoined,
  5. localParticipantLeft
  6. } from '../../base/participants';
  7. import { RouteRegistry } from '../../base/navigator';
  8. import {
  9. appNavigate,
  10. appWillMount,
  11. appWillUnmount
  12. } from '../actions';
  13. /**
  14. * Base (abstract) class for main App component.
  15. *
  16. * @abstract
  17. */
  18. export class AbstractApp extends Component {
  19. /**
  20. * AbstractApp component's property types.
  21. *
  22. * @static
  23. */
  24. static propTypes = {
  25. config: React.PropTypes.object,
  26. store: React.PropTypes.object,
  27. /**
  28. * The URL, if any, with which the app was launched.
  29. */
  30. url: React.PropTypes.string
  31. };
  32. /**
  33. * Initializes a new App instance.
  34. *
  35. * @param {Object} props - The read-only React Component props with which
  36. * the new instance is to be initialized.
  37. */
  38. constructor(props) {
  39. super(props);
  40. this.state = {
  41. /**
  42. * The Route rendered by this App.
  43. *
  44. * @type {Route}
  45. */
  46. route: undefined
  47. };
  48. }
  49. /**
  50. * Init lib-jitsi-meet and create local participant when component is going
  51. * to be mounted.
  52. *
  53. * @inheritdoc
  54. */
  55. componentWillMount() {
  56. const dispatch = this.props.store.dispatch;
  57. dispatch(appWillMount(this));
  58. dispatch(localParticipantJoined());
  59. this._openURL(this._getDefaultURL());
  60. }
  61. /**
  62. * Dispose lib-jitsi-meet and remove local participant when component is
  63. * going to be unmounted.
  64. *
  65. * @inheritdoc
  66. */
  67. componentWillUnmount() {
  68. const dispatch = this.props.store.dispatch;
  69. dispatch(localParticipantLeft());
  70. dispatch(appWillUnmount(this));
  71. }
  72. /**
  73. * Implements React's {@link Component#render()}.
  74. *
  75. * @inheritdoc
  76. * @returns {ReactElement}
  77. */
  78. render() {
  79. const route = this.state.route;
  80. if (route) {
  81. return (
  82. <Provider store = { this.props.store }>
  83. {
  84. this._createElement(route.component)
  85. }
  86. </Provider>
  87. );
  88. }
  89. return null;
  90. }
  91. /**
  92. * Create a ReactElement from the specified component, the specified props
  93. * and the props of this AbstractApp which are suitable for propagation to
  94. * the children of this Component.
  95. *
  96. * @param {Component} component - The component from which the ReactElement
  97. * is to be created.
  98. * @param {Object} props - The read-only React Component props with which
  99. * the ReactElement is to be initialized.
  100. * @returns {ReactElement}
  101. * @protected
  102. */
  103. _createElement(component, props) {
  104. /* eslint-disable no-unused-vars, lines-around-comment */
  105. const {
  106. // Don't propagate the config prop(erty) because the config is
  107. // stored inside the Redux state and, thus, is visible to the
  108. // children anyway.
  109. config,
  110. // Don't propagate the dispatch and store props because they usually
  111. // come from react-redux and programmers don't really expect them to
  112. // be inherited but rather explicitly connected.
  113. dispatch, // eslint-disable-line react/prop-types
  114. store,
  115. // The url property was introduced to be consumed entirely by
  116. // AbstractApp.
  117. url,
  118. // The remaining props, if any, are considered suitable for
  119. // propagation to the children of this Component.
  120. ...thisProps
  121. } = this.props;
  122. /* eslint-enable no-unused-vars, lines-around-comment */
  123. // eslint-disable-next-line object-property-newline
  124. return React.createElement(component, { ...thisProps, ...props });
  125. }
  126. /**
  127. * Gets the default URL to be opened when this App mounts.
  128. *
  129. * @private
  130. * @returns {string} The default URL to be opened when this App mounts.
  131. */
  132. _getDefaultURL() {
  133. // If the URL was explicitly specified to the React Component, then open
  134. // it.
  135. let url = this.props.url;
  136. if (url) {
  137. return url;
  138. }
  139. // If the execution environment provides a Location abstraction, then
  140. // this App at already at that location but it must be made aware of the
  141. // fact.
  142. const windowLocation = this._getWindowLocation();
  143. if (windowLocation) {
  144. url = windowLocation.toString();
  145. if (url) {
  146. return url;
  147. }
  148. }
  149. // By default, open the domain configured in the configuration file
  150. // which may be the domain at which the whole server infrastructure is
  151. // deployed.
  152. const config = this.props.config;
  153. if (typeof config === 'object') {
  154. const hosts = config.hosts;
  155. if (typeof hosts === 'object') {
  156. const domain = hosts.domain;
  157. if (domain) {
  158. return `https://${domain}`;
  159. }
  160. }
  161. }
  162. return 'https://meet.jit.si';
  163. }
  164. /**
  165. * Gets a Location object from the window with information about the current
  166. * location of the document. Explicitly defined to allow extenders to
  167. * override because React Native does not usually have a location property
  168. * on its window unless debugging remotely in which case the browser that is
  169. * the remote debugger will provide a location property on the window.
  170. *
  171. * @protected
  172. * @returns {Location} A Location object with information about the current
  173. * location of the document.
  174. */
  175. _getWindowLocation() {
  176. return undefined;
  177. }
  178. /**
  179. * Navigates to a specific Route.
  180. *
  181. * @param {Route} route - The Route to which to navigate.
  182. * @returns {void}
  183. */
  184. _navigate(route) {
  185. const currentRoute = this.state.route || {};
  186. if (!RouteRegistry.areRoutesEqual(route, currentRoute)) {
  187. let nextState = {
  188. ...this.state,
  189. route
  190. };
  191. // The Web App was using react-router so it utilized react-router's
  192. // onEnter. During the removal of react-router, modifications were
  193. // minimized by preserving the onEnter interface:
  194. // (1) Router would provide its nextState to the Route's onEnter.
  195. // As the role of Router is now this AbstractApp, provide its
  196. // nextState.
  197. // (2) A replace function would be provided to the Route in case it
  198. // chose to redirect to another path.
  199. this._onRouteEnter(route, nextState, pathname => {
  200. // FIXME In order to minimize the modifications related to the
  201. // removal of react-router, the Web implementation is provided
  202. // bellow because the replace function is used on Web only at
  203. // the time of this writing. Provide a platform-agnostic
  204. // implementation. It should likely find the best Route matching
  205. // the specified pathname and navigate to it.
  206. window.location.pathname = pathname;
  207. // Do not proceed with the route because it chose to redirect to
  208. // another path.
  209. nextState = undefined;
  210. });
  211. nextState && this.setState(nextState);
  212. }
  213. }
  214. /**
  215. * Notifies this App that a specific Route is about to be rendered.
  216. *
  217. * @param {Route} route - The Route that is about to be rendered.
  218. * @private
  219. * @returns {void}
  220. */
  221. _onRouteEnter(route, ...args) {
  222. // Notify the route that it is about to be entered.
  223. const onEnter = route.onEnter;
  224. if (typeof onEnter === 'function') {
  225. onEnter(...args);
  226. }
  227. }
  228. /**
  229. * Navigates this AbstractApp to (i.e. opens) a specific URL.
  230. *
  231. * @param {string} url - The URL to which to navigate this AbstractApp (i.e.
  232. * the URL to open).
  233. * @protected
  234. * @returns {void}
  235. */
  236. _openURL(url) {
  237. this.props.store.dispatch(appNavigate(url));
  238. }
  239. }