jiti-meet/react/features/base/util/interceptComponent.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-01-20 14:20:20 +00:00
import { Platform } from '../react';
2017-01-25 22:11:44 +00:00
import { UnsupportedMobileBrowser } from '../../unsupported-browser';
2017-01-20 14:20:20 +00:00
/**
* Array of rules defining whether we should intercept component to render
* or not.
*
* @type {Array<Function>}
* @param {Object} state - Redux state object.
* @returns {ReactElement|void}
*/
const RULES = [
/**
* This rule describes case when user opens application using mobile
* browser. In order to promote the app, we choose to suggest the mobile
* app even if the browser supports the app (e.g. Google Chrome with
* WebRTC support on Android).
*
2017-01-25 22:11:44 +00:00
* @returns {UnsupportedMobileBrowser|void} If the rule is satisfied then
* we should intercept existing component by UnsupportedMobileBrowser.
2017-01-20 14:20:20 +00:00
*/
() => {
2017-01-20 14:20:20 +00:00
const OS = Platform.OS;
if (OS === 'android' || OS === 'ios') {
2017-01-25 22:11:44 +00:00
return UnsupportedMobileBrowser;
2017-01-20 14:20:20 +00:00
}
}
];
/**
* Utility method that responsible for intercepting of route components based on
* the set of defined rules.
*
* @param {Object|Function} stateOrGetState - Either Redux state object or
* getState() function.
* @param {ReactElement} currentComponent - Current route component to render.
* @returns {ReactElement} If any of rules is satisfied returns intercepted
* component.
*/
export function interceptComponent(stateOrGetState, currentComponent) {
let result;
const state
= typeof stateOrGetState === 'function'
2017-01-25 22:11:44 +00:00
? stateOrGetState()
: stateOrGetState;
2017-01-20 14:20:20 +00:00
for (const rule of RULES) {
result = rule(state);
if (result) {
break;
}
}
return result || currentComponent;
}