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.

getRouteToRender.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // @flow
  2. import { generateRoomWithoutSeparator } from '@jitsi/js-utils/random';
  3. import type { Component } from 'react';
  4. import { isRoomValid } from '../base/conference';
  5. import { isSupportedBrowser } from '../base/environment';
  6. import { toState } from '../base/redux';
  7. import { Conference } from '../conference';
  8. import { getDeepLinkingPage } from '../deep-linking';
  9. import { UnsupportedDesktopBrowser } from '../unsupported-browser';
  10. import {
  11. BlankPage,
  12. WelcomePage,
  13. isWelcomePageAppEnabled,
  14. isWelcomePageUserEnabled
  15. } from '../welcome';
  16. /**
  17. * Object describing application route.
  18. *
  19. * @typedef {Object} Route
  20. * @property {Component} component - React Component constructor.
  21. * @property {string|undefined} href - New location, in case navigation involves
  22. * a location change.
  23. */
  24. export type Route = {
  25. component: Class<Component<*>>,
  26. href: ?string
  27. };
  28. /**
  29. * Determines which route is to be rendered in order to depict a specific Redux
  30. * store.
  31. *
  32. * @param {(Function|Object)} stateful - THe redux store, state, or
  33. * {@code getState} function.
  34. * @returns {Promise<Route>}
  35. */
  36. export function _getRouteToRender(stateful: Function | Object): Promise<Route> {
  37. const state = toState(stateful);
  38. if (navigator.product === 'ReactNative') {
  39. return _getMobileRoute(state);
  40. }
  41. return _getWebConferenceRoute(state) || _getWebWelcomePageRoute(state);
  42. }
  43. /**
  44. * Returns the {@code Route} to display on the React Native app.
  45. *
  46. * @param {Object} state - The redux state.
  47. * @returns {Promise<Route>}
  48. */
  49. function _getMobileRoute(state): Promise<Route> {
  50. const route = _getEmptyRoute();
  51. if (isRoomValid(state['features/base/conference'].room)) {
  52. route.component = Conference;
  53. } else if (isWelcomePageAppEnabled(state)) {
  54. route.component = WelcomePage;
  55. } else {
  56. route.component = BlankPage;
  57. }
  58. return Promise.resolve(route);
  59. }
  60. /**
  61. * Returns the {@code Route} to display when trying to access a conference if
  62. * a valid conference is being joined.
  63. *
  64. * @param {Object} state - The redux state.
  65. * @returns {Promise<Route>|undefined}
  66. */
  67. function _getWebConferenceRoute(state): ?Promise<Route> {
  68. if (!isRoomValid(state['features/base/conference'].room)) {
  69. return;
  70. }
  71. const route = _getEmptyRoute();
  72. // Update the location if it doesn't match. This happens when a room is
  73. // joined from the welcome page. The reason for doing this instead of using
  74. // the history API is that we want to load the config.js which takes the
  75. // room into account.
  76. const { locationURL } = state['features/base/connection'];
  77. if (window.location.href !== locationURL.href) {
  78. route.href = locationURL.href;
  79. return Promise.resolve(route);
  80. }
  81. return getDeepLinkingPage(state)
  82. .then(deepLinkComponent => {
  83. if (deepLinkComponent) {
  84. route.component = deepLinkComponent;
  85. } else if (isSupportedBrowser()) {
  86. route.component = Conference;
  87. } else {
  88. route.component = UnsupportedDesktopBrowser;
  89. }
  90. return route;
  91. });
  92. }
  93. /**
  94. * Returns the {@code Route} to display when trying to access the welcome page.
  95. *
  96. * @param {Object} state - The redux state.
  97. * @returns {Promise<Route>}
  98. */
  99. function _getWebWelcomePageRoute(state): Promise<Route> {
  100. const route = _getEmptyRoute();
  101. if (isWelcomePageUserEnabled(state)) {
  102. if (isSupportedBrowser()) {
  103. route.component = WelcomePage;
  104. } else {
  105. route.component = UnsupportedDesktopBrowser;
  106. }
  107. } else {
  108. // Web: if the welcome page is disabled, go directly to a random room.
  109. let href = window.location.href;
  110. href.endsWith('/') || (href += '/');
  111. route.href = href + generateRoomWithoutSeparator();
  112. }
  113. return Promise.resolve(route);
  114. }
  115. /**
  116. * Returns the default {@code Route}.
  117. *
  118. * @returns {Route}
  119. */
  120. function _getEmptyRoute(): Route {
  121. return {
  122. component: BlankPage,
  123. href: undefined
  124. };
  125. }