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.

actions.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { setRoom } from '../base/conference';
  2. import { getDomain, setDomain } from '../base/connection';
  3. import { loadConfig, setConfig } from '../base/lib-jitsi-meet';
  4. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
  5. import {
  6. _getRouteToRender,
  7. _parseURIString,
  8. init
  9. } from './functions';
  10. /**
  11. * Temporary solution. Should dispatch actions related to initial settings of
  12. * the app like setting log levels, reading the config parameters from query
  13. * string etc.
  14. *
  15. * @returns {Function}
  16. */
  17. export function appInit() {
  18. return () => init();
  19. }
  20. /**
  21. * Triggers an in-app navigation to a different route. Allows navigation to be
  22. * abstracted between the mobile and web versions.
  23. *
  24. * @param {(string|undefined)} uri - The URI to which to navigate. It may be a
  25. * full URL with an http(s) scheme, a full or partial URI with the app-specific
  26. * sheme, or a mere room name.
  27. * @returns {Function}
  28. */
  29. export function appNavigate(uri) {
  30. return (dispatch, getState) => {
  31. const state = getState();
  32. const oldDomain = getDomain(state);
  33. // eslint-disable-next-line prefer-const
  34. let { domain, room } = _parseURIString(uri);
  35. // If the specified URI does not identify a domain, use the app's
  36. // default.
  37. if (typeof domain === 'undefined') {
  38. domain
  39. = _parseURIString(state['features/app'].app._getDefaultURL())
  40. .domain;
  41. }
  42. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  43. // currently in a conference and ask her if she wants to close the
  44. // current conference and start a new one with the new room name or
  45. // domain.
  46. if (typeof domain === 'undefined' || oldDomain === domain) {
  47. dispatchSetRoomAndNavigate();
  48. } else if (oldDomain !== domain) {
  49. // Update domain without waiting for config to be loaded to prevent
  50. // race conditions when we will start to load config multiple times.
  51. dispatch(setDomain(domain));
  52. // If domain has changed, we need to load the config of the new
  53. // domain and set it, and only after that we can navigate to a
  54. // different route.
  55. loadConfig(`https://${domain}`)
  56. .then(
  57. config => configLoaded(/* err */ undefined, config),
  58. err => configLoaded(err, /* config */ undefined))
  59. .then(dispatchSetRoomAndNavigate);
  60. }
  61. /**
  62. * Notifies that an attempt to load the config(uration) of domain has
  63. * completed.
  64. *
  65. * @param {string|undefined} err - If the loading has failed, the error
  66. * detailing the cause of the failure.
  67. * @param {Object|undefined} config - If the loading has succeeded, the
  68. * loaded config(uration).
  69. * @returns {void}
  70. */
  71. function configLoaded(err, config) {
  72. if (err) {
  73. // XXX The failure could be, for example, because of a
  74. // certificate-related error. In which case the connection will
  75. // fail later in Strophe anyway even if we use the default
  76. // config here.
  77. // The function loadConfig will log the err.
  78. return;
  79. }
  80. dispatch(setConfig(config));
  81. }
  82. /**
  83. * Dispatches _setRoomAndNavigate in the Redux store.
  84. *
  85. * @returns {void}
  86. */
  87. function dispatchSetRoomAndNavigate() {
  88. // If both domain and room vars became undefined, that means we're
  89. // actually dealing with just room name and not with URL.
  90. dispatch(
  91. _setRoomAndNavigate(
  92. typeof room === 'undefined' && typeof domain === 'undefined'
  93. ? uri
  94. : room));
  95. }
  96. };
  97. }
  98. /**
  99. * Signals that a specific App will mount (in the terms of React).
  100. *
  101. * @param {App} app - The App which will mount.
  102. * @returns {{
  103. * type: APP_WILL_MOUNT,
  104. * app: App
  105. * }}
  106. */
  107. export function appWillMount(app) {
  108. return {
  109. type: APP_WILL_MOUNT,
  110. app
  111. };
  112. }
  113. /**
  114. * Signals that a specific App will unmount (in the terms of React).
  115. *
  116. * @param {App} app - The App which will unmount.
  117. * @returns {{
  118. * type: APP_WILL_UNMOUNT,
  119. * app: App
  120. * }}
  121. */
  122. export function appWillUnmount(app) {
  123. return {
  124. type: APP_WILL_UNMOUNT,
  125. app
  126. };
  127. }
  128. /**
  129. * Navigates to a route in accord with a specific Redux state.
  130. *
  131. * @param {Object} state - The Redux state which determines/identifies the route
  132. * to navigate to.
  133. * @private
  134. * @returns {void}
  135. */
  136. function _navigate(state) {
  137. const app = state['features/app'].app;
  138. const routeToRender = _getRouteToRender(state);
  139. app._navigate(routeToRender);
  140. }
  141. /**
  142. * Sets room and navigates to new route if needed.
  143. *
  144. * @param {string} newRoom - New room name.
  145. * @private
  146. * @returns {Function}
  147. */
  148. function _setRoomAndNavigate(newRoom) {
  149. return (dispatch, getState) => {
  150. dispatch(setRoom(newRoom));
  151. _navigate(getState());
  152. };
  153. }