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

Add catalog menu filter.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2021-09-02 15:45:49 +03:00
parent e2b096fae1
commit e413430528
3 changed files with 103 additions and 2 deletions

View File

@ -0,0 +1,62 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { kubernetesClusterCategory } from "../kubernetes-cluster";
describe("kubernetesClusterCategory", () => {
describe("filteredItems", () => {
const item1 = {
icon: "Icon",
title: "Title",
// eslint-disable-next-line @typescript-eslint/no-empty-function
onClick: () => {}
};
const item2 = {
icon: "Icon 2",
title: "Title 2",
// eslint-disable-next-line @typescript-eslint/no-empty-function
onClick: () => {}
};
it("returns all items if no filter set", () => {
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([item1, item2]);
});
it("returns filtered items", () => {
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([item1, item2]);
const disposer1 = kubernetesClusterCategory.addMenuFilter(item => item.icon === "Icon");
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([item1]);
const disposer2 = kubernetesClusterCategory.addMenuFilter(item => item.title === "Title 2");
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([]);
disposer1();
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([item2]);
disposer2();
expect(kubernetesClusterCategory.filteredItems([item1, item2])).toEqual([item1, item2]);
});
});
});

View File

@ -22,6 +22,8 @@
import EventEmitter from "events"; import EventEmitter from "events";
import type TypedEmitter from "typed-emitter"; import type TypedEmitter from "typed-emitter";
import { observable, makeObservable } from "mobx"; import { observable, makeObservable } from "mobx";
import { once } from "lodash";
import { iter, Disposer } from "../utils";
type ExtractEntityMetadataType<Entity> = Entity extends CatalogEntity<infer Metadata> ? Metadata : never; type ExtractEntityMetadataType<Entity> = Entity extends CatalogEntity<infer Metadata> ? Metadata : never;
type ExtractEntityStatusType<Entity> = Entity extends CatalogEntity<any, infer Status> ? Status : never; type ExtractEntityStatusType<Entity> = Entity extends CatalogEntity<any, infer Status> ? Status : never;
@ -48,6 +50,11 @@ export interface CatalogCategorySpec {
}; };
} }
/**
* If the filter returns true, the menu item is displayed
*/
export type AddMenuFilter = (menu: CatalogEntityAddMenu) => any;
export interface CatalogCategoryEvents { export interface CatalogCategoryEvents {
load: () => void; load: () => void;
catalogAddMenu: (context: CatalogEntityAddMenuContext) => void; catalogAddMenu: (context: CatalogEntityAddMenuContext) => void;
@ -63,6 +70,10 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm
}; };
abstract spec: CatalogCategorySpec; abstract spec: CatalogCategorySpec;
protected filters = observable.set<AddMenuFilter>([], {
deep: false,
});
static parseId(id = ""): { group?: string, kind?: string } { static parseId(id = ""): { group?: string, kind?: string } {
const [group, kind] = id.split("/") ?? []; const [group, kind] = id.split("/") ?? [];
@ -72,6 +83,32 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm
public getId(): string { public getId(): string {
return `${this.spec.group}/${this.spec.names.kind}`; return `${this.spec.group}/${this.spec.names.kind}`;
} }
/**
* Add a filter for menu items of catalogAddMenu
* @param fn The function that should return a truthy value if that menu item should be displayed
* @returns A function to remove that filter
*/
public addMenuFilter(fn: AddMenuFilter): Disposer {
this.filters.add(fn);
return once(() => void this.filters.delete(fn));
}
/**
* Filter menuItems according to the Category's set filters
* @param menuItems menu items to filter
* @returns filtered menu items
*/
public filteredItems(menuItems: CatalogEntityAddMenu[]) {
return Array.from(
iter.reduce(
this.filters,
iter.filter,
menuItems,
)
);
}
} }
export interface CatalogEntityMetadata { export interface CatalogEntityMetadata {

View File

@ -80,7 +80,9 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
} }
render() { render() {
if (this.menuItems.length === 0) { const filteredItems = this.props.category ? this.props.category.filteredItems(this.menuItems) : [];
if (filteredItems.length === 0) {
return null; return null;
} }
@ -95,7 +97,7 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
direction="up" direction="up"
onClick={this.onButtonClick} onClick={this.onButtonClick}
> >
{ this.menuItems.map((menuItem, index) => { {filteredItems.map((menuItem, index) => {
return <SpeedDialAction return <SpeedDialAction
key={index} key={index}
icon={<Icon material={menuItem.icon}/>} icon={<Icon material={menuItem.icon}/>}