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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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)} urlOrRoom - The URL or room name to which to
  25. * navigate.
  26. * @returns {Function}
  27. */
  28. export function appNavigate(urlOrRoom) {
  29. return (dispatch, getState) => {
  30. const state = getState();
  31. const oldDomain = getDomain(state);
  32. const { domain, room } = _parseURIString(urlOrRoom);
  33. // TODO Kostiantyn Tsaregradskyi: We should probably detect if user is
  34. // currently in a conference and ask her if she wants to close the
  35. // current conference and start a new one with the new room name or
  36. // domain.
  37. if (typeof domain === 'undefined' || oldDomain === domain) {
  38. dispatchSetRoomAndNavigate();
  39. } else if (oldDomain !== domain) {
  40. // Update domain without waiting for config to be loaded to prevent
  41. // race conditions when we will start to load config multiple times.
  42. dispatch(setDomain(domain));
  43. // If domain has changed, we need to load the config of the new
  44. // domain and set it, and only after that we can navigate to
  45. // different route.
  46. loadConfig(`https://${domain}`)
  47. .then(
  48. config => configLoaded(/* err */ undefined, config),
  49. err => configLoaded(err, /* config */ undefined))
  50. .then(dispatchSetRoomAndNavigate);
  51. }
  52. /**
  53. * Notifies that an attempt to load the config(uration) of domain has
  54. * completed.
  55. *
  56. * @param {string|undefined} err - If the loading has failed, the error
  57. * detailing the cause of the failure.
  58. * @param {Object|undefined} config - If the loading has succeeded, the
  59. * loaded config(uration).
  60. * @returns {void}
  61. */
  62. function configLoaded(err, config) {
  63. if (err) {
  64. // XXX The failure could be, for example, because of a
  65. // certificate-related error. In which case the connection will
  66. // fail later in Strophe anyway even if we use the default
  67. // config here.
  68. // The function loadConfig will log the err.
  69. return;
  70. }
  71. dispatch(setConfig(config));
  72. }
  73. /**
  74. * Dispatches _setRoomAndNavigate in the Redux store.
  75. *
  76. * @returns {void}
  77. */
  78. function dispatchSetRoomAndNavigate() {
  79. // If both domain and room vars became undefined, that means we're
  80. // actually dealing with just room name and not with URL.
  81. dispatch(
  82. _setRoomAndNavigate(
  83. typeof room === 'undefined' && typeof domain === 'undefined'
  84. ? urlOrRoom
  85. : room));
  86. }
  87. };
  88. }
  89. /**
  90. * Signals that a specific App will mount (in the terms of React).
  91. *
  92. * @param {App} app - The App which will mount.
  93. * @returns {{
  94. * type: APP_WILL_MOUNT,
  95. * app: App
  96. * }}
  97. */
  98. export function appWillMount(app) {
  99. return {
  100. type: APP_WILL_MOUNT,
  101. app
  102. };
  103. }
  104. /**
  105. * Signals that a specific App will unmount (in the terms of React).
  106. *
  107. * @param {App} app - The App which will unmount.
  108. * @returns {{
  109. * type: APP_WILL_UNMOUNT,
  110. * app: App
  111. * }}
  112. */
  113. export function appWillUnmount(app) {
  114. return {
  115. type: APP_WILL_UNMOUNT,
  116. app
  117. };
  118. }
  119. /**
  120. * Navigates to a route in accord with a specific Redux state.
  121. *
  122. * @param {Object} state - The Redux state which determines/identifies the route
  123. * to navigate to.
  124. * @private
  125. * @returns {void}
  126. */
  127. function _navigate(state) {
  128. const app = state['features/app'].app;
  129. const routeToRender = _getRouteToRender(state);
  130. app._navigate(routeToRender);
  131. }
  132. /**
  133. * Sets room and navigates to new route if needed.
  134. *
  135. * @param {string} newRoom - New room name.
  136. * @private
  137. * @returns {Function}
  138. */
  139. function _setRoomAndNavigate(newRoom) {
  140. return (dispatch, getState) => {
  141. dispatch(setRoom(newRoom));
  142. _navigate(getState());
  143. };
  144. }