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

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