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
Sebastian Malton 4d05eff051 Split Catalog Categories and Registries
- Main and Renderer now have different types
- No longer have unified class declarations
- Move towards a computed model on main, with the CatalogEntityRegistry
  folding over the declared handlers

Signed-off-by: Sebastian Malton <sebastian@malton.name>

finish design work, still doesn't compile

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2021-05-25 15:09:26 -04:00

116 lines
4.0 KiB
TypeScript

/**
* 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, { DOMAttributes } from "react";
import { makeObservable, observable } from "mobx";
import { observer } from "mobx-react";
import { CatalogCategoryRegistry, CatalogEntity, CatalogEntityRegistry, TransformedMenuItem } from "../../catalog";
import { cssNames, IClassName } from "../../utils";
import { Icon } from "../icon";
import { HotbarIcon } from "./hotbar-icon";
import { HotbarStore } from "../../../common/hotbar-store";
interface Props extends DOMAttributes<HTMLElement> {
entity: CatalogEntity;
index: number;
className?: IClassName;
errorClass?: IClassName;
add: (item: CatalogEntity, index: number) => void;
remove: (uid: string) => void;
}
@observer
export class HotbarEntityIcon extends React.Component<Props> {
menuItems = observable.array<TransformedMenuItem>();
constructor(props: Props) {
super(props);
makeObservable(this);
}
get kindIcon() {
const className = "badge";
const category = CatalogCategoryRegistry.getInstance().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} />;
}
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.getInstance().activeEntity?.metadata?.uid == item.id;
}
isPersisted(entity: CatalogEntity) {
return HotbarStore.getInstance().getActive().items.find((item) => item?.entity?.uid === entity.metadata.uid) !== undefined;
}
render() {
const {
entity, errorClass, add, remove,
index, children, ...elemProps
} = this.props;
const className = cssNames("HotbarEntityIcon", this.props.className, {
interactive: true,
active: this.isActive(entity),
disabled: !entity
});
const isActive = this.isActive(entity);
const persistAction = this.isPersisted(entity)
? ({
title: "Pin to Hotbar",
onClick: () => add(entity, index)
})
: ({
title: "Unpin from Hotbar",
onClick: () => remove(entity.metadata.uid)
});
return (
<HotbarIcon
uid={entity.metadata.uid}
title={entity.metadata.name}
source={entity.metadata.source}
className={className}
active={isActive}
onMenuOpen={() => this.menuItems.replace(CatalogCategoryRegistry.getInstance().runEntityHandlersFor(entity, "onContextMenuOpen"))}
menuItems={[...this.menuItems, persistAction]}
{...elemProps}
>
{ this.ledIcon }
{ this.kindIcon }
</HotbarIcon>
);
}
}