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

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