import "./hotbar-icon.scss"; import React, { DOMAttributes } from "react"; import { observer } from "mobx-react"; import { cssNames, IClassName } from "../../utils"; import { Tooltip } from "../tooltip"; import { Avatar } from "@material-ui/core"; import { CatalogEntity, CatalogEntityContextMenu, CatalogEntityContextMenuContext } from "../../../common/catalog-entity"; import { Menu, MenuItem } from "../menu"; import { Icon } from "../icon"; import { observable } from "mobx"; import { navigate } from "../../navigation"; import { HotbarStore } from "../../../common/hotbar-store"; import { ConfirmDialog } from "../confirm-dialog"; interface Props extends DOMAttributes { entity: CatalogEntity; index: number; className?: IClassName; errorClass?: IClassName; isActive?: boolean; } @observer export class HotbarIcon extends React.Component { @observable.deep private contextMenu: CatalogEntityContextMenuContext; @observable menuOpen = false; componentDidMount() { this.contextMenu = { menuItems: [], navigate: (url: string) => navigate(url) }; } get iconString() { let splittedName = this.props.entity.metadata.name.split(" "); if (splittedName.length === 1) { splittedName = splittedName[0].split("-"); } if (splittedName.length === 1) { splittedName = splittedName[0].split("@"); } splittedName = splittedName.map((part) => part.replace(/\W/g, "")); if (splittedName.length === 1) { return splittedName[0].substring(0, 2); } else if (splittedName.length === 2) { return splittedName[0].substring(0, 1) + splittedName[1].substring(0, 1); } else { return splittedName[0].substring(0, 1) + splittedName[1].substring(0, 1) + splittedName[2].substring(0, 1); } } toggleMenu() { this.menuOpen = !this.menuOpen; } removeFromHotbar(item: CatalogEntity) { const hotbar = HotbarStore.getInstance().getByName("default"); // FIXME if (!hotbar) { return; } hotbar.items = hotbar.items.filter((i) => i.entity.uid !== item.metadata.uid); } onMenuItemClick(menuItem: CatalogEntityContextMenu) { if (menuItem.confirm) { ConfirmDialog.open({ okButtonProps: { primary: false, accent: true, }, ok: () => { menuItem.onClick(); }, message: menuItem.confirm.message }); } else { menuItem.onClick(); } } render() { const { entity, errorClass, isActive, children, ...elemProps } = this.props; const entityIconId = `hotbar-icon-${this.props.index}`; const className = cssNames("HotbarIcon flex inline", this.props.className, { interactive: true, active: isActive, }); const onOpen = async () => { await entity.onContextMenuOpen(this.contextMenu); this.toggleMenu(); }; const menuItems = this.contextMenu?.menuItems.filter((menuItem) => !menuItem.onlyVisibleForSource || menuItem.onlyVisibleForSource === entity.metadata.source); return (
{entity.metadata.name} {this.iconString} onOpen()} close={() => this.toggleMenu()}> this.removeFromHotbar(entity) }> Remove from Hotbar { this.contextMenu && menuItems.map((menuItem) => { return ( this.onMenuItemClick(menuItem) }> {menuItem.title} ); })} {children}
); } }