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.

interceptComponent.js 1.8KB

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