/
helpers.ts
36 lines (28 loc) · 1.17 KB
/
helpers.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
import type { LocationDescriptor } from "history";
import { matchPath, RouteProps } from "react-router";
import { PageParam, PageSystemParamInit } from "./page-param";
import { clusterViewRoute, IClusterViewRouteParams } from "../components/cluster-manager/cluster-view.route";
import { navigation } from "./history";
export function navigate(location: LocationDescriptor) {
const currentLocation = navigation.location.pathname;
navigation.push(location);
if (currentLocation === navigation.location.pathname) {
navigation.goBack(); // prevent sequences of same url in history
}
}
export function createPageParam<V = string>(init: PageSystemParamInit<V>) {
return new PageParam<V>(init, navigation);
}
export function matchRoute<P>(route: string | string[] | RouteProps) {
return matchPath<P>(navigation.location.pathname, route);
}
export function isActiveRoute(route: string | string[] | RouteProps): boolean {
return !!matchRoute(route);
}
export function getMatchedClusterId(): string {
const matched = matchPath<IClusterViewRouteParams>(navigation.location.pathname, {
exact: true,
path: clusterViewRoute.path
});
return matched?.params.clusterId;
}