-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathroutes.ts
77 lines (61 loc) · 2.38 KB
/
routes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import type {Location} from 'history';
import qs from 'qs';
import {compile} from 'path-to-regexp';
import isEmpty from 'lodash/isEmpty';
import {backend, clusterName, webVersion} from './store';
export const CLUSTER = 'cluster';
export const TENANT = 'tenant';
export const NODE = 'node';
export const TABLET = 'tablet';
const routes = {
cluster: `/${CLUSTER}/:activeTab?`,
tenant: `/${TENANT}`,
node: `/${NODE}/:id/:activeTab?`,
tablet: `/${TABLET}/:id`,
tabletsFilters: `/tabletsFilters`,
auth: `/auth`,
};
export const parseQuery = (location: Location) => {
return qs.parse(location.search, {
ignoreQueryPrefix: true,
});
};
const prepareRoute = (route: string) => {
let preparedRoute = route;
const portRegExp = /:\d{3, 5}/g;
const portMatch = route.match(portRegExp);
// if port exists in route we escape port to avoid errors in function compile()
// compile(preparedRoute) parses prepared root by symbol ":"
// if we pass raw route and there is a port in route, compile()
// will try to parse the port and throw an error
if (portMatch) {
const port = portMatch[0];
preparedRoute = route.replace(portRegExp, ':\\' + port.slice(1));
}
return preparedRoute;
};
export type Query = Record<string | number, string | number | string[] | number[] | undefined>;
export function createHref(
route: string,
params?: Record<string, string | number>,
query: Query = {},
) {
let extendedQuery = query;
const isBackendInQuery = Boolean(query.backend);
if (backend && !isBackendInQuery && webVersion) {
extendedQuery = {...query, backend};
}
const isClusterNameInQuery = Boolean(query.clusterName);
if (clusterName && !isClusterNameInQuery && webVersion) {
extendedQuery = {...extendedQuery, clusterName};
}
const search = isEmpty(extendedQuery) ? '' : `?${qs.stringify(extendedQuery, {encode: false})}`;
const preparedRoute = prepareRoute(route);
return `${compile(preparedRoute)(params)}${search}`;
}
// embedded version could be located in some folder (e.g. host/some_folder/app_router_path)
// window.location has the full pathname, while location from router ignores path to project
// this navigation assumes page reloading
export const createExternalUILink = (query = {}) =>
createHref(window.location.pathname, undefined, query);
export default routes;