Skip to content

Commit 0905db8

Browse files
refactor(store): move getUrlData to a separate file
1 parent f9fc55a commit 0905db8

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

src/store/getUrlData.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import url from 'url';
2+
3+
export const getUrlData = (href: string, singleClusterMode: boolean) => {
4+
if (!singleClusterMode) {
5+
const {backend, clusterName} = url.parse(href, true).query;
6+
return {
7+
basename: '/',
8+
backend,
9+
clusterName,
10+
};
11+
} else if (window.custom_backend) {
12+
const {backend} = url.parse(href, true).query;
13+
return {
14+
basename: '/',
15+
backend: backend || window.custom_backend,
16+
};
17+
} else {
18+
const parsedPrefix = window.location.pathname.match(/.*(?=\/monitoring)/) || [];
19+
const basenamePrefix = Boolean(parsedPrefix.length) && parsedPrefix[0];
20+
const basename = [basenamePrefix, 'monitoring'].filter(Boolean).join('/');
21+
22+
return {
23+
basename,
24+
backend: basenamePrefix || '',
25+
};
26+
}
27+
};

src/store/index.js

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,16 @@
11
import {createStore, applyMiddleware, compose} from 'redux';
22
import thunkMiddleware from 'redux-thunk';
3-
import getLocationMiddleware from './state-url-mapping';
43
import {createBrowserHistory} from 'history';
54
import {listenForHistoryChange} from 'redux-location-state';
65

6+
import {getUrlData} from './getUrlData';
7+
import getLocationMiddleware from './state-url-mapping';
78
import rootReducer from './reducers';
89

9-
import url from 'url';
10-
11-
export const webVersion = window.web_version;
12-
13-
export const customBackend = window.custom_backend;
14-
15-
export const getUrlData = (href, singleClusterMode) => {
16-
if (!singleClusterMode) {
17-
const {backend, clusterName} = url.parse(href, true).query;
18-
return {
19-
basename: '/',
20-
backend,
21-
clusterName,
22-
};
23-
} else if (customBackend) {
24-
const {backend} = url.parse(href, true).query;
25-
return {
26-
basename: '/',
27-
backend: backend || window.custom_backend,
28-
};
29-
} else {
30-
const parsedPrefix = window.location.pathname.match(/.*(?=\/monitoring)/) || [];
31-
const basenamePrefix = Boolean(parsedPrefix.length) && parsedPrefix[0];
32-
const basename = [basenamePrefix, 'monitoring'].filter(Boolean).join('/');
33-
34-
return {
35-
basename,
36-
backend: basenamePrefix || '',
37-
};
38-
}
39-
};
10+
export let backend, basename, clusterName;
4011

4112
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
4213

43-
export let backend, basename, clusterName;
44-
4514
function _configureStore(aRootReducer, history, singleClusterMode) {
4615
const {locationMiddleware, reducersWithLocation} = getLocationMiddleware(history, aRootReducer);
4716
const middlewares = applyMiddleware(thunkMiddleware, locationMiddleware);
@@ -51,11 +20,16 @@ function _configureStore(aRootReducer, history, singleClusterMode) {
5120
});
5221
}
5322

54-
export default function configureStore(aRootReducer = rootReducer, singleClusterMode = true) {
23+
function configureStore(aRootReducer = rootReducer, singleClusterMode = true) {
5524
({backend, basename, clusterName} = getUrlData(window.location.href, singleClusterMode));
5625
const history = createBrowserHistory({basename});
5726

5827
const store = _configureStore(aRootReducer, history, singleClusterMode);
5928
listenForHistoryChange(store, history);
6029
return {history, store};
6130
}
31+
32+
export const webVersion = window.web_version;
33+
export const customBackend = window.custom_backend;
34+
35+
export default configureStore;

src/types/window.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ interface Window {
3030
Ya?: {
3131
Rum?: RumCounter;
3232
};
33+
34+
// eslint-disable-next-line
35+
web_version?: boolean;
36+
// eslint-disable-next-line
37+
custom_backend?: string;
38+
39+
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof import('redux').compose;
3340
}

0 commit comments

Comments
 (0)