12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import {PlainRoute} from 'react-router';
- import {Location} from 'history';
- import findLastIndex from 'lodash/findLastIndex';
- import replaceRouterParams from 'sentry/utils/replaceRouterParams';
- type Options = {
-
-
- params: {[key: string]: string | undefined};
- routes: PlainRoute[];
- location?: Location;
-
- stepBack?: -1 | -2 | -3 | -4 | -5 | -6 | -7 | -8 | -9;
- };
- export default function recreateRoute(to: string | PlainRoute, options: Options): string {
- const {routes, params, location, stepBack} = options;
- const paths = routes.map(({path}) => {
- path = path || '';
- if (path.length > 0 && !path.endsWith('/')) {
- path = `${path}/`;
- }
- return path;
- });
- let lastRootIndex: number;
- let routeIndex: number | undefined;
-
- if (typeof to !== 'string') {
- routeIndex = routes.indexOf(to) + 1;
- lastRootIndex = findLastIndex(paths.slice(0, routeIndex), path => path[0] === '/');
- } else {
- lastRootIndex = findLastIndex(paths, path => path[0] === '/');
- }
- let baseRoute = paths.slice(lastRootIndex, routeIndex);
- if (typeof stepBack !== 'undefined') {
- baseRoute = baseRoute.slice(0, stepBack);
- }
- const search = location?.search ?? '';
- const hash = location?.hash ?? '';
- const fullRoute = `${baseRoute.join('')}${
- typeof to !== 'string' ? '' : to
- }${search}${hash}`;
- return replaceRouterParams(fullRoute, params);
- }
|