mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add catalogAddMenu filter to CatalogCategory (#3722)
This commit is contained in:
1 parent
9d9f9c0072
commit
8e9dd50828
6 files changed
+253
-2
No files matched your search
@@ -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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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> = Entity extends CatalogEntity<infer Metadata> ? Metadata : 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 {
|
||||
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<AddMenuFilter>([], {
|
||||
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 {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 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 React from "react";
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { CatalogCategory, CatalogCategorySpec } from "../../../../common/catalog";
|
||||
import { CatalogAddButton } from "../catalog-add-button";
|
||||
|
||||
class TestCatalogCategory extends CatalogCategory {
|
||||
public readonly apiVersion = "catalog.k8slens.dev/v1alpha1";
|
||||
public readonly kind = "CatalogCategory";
|
||||
public metadata = {
|
||||
name: "Test Category",
|
||||
icon: "",
|
||||
};
|
||||
public spec: CatalogCategorySpec = {
|
||||
group: "entity.k8slens.dev",
|
||||
versions: [],
|
||||
names: {
|
||||
kind: "Test"
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
describe("CatalogAddButton", () => {
|
||||
it("opens Add menu", async () => {
|
||||
const category = new TestCatalogCategory();
|
||||
|
||||
category.on("catalogAddMenu", ctx => {
|
||||
ctx.menuItems.push(
|
||||
{
|
||||
icon: "text_snippet",
|
||||
title: "Add from kubeconfig",
|
||||
onClick: () => {}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
render(<CatalogAddButton category={category}/>);
|
||||
|
||||
userEvent.hover(screen.getByLabelText("SpeedDial CatalogAddButton"));
|
||||
await screen.findByTitle("Add from kubeconfig");
|
||||
});
|
||||
|
||||
it("filters menu items", async () => {
|
||||
const category = new TestCatalogCategory();
|
||||
|
||||
category.on("catalogAddMenu", ctx => {
|
||||
ctx.menuItems.push(
|
||||
{
|
||||
icon: "text_snippet",
|
||||
title: "foobar",
|
||||
onClick: () => {}
|
||||
}
|
||||
);
|
||||
ctx.menuItems.push(
|
||||
{
|
||||
icon: "text_snippet",
|
||||
title: "Add from kubeconfig",
|
||||
onClick: () => {}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
category.addMenuFilter(item => item.title === "foobar");
|
||||
|
||||
render(<CatalogAddButton category={category}/>);
|
||||
|
||||
userEvent.hover(screen.getByLabelText("SpeedDial CatalogAddButton"));
|
||||
|
||||
await expect(screen.findByTitle("Add from kubeconfig"))
|
||||
.rejects
|
||||
.toThrow();
|
||||
await screen.findByTitle("foobar");
|
||||
});
|
||||
});
|
||||
@@ -80,7 +80,9 @@ export class CatalogAddButton extends React.Component<CatalogAddButtonProps> {
|
||||
}
|
||||
|
||||
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<CatalogAddButtonProps> {
|
||||
direction="up"
|
||||
onClick={this.onButtonClick}
|
||||
>
|
||||
{ this.menuItems.map((menuItem, index) => {
|
||||
{filteredItems.map((menuItem, index) => {
|
||||
return <SpeedDialAction
|
||||
key={index}
|
||||
icon={<Icon material={menuItem.icon}/>}
|
||||
|
||||
Reference in new issue
Block a user