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.5KB

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