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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. * @private
  8. * @returns {ReactElement|void}
  9. * @type {Function[]}
  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. * @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then
  19. * we should intercept existing component by UnsupportedMobileBrowser.
  20. */
  21. () => {
  22. const OS = Platform.OS;
  23. if (OS === 'android' || OS === 'ios') {
  24. return UnsupportedMobileBrowser;
  25. }
  26. }
  27. ];
  28. /**
  29. * Utility method that responsible for intercepting of route components based on
  30. * the set of defined rules.
  31. *
  32. * @param {Object|Function} stateOrGetState - Either Redux state object or
  33. * getState() function.
  34. * @param {ReactElement} currentComponent - Current route component to render.
  35. * @returns {ReactElement} If any of rules is satisfied returns intercepted
  36. * component.
  37. */
  38. export function interceptComponent(stateOrGetState, currentComponent) {
  39. let result;
  40. const state
  41. = typeof stateOrGetState === 'function'
  42. ? stateOrGetState()
  43. : stateOrGetState;
  44. for (const rule of _RULES) {
  45. result = rule(state);
  46. if (result) {
  47. break;
  48. }
  49. }
  50. return result || currentComponent;
  51. }