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

Add optional badge to Catalog Category label (#5156)

* Add optional badge to Catalog Category label.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Don't render badge container if there is no badge. Change CatalogCategoryLabel interface.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Add badgeBackgroundColor CSS variable for extensions. Regenerate theme-vars.css.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>

* Use category as prop in CatalogCategoryLabel

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2022-04-06 15:20:41 +03:00 committed by GitHub
parent 66330485b1
commit ca21e0842a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 117 additions and 4 deletions

View File

@ -3,15 +3,18 @@
* Licensed under MIT License. See LICENSE in root directory for more information. * Licensed under MIT License. See LICENSE in root directory for more information.
*/ */
import React from "react";
import { CatalogCategory, CatalogCategorySpec } from "../catalog"; import { CatalogCategory, CatalogCategorySpec } from "../catalog";
class TestCatalogCategory extends CatalogCategory { class TestCatalogCategoryWithoutBadge extends CatalogCategory {
public readonly apiVersion = "catalog.k8slens.dev/v1alpha1"; public readonly apiVersion = "catalog.k8slens.dev/v1alpha1";
public readonly kind = "CatalogCategory"; public readonly kind = "CatalogCategory";
public metadata = { public metadata = {
name: "Test Category", name: "Test Category",
icon: "", icon: "",
}; };
public spec: CatalogCategorySpec = { public spec: CatalogCategorySpec = {
group: "entity.k8slens.dev", group: "entity.k8slens.dev",
versions: [], versions: [],
@ -21,10 +24,28 @@ class TestCatalogCategory extends CatalogCategory {
}; };
} }
class TestCatalogCategoryWithBadge extends TestCatalogCategoryWithoutBadge {
getBadge() {
return (<div>Test Badge</div>);
}
}
describe("CatalogCategory", () => { describe("CatalogCategory", () => {
it("returns name", () => { it("returns name", () => {
const category = new TestCatalogCategory(); const category = new TestCatalogCategoryWithoutBadge();
expect(category.getName()).toEqual("Test Category"); expect(category.getName()).toEqual("Test Category");
}); });
it("doesn't return badge by default", () => {
const category = new TestCatalogCategoryWithoutBadge();
expect(category.getBadge()).toEqual(null);
});
it("returns a badge", () => {
const category = new TestCatalogCategoryWithBadge();
expect(category.getBadge()).toBeTruthy();
});
}); });

View File

@ -180,6 +180,15 @@ export abstract class CatalogCategory extends (EventEmitter as new () => TypedEm
return this.metadata.name; return this.metadata.name;
} }
/**
* Get the badge of this category.
* Defaults to no badge.
* The badge is displayed next to the Category name in the Catalog Category menu
*/
public getBadge(): React.ReactNode {
return null;
}
/** /**
* Add a filter for menu items of catalogAddMenu * 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 * @param fn The function that should return a truthy value if that menu item should be displayed

View File

@ -0,0 +1,53 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { render, screen } from "@testing-library/react";
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { CatalogCategory, CatalogCategorySpec } from "../../../../common/catalog";
import { CatalogCategoryLabel } from "../catalog-category-label";
class CatalogCategoryWithoutBadge 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",
},
};
}
class CatalogCategoryWithBadge extends CatalogCategoryWithoutBadge {
getBadge() {
return (<div>Test Badge</div>);
}
}
describe("CatalogCategoryLabel", () => {
it("renders without a badge", async () => {
const category = new CatalogCategoryWithoutBadge();
render(<CatalogCategoryLabel category={category}/>);
expect(await screen.findByText("Test Category")).toBeInTheDocument();
});
it("renders with a badge", async () => {
const category = new CatalogCategoryWithBadge();
render(<CatalogCategoryLabel category={category}/>);
expect(await screen.findByText("Test Category")).toBeInTheDocument();
expect(await screen.findByText("Test Badge")).toBeInTheDocument();
});
});

View File

@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import type { CatalogCategory } from "../../../common/catalog/catalog-entity";
interface CatalogCategoryLabelProps {
category: CatalogCategory;
}
/**
* Display label for Catalog Category for the Catalog menu
*/
export const CatalogCategoryLabel = ({ category }: CatalogCategoryLabelProps) => {
const badge = category.getBadge();
return (
<div className="flex">
<div>{category.metadata.name}</div>
{badge ? (<div className="flex items-center">{badge}</div>) : null}
</div>
);
};

View File

@ -14,6 +14,7 @@ import { StylesProvider } from "@material-ui/core";
import { cssNames } from "../../utils"; import { cssNames } from "../../utils";
import type { CatalogCategory } from "../../api/catalog-entity"; import type { CatalogCategory } from "../../api/catalog-entity";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { CatalogCategoryLabel } from "./catalog-category-label";
export interface CatalogMenuProps { export interface CatalogMenuProps {
activeItem: string; activeItem: string;
@ -66,7 +67,7 @@ export const CatalogMenu = observer((props: CatalogMenuProps) => {
icon={getCategoryIcon(category)} icon={getCategoryIcon(category)}
key={category.getId()} key={category.getId()}
nodeId={category.getId()} nodeId={category.getId()}
label={category.metadata.name} label={<CatalogCategoryLabel category={category}/>}
data-testid={`${category.getId()}-tab`} data-testid={`${category.getId()}-tab`}
onClick={() => props.onItemClick(category.getId())} onClick={() => props.onItemClick(category.getId())}
/> />

View File

@ -29,6 +29,7 @@
"sidebarActiveColor": "#ffffff", "sidebarActiveColor": "#ffffff",
"sidebarSubmenuActiveColor": "#ffffff", "sidebarSubmenuActiveColor": "#ffffff",
"sidebarItemHoverBackground": "#3a3e44", "sidebarItemHoverBackground": "#3a3e44",
"badgeBackgroundColor": "#ffba44",
"buttonPrimaryBackground": "#3d90ce", "buttonPrimaryBackground": "#3d90ce",
"buttonDefaultBackground": "#414448", "buttonDefaultBackground": "#414448",
"buttonLightBackground": "#f1f1f1", "buttonLightBackground": "#f1f1f1",

View File

@ -29,6 +29,7 @@
"sidebarSubmenuActiveColor": "#3d90ce", "sidebarSubmenuActiveColor": "#3d90ce",
"sidebarBackground": "#e8e8e8", "sidebarBackground": "#e8e8e8",
"sidebarItemHoverBackground": "#f0f2f5", "sidebarItemHoverBackground": "#f0f2f5",
"badgeBackgroundColor": "#ffba44",
"buttonPrimaryBackground": "#3d90ce", "buttonPrimaryBackground": "#3d90ce",
"buttonDefaultBackground": "#414448", "buttonDefaultBackground": "#414448",
"buttonLightBackground": "#f1f1f1", "buttonLightBackground": "#f1f1f1",

View File

@ -29,6 +29,7 @@
--sidebarActiveColor: #ffffff; --sidebarActiveColor: #ffffff;
--sidebarSubmenuActiveColor: #ffffff; --sidebarSubmenuActiveColor: #ffffff;
--sidebarItemHoverBackground: #3a3e44; --sidebarItemHoverBackground: #3a3e44;
--badgeBackgroundColor: #ffba44;
--buttonPrimaryBackground: #3d90ce; --buttonPrimaryBackground: #3d90ce;
--buttonDefaultBackground: #414448; --buttonDefaultBackground: #414448;
--buttonLightBackground: #f1f1f1; --buttonLightBackground: #f1f1f1;
@ -73,6 +74,8 @@
--dockEditorComment: #808080; --dockEditorComment: #808080;
--dockEditorActiveLineBackground: #3a3d41; --dockEditorActiveLineBackground: #3a3d41;
--dockBadgeBackground: #36393e; --dockBadgeBackground: #36393e;
--dockTabBorderColor: #43424d;
--dockTabActiveBackground: #3a3e45;
--logsBackground: #000000; --logsBackground: #000000;
--logsForeground: #ffffff; --logsForeground: #ffffff;
--logRowHoverBackground: #35373a; --logRowHoverBackground: #35373a;
@ -125,7 +128,7 @@
--inputControlHoverBorder: #474a4f; --inputControlHoverBorder: #474a4f;
--lineProgressBackground: #414448; --lineProgressBackground: #414448;
--radioActiveBackground: #36393e; --radioActiveBackground: #36393e;
--menuActiveBackground: #36393e; --menuActiveBackground: #3d90ce;
--menuSelectedOptionBgc: #36393e; --menuSelectedOptionBgc: #36393e;
--canvasBackground: #24292e; --canvasBackground: #24292e;
--scrollBarColor: #5f6064; --scrollBarColor: #5f6064;