diff --git a/src/common/catalog-entities/__tests__/kubernetes-cluster.test.ts b/src/common/catalog-entities/__tests__/kubernetes-cluster.test.ts new file mode 100644 index 0000000000..d14a093ee5 --- /dev/null +++ b/src/common/catalog-entities/__tests__/kubernetes-cluster.test.ts @@ -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]); + }); + }); +}); diff --git a/src/common/catalog/catalog-entity.ts b/src/common/catalog/catalog-entity.ts index 7b9db2f0ff..d59a21686c 100644 --- a/src/common/catalog/catalog-entity.ts +++ b/src/common/catalog/catalog-entity.ts @@ -22,6 +22,8 @@ import EventEmitter from "events"; import type TypedEmitter from "typed-emitter"; import { observable, makeObservable } from "mobx"; +import { once } from "lodash"; +import { iter, Disposer } from "../utils"; type ExtractEntityMetadataType = Entity extends CatalogEntity ? Metadata : never; type ExtractEntityStatusType = Entity extends CatalogEntity ? 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 { load: () => void; catalogAddMenu: (context: CatalogEntityAddMenuContext) => void; @@ -63,6 +70,10 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm }; abstract spec: CatalogCategorySpec; + protected filters = observable.set([], { + deep: false, + }); + static parseId(id = ""): { group?: string, kind?: string } { const [group, kind] = id.split("/") ?? []; @@ -72,6 +83,32 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm public getId(): string { 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 { diff --git a/src/renderer/components/+catalog/catalog-add-button.tsx b/src/renderer/components/+catalog/catalog-add-button.tsx index 8f3cc20412..558cdca3db 100644 --- a/src/renderer/components/+catalog/catalog-add-button.tsx +++ b/src/renderer/components/+catalog/catalog-add-button.tsx @@ -80,7 +80,9 @@ export class CatalogAddButton extends React.Component { } render() { - if (this.menuItems.length === 0) { + const filteredItems = this.props.category ? this.props.category.filteredItems(this.menuItems) : []; + + if (filteredItems.length === 0) { return null; } @@ -95,7 +97,7 @@ export class CatalogAddButton extends React.Component { direction="up" onClick={this.onButtonClick} > - { this.menuItems.map((menuItem, index) => { + {filteredItems.map((menuItem, index) => { return }