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

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