Browse Source

[RN] Refactor getting the default URL

Move it away from AbstractApp into an auxiliary function. In addition, introduce
a new `getServerURL` function which gets the configured server URL and defaults
to meet.jit.si as before.
master
Saúl Ibarra Corretgé 7 years ago
parent
commit
3bfab7718f

+ 2
- 2
react/features/app/actions.js View File

@@ -16,6 +16,7 @@ import { parseURIString, toURLString } from '../base/util';
16 16
 import { setFatalError } from '../overlay';
17 17
 
18 18
 import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
19
+import { getDefaultURL } from './functions';
19 20
 
20 21
 const logger = require('jitsi-meet-logger').getLogger(__filename);
21 22
 
@@ -116,8 +117,7 @@ function _appNavigateToOptionalLocation(
116 117
     // If the specified location (URI) does not identify a host, use the app's
117 118
     // default.
118 119
     if (!location || !location.host) {
119
-        const defaultLocation
120
-            = parseURIString(getState()['features/app'].app._getDefaultURL());
120
+        const defaultLocation = parseURIString(getDefaultURL(getState));
121 121
 
122 122
         if (location) {
123 123
             location.host = defaultLocation.host;

+ 2
- 27
react/features/app/components/AbstractApp.js View File

@@ -20,15 +20,7 @@ import { toURLString } from '../../base/util';
20 20
 import { OverlayContainer } from '../../overlay';
21 21
 
22 22
 import { appNavigate, appWillMount, appWillUnmount } from '../actions';
23
-
24
-/**
25
- * The default URL to open if no other was specified to {@code AbstractApp} via
26
- * props.
27
- *
28
- * FIXME: This is not at the best place here. This should be either in the
29
- * base/settings feature or a default in base/config.
30
- */
31
-const DEFAULT_URL = 'https://meet.jit.si';
23
+import { getDefaultURL } from '../functions';
32 24
 
33 25
 /**
34 26
  * Base (abstract) class for main App component.
@@ -322,24 +314,7 @@ export class AbstractApp extends Component {
322 314
      * mounts.
323 315
      */
324 316
     _getDefaultURL() {
325
-        // If the execution environment provides a Location abstraction, then
326
-        // this App at already at that location but it must be made aware of the
327
-        // fact.
328
-        const windowLocation = this.getWindowLocation();
329
-
330
-        if (windowLocation) {
331
-            const href = windowLocation.toString();
332
-
333
-            if (href) {
334
-                return href;
335
-            }
336
-        }
337
-
338
-        return (
339
-            this.props.defaultURL
340
-                || this.state.store.getState()['features/base/settings']
341
-                    .serverURL
342
-                || DEFAULT_URL);
317
+        return getDefaultURL(this.state.store);
343 318
     }
344 319
 
345 320
     /**

+ 28
- 0
react/features/app/functions.any.js View File

@@ -1,6 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import { toState } from '../base/redux';
4
+import { getServerURL } from '../base/settings';
4 5
 
5 6
 /**
6 7
  * Gets the value of a specific React {@code Component} prop of the currently
@@ -26,3 +27,30 @@ export function getAppProp(stateful: Function | Object, propName: string) {
26 27
 
27 28
     return undefined;
28 29
 }
30
+
31
+/**
32
+ * Retrieves the default URL for the app. This can either come from a prop to
33
+ * the root App component or be configured in the settings.
34
+ *
35
+ * @param {Function|Object} stateful - The redux store or {@code getState}
36
+ * function.
37
+ * @returns {string} - Default URL for the app.
38
+ */
39
+export function getDefaultURL(stateful: Function | Object) {
40
+    const state = toState(stateful);
41
+    const { app } = state['features/app'];
42
+
43
+    // If the execution environment provides a Location abstraction (e.g. a Web
44
+    // browser), then we'll presume it's the one and only base URL it can be on.
45
+    const windowLocation = app.getWindowLocation();
46
+
47
+    if (windowLocation) {
48
+        const href = windowLocation.toString();
49
+
50
+        if (href) {
51
+            return href;
52
+        }
53
+    }
54
+
55
+    return getAppProp(state, 'defaultURL') || getServerURL(state);
56
+}

+ 3
- 3
react/features/base/known-domains/middleware.js View File

@@ -1,6 +1,7 @@
1 1
 // @flow
2 2
 
3
-import { APP_WILL_MOUNT } from '../../app';
3
+import { APP_WILL_MOUNT, getDefaultURL } from '../../app';
4
+
4 5
 import { SET_ROOM } from '../conference';
5 6
 import { MiddlewareRegistry } from '../redux';
6 7
 import { parseURIString } from '../util';
@@ -32,8 +33,7 @@ MiddlewareRegistry.register(store => next => action => {
32 33
  * @returns {Promise}
33 34
  */
34 35
 function _appWillMount({ dispatch, getState }) {
35
-    const defaultURL
36
-        = parseURIString(getState()['features/app'].app._getDefaultURL());
36
+    const defaultURL = parseURIString(getDefaultURL(getState));
37 37
 
38 38
     dispatch(addKnownDomains(defaultURL.host));
39 39
 }

+ 6
- 0
react/features/base/settings/constants.js View File

@@ -0,0 +1,6 @@
1
+// @flow
2
+
3
+/**
4
+ * The default server URL to open if no other was specified.
5
+ */
6
+export const DEFAULT_SERVER_URL = 'https://meet.jit.si';

+ 14
- 0
react/features/base/settings/functions.js View File

@@ -3,6 +3,7 @@
3 3
 import { parseURLParams } from '../config';
4 4
 import { toState } from '../redux';
5 5
 
6
+import { DEFAULT_SERVER_URL } from './constants';
6 7
 
7 8
 /**
8 9
  * Returns the effective value of a configuration/preference/setting by applying
@@ -83,3 +84,16 @@ export function getPropertyValue(
83 84
 
84 85
     return undefined;
85 86
 }
87
+
88
+/**
89
+ * Gets the currently configured server URL.
90
+ *
91
+ * @param {Object|Function} stateful - The redux state object or
92
+ * {@code getState} function.
93
+ * @returns {string} - The currently configured server URL.
94
+ */
95
+export function getServerURL(stateful: Object | Function) {
96
+    const state = toState(stateful);
97
+
98
+    return state['features/base/settings'].serverURL || DEFAULT_SERVER_URL;
99
+}

+ 1
- 0
react/features/base/settings/index.js View File

@@ -1,4 +1,5 @@
1 1
 export * from './actions';
2
+export * from './constants';
2 3
 export * from './functions';
3 4
 
4 5
 import './middleware';

+ 2
- 2
react/features/recent-list/components/RecentList.native.js View File

@@ -2,7 +2,7 @@
2 2
 import React, { Component } from 'react';
3 3
 import { connect } from 'react-redux';
4 4
 
5
-import { appNavigate } from '../../app';
5
+import { appNavigate, getDefaultURL } from '../../app';
6 6
 import {
7 7
     getLocalizedDateFormatter,
8 8
     getLocalizedDurationFormatter,
@@ -226,7 +226,7 @@ class RecentList extends Component<Props> {
226 226
  */
227 227
 export function _mapStateToProps(state: Object) {
228 228
     return {
229
-        _defaultServerURL: state['features/app'].app._getDefaultURL(),
229
+        _defaultServerURL: getDefaultURL(state),
230 230
         _recentList: state['features/recent-list']
231 231
     };
232 232
 }

+ 2
- 1
react/features/settings/components/AbstractSettingsView.js View File

@@ -2,6 +2,7 @@
2 2
 
3 3
 import { Component } from 'react';
4 4
 
5
+import { getDefaultURL } from '../../app';
5 6
 import { updateSettings } from '../../base/settings';
6 7
 
7 8
 /**
@@ -173,7 +174,7 @@ export class AbstractSettingsView extends Component<Props> {
173 174
  */
174 175
 export function _mapStateToProps(state: Object) {
175 176
     return {
176
-        _serverURL: state['features/app'].app._getDefaultURL(),
177
+        _serverURL: getDefaultURL(state),
177 178
         _settings: state['features/base/settings'],
178 179
         _visible: state['features/settings'].visible
179 180
     };

Loading…
Cancel
Save