mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
simplify page/menu/registry implementation
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
parent
97068f4c07
commit
6d0c07b3d1
@ -57,29 +57,29 @@ export class ExtensionLoader {
|
||||
loadOnMain() {
|
||||
logger.info('[EXTENSIONS-LOADER]: load on main')
|
||||
this.autoInitExtensions((ext: LensMainExtension) => [
|
||||
registries.menuRegistry.add(ext.appMenus, { key: ext })
|
||||
registries.menuRegistry.add(ext.appMenus)
|
||||
]);
|
||||
}
|
||||
|
||||
loadOnClusterManagerRenderer() {
|
||||
logger.info('[EXTENSIONS-LOADER]: load on main renderer (cluster manager)')
|
||||
this.autoInitExtensions((ext: LensRendererExtension) => [
|
||||
registries.globalPageRegistry.add(ext.globalPages, { key: ext }),
|
||||
registries.globalPageMenuRegistry.add(ext.globalPageMenus, { key: ext }),
|
||||
registries.appPreferenceRegistry.add(ext.appPreferences, { key: ext }),
|
||||
registries.clusterFeatureRegistry.add(ext.clusterFeatures, { key: ext }),
|
||||
registries.statusBarRegistry.add(ext.statusBarItems, { key: ext }),
|
||||
registries.globalPageRegistry.add(ext.globalPages, ext),
|
||||
registries.globalPageMenuRegistry.add(ext.globalPageMenus, ext),
|
||||
registries.appPreferenceRegistry.add(ext.appPreferences),
|
||||
registries.clusterFeatureRegistry.add(ext.clusterFeatures),
|
||||
registries.statusBarRegistry.add(ext.statusBarItems),
|
||||
]);
|
||||
}
|
||||
|
||||
loadOnClusterRenderer() {
|
||||
logger.info('[EXTENSIONS-LOADER]: load on cluster renderer (dashboard)')
|
||||
this.autoInitExtensions((ext: LensRendererExtension) => [
|
||||
registries.clusterPageRegistry.add(ext.clusterPages, { key: ext }),
|
||||
registries.clusterPageMenuRegistry.add(ext.clusterPageMenus, { key: ext }),
|
||||
registries.kubeObjectMenuRegistry.add(ext.kubeObjectMenuItems, { key: ext }),
|
||||
registries.kubeObjectDetailRegistry.add(ext.kubeObjectDetailItems, { key: ext }),
|
||||
registries.kubeObjectStatusRegistry.add(ext.kubeObjectStatusTexts, { key: ext })
|
||||
registries.clusterPageRegistry.add(ext.clusterPages, ext),
|
||||
registries.clusterPageMenuRegistry.add(ext.clusterPageMenus, ext),
|
||||
registries.kubeObjectMenuRegistry.add(ext.kubeObjectMenuItems),
|
||||
registries.kubeObjectDetailRegistry.add(ext.kubeObjectDetailItems),
|
||||
registries.kubeObjectStatusRegistry.add(ext.kubeObjectStatusTexts)
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import type React from "react"
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
|
||||
export interface AppPreferenceComponents {
|
||||
Hint: React.ComponentType<any>;
|
||||
Input: React.ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface AppPreferenceRegistration extends BaseRegistryItem {
|
||||
export interface AppPreferenceRegistration {
|
||||
title: string;
|
||||
components: AppPreferenceComponents;
|
||||
}
|
||||
|
||||
@ -1,65 +1,24 @@
|
||||
// Base class for extensions-api registries
|
||||
import { action, observable } from "mobx";
|
||||
import { LensExtension } from "../lens-extension";
|
||||
import { getRandId } from "../../common/utils";
|
||||
|
||||
export type BaseRegistryKey = LensExtension | null;
|
||||
export type BaseRegistryItemId = string | symbol;
|
||||
export class BaseRegistry<T = any> {
|
||||
protected items = observable<T>([], { deep: false });
|
||||
|
||||
export interface BaseRegistryItem {
|
||||
id?: BaseRegistryItemId; // uniq id, generated automatically when not provided
|
||||
}
|
||||
|
||||
export interface BaseRegistryAddMeta {
|
||||
key?: BaseRegistryKey;
|
||||
merge?: boolean
|
||||
}
|
||||
|
||||
export class BaseRegistry<T extends BaseRegistryItem = any> {
|
||||
private items = observable.map<BaseRegistryKey, T[]>([], { deep: false });
|
||||
|
||||
getItems(): (T & { extension?: LensExtension | null })[] {
|
||||
return Array.from(this.items).map(([ext, items]) => {
|
||||
return items.map(item => ({
|
||||
...item,
|
||||
extension: ext,
|
||||
}))
|
||||
}).flat()
|
||||
}
|
||||
|
||||
getById(itemId: BaseRegistryItemId, key?: BaseRegistryKey): T {
|
||||
const byId = (item: BaseRegistryItem) => item.id === itemId;
|
||||
if (key) {
|
||||
return this.items.get(key)?.find(byId)
|
||||
}
|
||||
return this.getItems().find(byId);
|
||||
getItems(): T[] {
|
||||
return this.items.toJS();
|
||||
}
|
||||
|
||||
@action
|
||||
add(items: T | T[], { key = null, merge = true }: BaseRegistryAddMeta = {}) {
|
||||
const normalizedItems = (Array.isArray(items) ? items : [items]).map((item: T) => {
|
||||
item.id = item.id || getRandId();
|
||||
return item;
|
||||
});
|
||||
if (merge && this.items.has(key)) {
|
||||
const newItems = new Set(this.items.get(key));
|
||||
normalizedItems.forEach(item => newItems.add(item))
|
||||
this.items.set(key, [...newItems]);
|
||||
} else {
|
||||
this.items.set(key, normalizedItems);
|
||||
}
|
||||
return () => this.remove(normalizedItems, key)
|
||||
add(items: T | T[]) {
|
||||
const normalizedItems = (Array.isArray(items) ? items : [items])
|
||||
this.items.push(...normalizedItems);
|
||||
return () => this.remove(...normalizedItems);
|
||||
}
|
||||
|
||||
@action
|
||||
remove(items: T[], key: BaseRegistryKey = null) {
|
||||
const storedItems = this.items.get(key);
|
||||
if (!storedItems) return;
|
||||
const newItems = storedItems.filter(item => !items.includes(item)); // works because of {deep: false};
|
||||
if (newItems.length > 0) {
|
||||
this.items.set(key, newItems)
|
||||
} else {
|
||||
this.items.delete(key);
|
||||
}
|
||||
remove(...items: T[]) {
|
||||
items.forEach(item => {
|
||||
this.items.remove(item); // works because of {deep: false};
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import type React from "react"
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
import { ClusterFeature } from "../cluster-feature";
|
||||
|
||||
export interface ClusterFeatureComponents {
|
||||
Description: React.ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface ClusterFeatureRegistration extends BaseRegistryItem {
|
||||
export interface ClusterFeatureRegistration {
|
||||
title: string;
|
||||
components: ClusterFeatureComponents
|
||||
feature: ClusterFeature
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import React from "react"
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
|
||||
export interface KubeObjectDetailComponents {
|
||||
Details: React.ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface KubeObjectDetailRegistration extends BaseRegistryItem {
|
||||
export interface KubeObjectDetailRegistration {
|
||||
kind: string;
|
||||
apiVersions: string[];
|
||||
components: KubeObjectDetailComponents;
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import React from "react"
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
|
||||
export interface KubeObjectMenuComponents {
|
||||
MenuItem: React.ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface KubeObjectMenuRegistration extends BaseRegistryItem {
|
||||
export interface KubeObjectMenuRegistration {
|
||||
kind: string;
|
||||
apiVersions: string[];
|
||||
components: KubeObjectMenuComponents;
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { KubeObject, KubeObjectStatus } from "../renderer-api/k8s-api";
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
|
||||
export interface KubeObjectStatusRegistration extends BaseRegistryItem {
|
||||
export interface KubeObjectStatusRegistration {
|
||||
kind: string;
|
||||
apiVersions: string[];
|
||||
resolve: (object: KubeObject) => KubeObjectStatus;
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
// Extensions-api -> Register page menu items
|
||||
|
||||
import type React from "react";
|
||||
import { action } from "mobx";
|
||||
import type { IconProps } from "../../renderer/components/icon";
|
||||
import { BaseRegistry, BaseRegistryItem, BaseRegistryItemId } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
import { LensExtension } from "../lens-extension";
|
||||
import { getPageUrl } from "./page-registry";
|
||||
|
||||
export interface PageMenuRegistration extends BaseRegistryItem {
|
||||
id: BaseRegistryItemId; // required id from page-registry item to match with
|
||||
export interface PageMenuRegistration {
|
||||
url?: string; // when not provided initial extension's path used, e.g. "/extension/lens-extension-name"
|
||||
title: React.ReactNode;
|
||||
components: PageMenuComponents;
|
||||
@ -22,11 +24,17 @@ export interface PageMenuComponents {
|
||||
}
|
||||
|
||||
export class PageMenuRegistry<T extends PageMenuRegistration> extends BaseRegistry<T> {
|
||||
getItems() {
|
||||
return super.getItems().map(item => {
|
||||
item.url = item.extension.getPageUrl(item.url)
|
||||
return item
|
||||
});
|
||||
@action
|
||||
add(items: T[], ext?: LensExtension) {
|
||||
const normalizedItems = items.map((i) => {
|
||||
i.url = getPageUrl(ext, i.url)
|
||||
return i
|
||||
})
|
||||
return super.add(normalizedItems);
|
||||
}
|
||||
|
||||
getByRoutePath(routePath: string) {
|
||||
return this.getItems().find((i) => i.url === routePath)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
// 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"
|
||||
|
||||
export interface PageRegistration extends BaseRegistryItem {
|
||||
export interface PageRegistration {
|
||||
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;
|
||||
@ -20,12 +23,26 @@ 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);
|
||||
}
|
||||
|
||||
getByUrl(url: string) {
|
||||
return this.getItems().find((i) => i.routePath === url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
// Extensions API -> Status bar customizations
|
||||
|
||||
import React from "react";
|
||||
import { BaseRegistry, BaseRegistryItem } from "./base-registry";
|
||||
import { BaseRegistry } from "./base-registry";
|
||||
|
||||
export interface StatusBarRegistration extends BaseRegistryItem {
|
||||
export interface StatusBarRegistration {
|
||||
item?: React.ReactNode;
|
||||
}
|
||||
|
||||
|
||||
@ -74,11 +74,11 @@ export class App extends React.Component {
|
||||
}
|
||||
|
||||
renderExtensionRoutes() {
|
||||
return clusterPageRegistry.getItems().map(({ id: pageId, components: { Page }, exact, routePath, subPages }) => {
|
||||
return clusterPageRegistry.getItems().map(({ components: { Page }, exact, routePath, subPages }) => {
|
||||
const Component = () => {
|
||||
if (subPages) {
|
||||
const tabs: TabLayoutRoute[] = subPages.map(({ exact, routePath, components: { Page } }) => {
|
||||
const menuItem = clusterPageMenuRegistry.getById(pageId);
|
||||
const menuItem = clusterPageMenuRegistry.getByRoutePath(routePath);
|
||||
if (!menuItem) return;
|
||||
return {
|
||||
routePath, exact,
|
||||
|
||||
@ -70,7 +70,7 @@ export class ClusterManager extends React.Component {
|
||||
<Route component={ClusterView} {...clusterViewRoute} />
|
||||
<Route component={ClusterSettings} {...clusterSettingsRoute} />
|
||||
{globalPageRegistry.getItems().map(({ routePath, exact, components: { Page } }) => {
|
||||
return <Route key={routePath} path={routePath} component={Page} exact={exact}/>
|
||||
return <Route key={routePath} path={routePath} component={Page} exact={true}/>
|
||||
})}
|
||||
<Redirect exact to={this.startUrl}/>
|
||||
</Switch>
|
||||
|
||||
@ -148,8 +148,8 @@ export class ClustersMenu extends React.Component<Props> {
|
||||
)}
|
||||
</div>
|
||||
<div className="extensions">
|
||||
{globalPageMenuRegistry.getItems().map(({ id: menuItemId, title, url, components: { Icon } }) => {
|
||||
const registeredPage = globalPageRegistry.getById(menuItemId);
|
||||
{globalPageMenuRegistry.getItems().map(({ title, url, components: { Icon } }) => {
|
||||
const registeredPage = globalPageRegistry.getByUrl(url);
|
||||
if (!registeredPage) return;
|
||||
const { routePath, exact } = registeredPage;
|
||||
return (
|
||||
|
||||
@ -191,8 +191,9 @@ export class Sidebar extends React.Component<Props> {
|
||||
>
|
||||
{this.renderCustomResources()}
|
||||
</SidebarNavItem>
|
||||
{clusterPageMenuRegistry.getItems().map(({ id: menuItemId, title, url, components: { Icon } }) => {
|
||||
const registeredPage = clusterPageRegistry.getById(menuItemId);
|
||||
{clusterPageMenuRegistry.getItems().map(({ title, url, components: { Icon } }) => {
|
||||
const registeredPage = clusterPageRegistry.getByUrl(url);
|
||||
console.log(url, registeredPage)
|
||||
if (!registeredPage) return;
|
||||
const { routePath, exact } = registeredPage;
|
||||
return (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user