Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

componentInterceptor.js 1.8KB

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