1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/hotbar/hotbar-entity-icon.tsx
Alex Andreev a930d5f14f
Hotbar disabled items (#2710)
* Saving more entity data to HotbarItem

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Adding generic MaterialTooltip component

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Move HotbarCell to separate component

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Abstract out HotbarEntityIcon from HotbarIcon

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Styling disabled hotbar items

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Migration for adding extra data to hotbar items

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Testing migration

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Some cleaning up

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Bump migration version

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Bump app version in package.json

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
2021-05-13 10:24:58 +03:00

116 lines
3.3 KiB
TypeScript

import "./hotbar-icon.scss";
import React, { DOMAttributes } from "react";
import { observable } from "mobx";
import { observer } from "mobx-react";
import randomColor from "randomcolor";
import { CatalogEntity, CatalogEntityContextMenu, CatalogEntityContextMenuContext } from "../../../common/catalog";
import { catalogCategoryRegistry } from "../../api/catalog-category-registry";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry";
import { navigate } from "../../navigation";
import { cssNames, IClassName } from "../../utils";
import { ConfirmDialog } from "../confirm-dialog";
import { Icon } from "../icon";
import { HotbarIcon } from "./hotbar-icon";
interface Props extends DOMAttributes<HTMLElement> {
entity: CatalogEntity;
className?: IClassName;
errorClass?: IClassName;
remove: (uid: string) => void;
}
@observer
export class HotbarEntityIcon extends React.Component<Props> {
@observable.deep private contextMenu: CatalogEntityContextMenuContext;
componentDidMount() {
this.contextMenu = {
menuItems: [],
navigate: (url: string) => navigate(url)
};
}
get kindIcon() {
const className = "badge";
const category = catalogCategoryRegistry.getCategoryForEntity(this.props.entity);
if (!category) {
return <Icon material="bug_report" className={className} />;
}
if (category.metadata.icon.includes("<svg")) {
return <Icon svg={category.metadata.icon} className={className} />;
} else {
return <Icon material={category.metadata.icon} className={className} />;
}
}
get ledIcon() {
const className = cssNames("led", { online: this.props.entity.status.phase == "connected"}); // TODO: make it more generic
return <div className={className} />;
}
isActive(item: CatalogEntity) {
return catalogEntityRegistry.activeEntity?.metadata?.uid == item.getId();
}
onMenuItemClick(menuItem: CatalogEntityContextMenu) {
if (menuItem.confirm) {
ConfirmDialog.open({
okButtonProps: {
primary: false,
accent: true,
},
ok: () => {
menuItem.onClick();
},
message: menuItem.confirm.message
});
} else {
menuItem.onClick();
}
}
generateAvatarStyle(entity: CatalogEntity): React.CSSProperties {
return {
"backgroundColor": randomColor({ seed: `${entity.metadata.name}-${entity.metadata.source}`, luminosity: "dark" })
};
}
render() {
const {
entity, errorClass, remove,
children, ...elemProps
} = this.props;
const className = cssNames("HotbarEntityIcon", this.props.className, {
interactive: true,
active: this.isActive(entity),
disabled: !entity
});
const onOpen = async () => {
await entity.onContextMenuOpen(this.contextMenu);
};
const menuItems = this.contextMenu?.menuItems.filter((menuItem) => !menuItem.onlyVisibleForSource || menuItem.onlyVisibleForSource === entity.metadata.source);
return (
<HotbarIcon
uid={entity.getId()}
title={entity.getName()}
source={`${entity.metadata.source || "local"}`}
className={className}
active={this.isActive(entity)}
remove={remove}
onMenuOpen={onOpen}
menuItems={menuItems}
{...elemProps}
>
{ this.ledIcon }
{ this.kindIcon }
</HotbarIcon>
);
}
}