Default PageRegistration.routePath to path based on id (#1374)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2020-11-13 19:37:03 +02:00
1 parent 648dbfee98
commit 0fb859a22a
3 files changed
+13 -14

No files matched your search

@@ -28,4 +28,8 @@ describe("getPageUrl", () => {
ext.manifest.name = "@foo/bar"
expect(getPageUrl(ext)).toBe("/extension/foo-bar")
})
it("adds / prefix", () => {
expect(getPageUrl(ext, "test")).toBe("/extension/foo-bar/test")
})
})
@@ -18,11 +18,6 @@ export interface PageMenuRegistration {
components: PageMenuComponents;
}
export interface PageSubMenuRegistration {
url: string;
title: React.ReactNode;
}
export interface PageMenuComponents {
Icon: React.ComponentType<IconProps>;
}
+9 -9
View File
@@ -14,12 +14,6 @@ export interface PageRegistration {
components: PageComponents;
}
export interface SubPageRegistration {
routePath: string; // required for sub-pages
exact?: boolean;
components: PageComponents;
}
export interface PageComponents {
Page: React.ComponentType<any>;
}
@@ -27,6 +21,9 @@ export interface PageComponents {
const routePrefix = "/extension/:name"
export function getPageUrl(ext: LensExtension, baseUrl = "") {
if (baseUrl !== "" && !baseUrl.startsWith("/")) {
baseUrl = "/" + baseUrl
}
const validUrlName = ext.name.replace("@", "").replace("/", "-");
return compile(routePrefix)({ name: validUrlName }) + baseUrl;
}
@@ -35,9 +32,12 @@ export class PageRegistry<T extends PageRegistration> extends BaseRegistry<T> {
@action
add(items: T[], ext?: LensExtension) {
const normalizedItems = items.map((i) => {
i.routePath = getPageUrl(ext, i.routePath)
return i
const normalizedItems = items.map((page) => {
if (!page.routePath) {
page.routePath = `/${page.id}`
}
page.routePath = getPageUrl(ext, page.routePath)
return page
})
return super.add(normalizedItems);
}