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 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* global APP, interfaceConfig */
  2. import { Platform } from '../react';
  3. import {
  4. NoMobileApp,
  5. PluginRequiredBrowser,
  6. UnsupportedDesktopBrowser,
  7. UnsupportedMobileBrowser
  8. } from '../../unsupported-browser';
  9. /**
  10. * Array of rules defining whether we should intercept component to render
  11. * or not.
  12. *
  13. * @private
  14. * @returns {ReactElement|void}
  15. * @type {Function[]}
  16. */
  17. const _RULES = [
  18. /**
  19. * This rule describes case when user opens application using mobile
  20. * browser. In order to promote the app, we choose to suggest the mobile
  21. * app even if the browser supports the app (e.g. Google Chrome with
  22. * WebRTC support on Android).
  23. *
  24. * @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then
  25. * we should intercept existing component by UnsupportedMobileBrowser.
  26. */
  27. () => {
  28. const OS = Platform.OS;
  29. if (OS === 'android' || OS === 'ios') {
  30. return (
  31. interfaceConfig.MOBILE_APP_ENABLED
  32. ? UnsupportedMobileBrowser
  33. : NoMobileApp);
  34. }
  35. },
  36. () => {
  37. if (APP.unsupportedBrowser) {
  38. const { isOldBrowser } = APP.unsupportedBrowser;
  39. if (isOldBrowser) {
  40. return UnsupportedDesktopBrowser;
  41. }
  42. return PluginRequiredBrowser;
  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. }