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.

componentInterceptor.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Platform } from '../react';
  2. import {
  3. MobileBrowserPage,
  4. UnsupportedBrowserPage
  5. } from '../../unsupported-browser';
  6. /**
  7. * Array of rules defining whether we should intercept component to render
  8. * or not.
  9. *
  10. * @type {Array<Function>}
  11. * @param {Object} state - Redux state object.
  12. * @returns {ReactElement|void}
  13. */
  14. const RULES = [
  15. /**
  16. * This rule describes case when user opens application using mobile
  17. * browser. In order to promote the app, we choose to suggest the mobile
  18. * app even if the browser supports the app (e.g. Google Chrome with
  19. * WebRTC support on Android).
  20. *
  21. * @param {Object} state - Object containing Redux state.
  22. * @returns {MobileBrowserPage|void} If the rule is satisfied then
  23. * we should intercept existing component by MobileBrowserPage.
  24. */
  25. state => {
  26. const OS = Platform.OS;
  27. const { mobileBrowserPageIsShown }
  28. = state['features/unsupported-browser'];
  29. if ((OS === 'android' || OS === 'ios') && !mobileBrowserPageIsShown) {
  30. return MobileBrowserPage;
  31. }
  32. },
  33. /**
  34. * This rule describes case when user opens application using web browser
  35. * that doesn't support WebRTC or Temasys plugin should be installed.
  36. *
  37. * @returns {UnsupportedBrowserPage|void} If the rule is satisfied
  38. * then we should intercept existing component by UnsupportedBrowserPage.
  39. */
  40. () => {
  41. if (true) {
  42. return UnsupportedBrowserPage;
  43. }
  44. }
  45. ];
  46. /**
  47. * Utility method that responsible for intercepting of route components based on
  48. * the set of defined rules.
  49. *
  50. * @param {Object|Function} stateOrGetState - Either Redux state object or
  51. * getState() function.
  52. * @param {ReactElement} currentComponent - Current route component to render.
  53. * @returns {ReactElement} If any of rules is satisfied returns intercepted
  54. * component.
  55. */
  56. export function interceptComponent(stateOrGetState, currentComponent) {
  57. let result;
  58. const state
  59. = typeof stateOrGetState === 'function'
  60. ? stateOrGetState()
  61. : stateOrGetState;
  62. for (const rule of RULES) {
  63. result = rule(state);
  64. if (result) {
  65. break;
  66. }
  67. }
  68. return result || currentComponent;
  69. }