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

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