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