1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

example-extension reworks -- part 1

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-09-08 12:07:21 +03:00 committed by Lauri Nevala
parent 1c5c394637
commit f5cea39c64
5 changed files with 33 additions and 20 deletions

View File

@ -3,21 +3,23 @@ import React from "react";
import path from "path";
export default class ExampleExtension extends LensExtension {
protected routePath = "/extension-example"
protected unRegisterPage = Function();
onActivate() {
console.log('EXAMPLE EXTENSION: ACTIVATE', this.getMeta())
const { dynamicPages } = this.runtime;
dynamicPages.register(this.routePath, {
Main: ExtensionPage,
MenuIcon: ExtensionIcon,
this.unRegisterPage = this.runtime.dynamicPages.register({
type: "cluster-view",
path: "/extension-example",
components: {
Main: ExtensionPage,
MenuIcon: ExtensionIcon,
}
})
}
onDeactivate() {
console.log('EXAMPLE EXTENSION: DEACTIVATE', this.getMeta());
const { dynamicPages } = this.runtime;
dynamicPages.unregister(this.routePath);
this.unRegisterPage();
}
}

View File

@ -13,7 +13,7 @@ export interface ExtensionStoreModel {
}
export interface ExtensionModel {
id?: ExtensionId; // available in lens-extension instance
id: ExtensionId;
version: ExtensionVersion;
name: string;
manifestPath: string;

View File

@ -63,8 +63,8 @@ export class ClusterManager extends React.Component {
<Route component={ClusterView} {...clusterViewRoute}/>
<Route component={ClusterSettings} {...clusterSettingsRoute}/>
<Route component={Extensions} {...extensionsRoute}/>
{Array.from(dynamicPages.routes).map(([path, { Main }]) => {
return <Route key={path} path={path} component={Main}/>
{dynamicPages.globalPages.map(({ path, components: { Page } }) => {
return <Route key={path} path={path} component={Page}/>
})}
<Redirect exact to={this.startUrl}/>
</Switch>

View File

@ -149,7 +149,7 @@ export class ClustersMenu extends React.Component<Props> {
)}
</div>
<div className="dynamic-pages">
{Array.from(dynamicPages.routes).map(([path, { MenuIcon }]) => {
{dynamicPages.globalPages.map(({ path, components: { MenuIcon } }) => {
if (MenuIcon) {
return <MenuIcon key={path} onClick={() => navigate(path)}/>
}

View File

@ -1,27 +1,38 @@
// Dynamic pages
import React from "react";
import { observable } from "mobx";
import { computed, observable } from "mobx";
import type { IconProps } from "../icon";
export interface PageRegistration {
path: string;
type: "global" | "cluster-view";
components: PageComponents;
}
export interface PageComponents {
Main: React.ComponentType<any>;
Page: React.ComponentType<any>;
MenuIcon: React.ComponentType<IconProps>;
}
export class PagesStore {
routes = observable.map<string, PageComponents>();
protected pages = observable.array<PageRegistration>([], { deep: false });
getComponents(path: string): PageComponents | null {
return this.routes.get(path);
@computed get globalPages() {
return this.pages.filter(page => page.type === "global");
}
register(path: string, components: PageComponents) {
this.routes.set(path, components);
@computed get clusterPages() {
return this.pages.filter(page => page.type === "cluster-view");
}
unregister(path: string) {
this.routes.delete(path);
register(params: PageRegistration) {
this.pages.push(params);
return () => {
this.pages.replace(
this.pages.filter(page => page.components !== params.components)
)
};
}
}