mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Simplify pages/menus/registry extension api internal implementation (#1364)
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com> Co-authored-by: Roman <ixrock@gmail.com>
This commit is contained in:
1 parent
eccc502ef2
commit
df0f080380
21 files changed
+176
-142
No files matched your search
@@ -1,13 +1,17 @@
|
||||
// Extensions-api -> Custom page registration
|
||||
|
||||
import React from "react";
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { action } from "mobx";
|
||||
import { compile } from "path-to-regexp";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
import { LensExtension } from "../lens-extension"
|
||||
import type { PageMenuTarget } from "./page-menu-registry";
|
||||
|
||||
export interface PageRegistration extends BaseRegistryItem {
|
||||
export interface PageRegistration {
|
||||
id: string; // will be automatically prefixed with extension name
|
||||
routePath?: string; // additional (suffix) route path to base extension's route: "/extension/:name"
|
||||
exact?: boolean; // route matching flag, see: https://reactrouter.com/web/api/NavLink/exact-bool
|
||||
components: PageComponents;
|
||||
subPages?: SubPageRegistration[];
|
||||
}
|
||||
|
||||
export interface SubPageRegistration {
|
||||
@@ -20,14 +24,31 @@ export interface PageComponents {
|
||||
Page: React.ComponentType<any>;
|
||||
}
|
||||
|
||||
const routePrefix = "/extension/:name"
|
||||
|
||||
export function getPageUrl(ext: LensExtension, baseUrl = "") {
|
||||
const validUrlName = ext.name.replace("@", "").replace("/", "-");
|
||||
return compile(routePrefix)({ name: validUrlName }) + baseUrl;
|
||||
}
|
||||
|
||||
export class PageRegistry<T extends PageRegistration> extends BaseRegistry<T> {
|
||||
getItems() {
|
||||
return super.getItems().map(item => {
|
||||
item.routePath = item.extension.getPageRoute(item.routePath)
|
||||
return item
|
||||
});
|
||||
|
||||
@action
|
||||
add(items: T[], ext?: LensExtension) {
|
||||
const normalizedItems = items.map((i) => {
|
||||
i.routePath = getPageUrl(ext, i.routePath)
|
||||
return i
|
||||
})
|
||||
return super.add(normalizedItems);
|
||||
}
|
||||
|
||||
getByPageMenuTarget(target: PageMenuTarget) {
|
||||
if (!target) {
|
||||
return null
|
||||
}
|
||||
return this.getItems().find((page) => page.routePath.startsWith(`/extension/${target.extensionId}/`) && page.id === target.pageId)
|
||||
}
|
||||
}
|
||||
|
||||
export const globalPageRegistry = new PageRegistry<Omit<PageRegistration, "subPages">>();
|
||||
export const clusterPageRegistry = new PageRegistry();
|
||||
export const globalPageRegistry = new PageRegistry<PageRegistration>();
|
||||
export const clusterPageRegistry = new PageRegistry<PageRegistration>();
|
||||
Reference in new issue
Block a user