Skip to content
Snippets Groups Projects
Unverified Commit 176ded67 authored by Emmad Usmani's avatar Emmad Usmani Committed by GitHub
Browse files

fix url bug in sandbox editor (#33469)

parent e33b06fc
Branches
Tags
No related merge requests found
......@@ -5,6 +5,7 @@ import { push } from "react-router-redux";
import { connect } from "react-redux";
import type { LocationDescriptor } from "history";
import MetabaseSettings from "metabase/lib/settings";
import Modal from "metabase/components/Modal";
type IRoute = {
......@@ -12,7 +13,23 @@ type IRoute = {
};
export const getParentPath = (route: IRoute, location: Location) => {
const fullPathSegments = location.pathname.split("/");
// If instance has a custom url we need to exclude its subpath
const siteUrlSegments = (MetabaseSettings.get("site-url") ?? "").split("/");
const subPath = siteUrlSegments.slice(3).join("/");
let pathName: string;
if (subPath) {
const subPathSplit = location.pathname.split(subPath);
pathName =
subPathSplit.length === 1
? subPathSplit[0]
: subPathSplit.slice(1).join(subPath);
} else {
pathName = location.pathname;
}
const fullPathSegments = pathName.split("/");
const routeSegments = route.path.split("/");
fullPathSegments.splice(-routeSegments.length);
......
import { mockSettings } from "__support__/settings";
import { getParentPath } from "./ModalRoute";
const setup = (routePath, locationPath) => {
const setup = (routePath, locationPath, siteURL = undefined) => {
if (siteURL) {
mockSettings({ "site-url": siteURL });
}
return getParentPath({ path: routePath }, { pathname: locationPath });
};
......@@ -33,5 +38,39 @@ describe("getParentPath", () => {
expect(parentPath).toEqual("/a/b");
});
it("without single segment site url subpath", () => {
const parentPath = setup(
"c",
"metabase/a/b/c",
"https://somesite.com/metabase",
);
expect(parentPath).toEqual("/a/b");
});
it("without multi segment site url subpath", () => {
const parentPath = setup(
"c",
"meta/base/a/b/c",
"https://somesite.com/meta/base",
);
expect(parentPath).toEqual("/a/b");
});
// This is to handle the edge case where someone uses a subpath name
// like "data" that is also used within Metabase's routes
// e.g. If the site-url is "https://corp.com/data", it should not
// remove the second "data" from the path in "https://corp.com/data/admin/permissions/data/groups"
it("without leading url subpath while preserving later occurances", () => {
const parentPath = setup(
"groups",
"data/admin/permissions/data/groups",
"https://corp.com/data",
);
expect(parentPath).toEqual("/admin/permissions/data");
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment