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

Toggle add/remove items to hotbar

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-10-11 10:23:27 +03:00
parent e990c721e4
commit 2f1d32b47e
7 changed files with 46 additions and 42 deletions

View File

@ -330,8 +330,8 @@ describe("HotbarStore", () => {
hotbarStore.addToHotbar(testCluster); hotbarStore.addToHotbar(testCluster);
expect(hotbarStore.entityPinnedToHotbar(testCluster)).toBeTruthy(); expect(hotbarStore.isAddedToActive(testCluster)).toBeTruthy();
expect(hotbarStore.entityPinnedToHotbar(awsCluster)).toBeFalsy(); expect(hotbarStore.isAddedToActive(awsCluster)).toBeFalsy();
}); });
}); });

View File

@ -171,7 +171,7 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
}}; }};
if (this.entityPinnedToHotbar(item)) { if (this.isAddedToActive(item)) {
return; return;
} }
@ -279,7 +279,7 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
* Checks if entity already pinned to hotbar * Checks if entity already pinned to hotbar
* @returns boolean * @returns boolean
*/ */
entityPinnedToHotbar(entity: CatalogEntity) { isAddedToActive(entity: CatalogEntity) {
return !!this.getActive().items.find(item => item?.entity.uid === entity.metadata.uid); return !!this.getActive().items.find(item => item?.entity.uid === entity.metadata.uid);
} }
} }

View File

@ -74,6 +74,10 @@ export class CatalogEntityDrawerMenu<T extends CatalogEntity> extends React.Comp
HotbarStore.getInstance().addToHotbar(entity); HotbarStore.getInstance().addToHotbar(entity);
} }
removeFromHotbar(item: CatalogEntity): void {
HotbarStore.getInstance().removeFromHotbar(item.getId());
}
getMenuItems(entity: T): React.ReactChild[] { getMenuItems(entity: T): React.ReactChild[] {
if (!entity) { if (!entity) {
return []; return [];
@ -98,11 +102,19 @@ export class CatalogEntityDrawerMenu<T extends CatalogEntity> extends React.Comp
); );
} }
items.push( if (!HotbarStore.getInstance().isAddedToActive(entity)) {
<MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(entity) }> items.push(
<Icon material="push_pin" small tooltip="Add to Hotbar" /> <MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(entity) }>
</MenuItem> <Icon material="push_pin" small tooltip="Add to Hotbar" />
); </MenuItem>
);
} else {
items.push(
<MenuItem key="remove-from-hotbar" onClick={() => this.removeFromHotbar(entity)}>
<Icon material="push_pin" small tooltip="Remove from Hotbar" />
</MenuItem>
);
}
return items; return items;
} }

View File

@ -44,10 +44,6 @@
&:hover .pinIcon { &:hover .pinIcon {
opacity: 1; opacity: 1;
&:global(.disabled) {
opacity: 0.3;
}
} }
} }

View File

@ -37,7 +37,7 @@ import { CatalogAddButton } from "./catalog-add-button";
import type { RouteComponentProps } from "react-router"; import type { RouteComponentProps } from "react-router";
import { Notifications } from "../notifications"; import { Notifications } from "../notifications";
import { MainLayout } from "../layout/main-layout"; import { MainLayout } from "../layout/main-layout";
import { createAppStorage, cssNames } from "../../utils"; import { createAppStorage, cssNames, prevDefault } from "../../utils";
import { makeCss } from "../../../common/utils/makeCss"; import { makeCss } from "../../../common/utils/makeCss";
import { CatalogEntityDetails } from "./catalog-entity-details"; import { CatalogEntityDetails } from "./catalog-entity-details";
import { browseCatalogTab, catalogURL, CatalogViewRouteParam } from "../../../common/routes"; import { browseCatalogTab, catalogURL, CatalogViewRouteParam } from "../../../common/routes";
@ -130,6 +130,10 @@ export class Catalog extends React.Component<Props> {
HotbarStore.getInstance().addToHotbar(item.entity); HotbarStore.getInstance().addToHotbar(item.entity);
} }
removeFromHotbar(item: CatalogEntityItem<CatalogEntity>): void {
HotbarStore.getInstance().removeFromHotbar(item.getId());
}
onDetails = (item: CatalogEntityItem<CatalogEntity>) => { onDetails = (item: CatalogEntityItem<CatalogEntity>) => {
if (this.catalogEntityStore.selectedItemId) { if (this.catalogEntityStore.selectedItemId) {
this.catalogEntityStore.selectedItemId = null; this.catalogEntityStore.selectedItemId = null;
@ -177,6 +181,8 @@ export class Catalog extends React.Component<Props> {
} }
renderItemMenu = (item: CatalogEntityItem<CatalogEntity>) => { renderItemMenu = (item: CatalogEntityItem<CatalogEntity>) => {
const isItemInHotbar = HotbarStore.getInstance().isAddedToActive(item.entity);
const onOpen = () => { const onOpen = () => {
this.contextMenu.menuItems = []; this.contextMenu.menuItems = [];
@ -195,29 +201,32 @@ export class Catalog extends React.Component<Props> {
</MenuItem> </MenuItem>
)) ))
} }
<MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item)}> {!isItemInHotbar ? (
Pin to Hotbar <MenuItem key="add-to-hotbar" onClick={() => this.addToHotbar(item)}>
</MenuItem> Add to Hotbar
</MenuItem>
) : (
<MenuItem key="remove-from-hotbar" onClick={() => this.removeFromHotbar(item)}>
Remove from Hotbar
</MenuItem>
)}
</MenuActions> </MenuActions>
); );
}; };
renderName(item: CatalogEntityItem<CatalogEntity>) { renderName(item: CatalogEntityItem<CatalogEntity>) {
const disabledIcon = HotbarStore.getInstance().entityPinnedToHotbar(item.entity); const isItemInHotbar = HotbarStore.getInstance().isAddedToActive(item.entity);
return ( return (
<div className={styles.entityName}> <div className={styles.entityName}>
{item.name} {item.name}
<Icon <Icon
small small
disabled={disabledIcon}
className={styles.pinIcon} className={styles.pinIcon}
material="push_pin" material="push_pin"
tooltip={disabledIcon ? "" : "Pin to Hotbar"} tooltip={isItemInHotbar ? "Remove from Hotbar" : "Add to Hotbar"}
onClick={(event) => { onClick={prevDefault(() => isItemInHotbar ? this.removeFromHotbar(item) : this.addToHotbar(item))}
event.stopPropagation(); />
this.addToHotbar(item);
}}/>
</div> </div>
); );
} }

View File

@ -30,7 +30,6 @@ import { navigate } from "../../navigation";
import { cssNames, IClassName } from "../../utils"; import { cssNames, IClassName } from "../../utils";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { HotbarIcon } from "./hotbar-icon"; import { HotbarIcon } from "./hotbar-icon";
import { HotbarStore } from "../../../common/hotbar-store";
interface Props extends DOMAttributes<HTMLElement> { interface Props extends DOMAttributes<HTMLElement> {
entity: CatalogEntity; entity: CatalogEntity;
@ -87,10 +86,6 @@ export class HotbarEntityIcon extends React.Component<Props> {
return catalogEntityRegistry.activeEntity?.metadata?.uid == item.getId(); return catalogEntityRegistry.activeEntity?.metadata?.uid == item.getId();
} }
isPersisted(entity: CatalogEntity) {
return HotbarStore.getInstance().getActive().items.find((item) => item?.entity?.uid === entity.metadata.uid) !== undefined;
}
render() { render() {
if (!this.contextMenu) { if (!this.contextMenu) {
return null; return null;
@ -106,21 +101,13 @@ export class HotbarEntityIcon extends React.Component<Props> {
disabled: !entity disabled: !entity
}); });
const isPersisted = this.isPersisted(entity);
const onOpen = async () => { const onOpen = async () => {
const menuItems: CatalogEntityContextMenu[] = []; const menuItems: CatalogEntityContextMenu[] = [];
if (!isPersisted) { menuItems.unshift({
menuItems.unshift({ title: "Remove from Hotbar",
title: "Pin to Hotbar", onClick: () => remove(entity.metadata.uid)
onClick: () => add(entity, index) });
});
} else {
menuItems.unshift({
title: "Unpin from Hotbar",
onClick: () => remove(entity.metadata.uid)
});
}
this.contextMenu.menuItems = menuItems; this.contextMenu.menuItems = menuItems;

View File

@ -138,7 +138,7 @@ export class HotbarMenu extends React.Component<Props> {
tooltip={`${item.entity.name} (${item.entity.source})`} tooltip={`${item.entity.name} (${item.entity.source})`}
menuItems={[ menuItems={[
{ {
title: "Unpin from Hotbar", title: "Remove from Hotbar",
onClick: () => this.removeItem(item.entity.uid) onClick: () => this.removeItem(item.entity.uid)
} }
]} ]}