diff --git a/src/common/hotbar-store.ts b/src/common/hotbar-store.ts index 062a947557..b90a007b34 100644 --- a/src/common/hotbar-store.ts +++ b/src/common/hotbar-store.ts @@ -38,16 +38,12 @@ export interface HotbarItem { } } -export interface Hotbar { - id: string; - name: string; - items: HotbarItem[]; -} +export type Hotbar = Required; export interface HotbarCreateOptions { id?: string; name: string; - items?: HotbarItem[]; + items?: (HotbarItem | null)[]; } export interface HotbarStoreModel { @@ -123,18 +119,19 @@ export class HotbarStore extends BaseStore { return this.hotbars.find((hotbar) => hotbar.id === id); } - add(data: HotbarCreateOptions) { + @action + add(data: HotbarCreateOptions, { setActive = false } = {}) { const { id = uuid.v4(), items = HotbarStore.getInitialItems(), name, } = data; - const hotbar = { id, name, items }; + this.hotbars.push({ id, name, items }); - this.hotbars.push(hotbar as Hotbar); - - return hotbar as Hotbar; + if (setActive) { + this._activeHotbarId = id; + } } @action diff --git a/src/migrations/hotbar-store/5.0.0-beta.10.ts b/src/migrations/hotbar-store/5.0.0-beta.10.ts index 4150038e8e..d17f1448db 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.10.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.10.ts @@ -22,10 +22,12 @@ import { createHash } from "crypto"; import { app } from "electron"; import fse from "fs-extra"; +import { isNull } from "lodash"; import path from "path"; import * as uuid from "uuid"; import type { ClusterStoreModel } from "../../common/cluster-store"; -import { defaultHotbarCells, Hotbar } from "../../common/hotbar-store"; +import { defaultHotbarCells, Hotbar, HotbarStore } from "../../common/hotbar-store"; +import { catalogEntity } from "../../main/catalog-sources/general"; import type { MigrationDeclaration } from "../helpers"; interface Pre500WorkspaceStoreModel { @@ -75,6 +77,46 @@ export default { hotbars.push(hotbar); } + /** + * Finally, make sure that the catalog entity hotbar item is in place. + * Just in case something else removed it. + * + * if every hotbar hasĀ elements that all not the `catalog-entity` item + */ + if (hotbars.every(hotbar => hotbar.items.every(item => item?.entity?.uid !== "catalog-entity"))) { + // note, we will add a new whole hotbar here called "default" if that was previously removed + const defaultHotbar = hotbars.find(hotbar => hotbar.name === "default"); + const { metadata: { uid, name, source } } = catalogEntity; + + if (defaultHotbar) { + const freeIndex = defaultHotbar.items.findIndex(isNull); + + if (freeIndex === -1) { + // making a new hotbar is less destructive if the first hotbar called default + // is full then overriding a previously pinned item + const hotbar = { + id: uuid.v4(), + name: "initial", + items: HotbarStore.getInitialItems(), + }; + + hotbar.items[0] = { entity: { uid, name, source } }; + hotbars.unshift(hotbar); + } else { + defaultHotbar.items[freeIndex] = { entity: { uid, name, source } }; + } + } else { + const hotbar = { + id: uuid.v4(), + name: "default", + items: HotbarStore.getInitialItems(), + }; + + hotbar.items[0] = { entity: { uid, name, source } }; + hotbars.unshift(hotbar); + } + } + store.set("hotbars", hotbars); } catch (error) { if (!(error.code === "ENOENT" && error.path.endsWith("lens-workspace-store.json"))) { diff --git a/src/renderer/components/hotbar/hotbar-add-command.tsx b/src/renderer/components/hotbar/hotbar-add-command.tsx index 8141594b7c..fde97ef561 100644 --- a/src/renderer/components/hotbar/hotbar-add-command.tsx +++ b/src/renderer/components/hotbar/hotbar-add-command.tsx @@ -33,22 +33,14 @@ const uniqueHotbarName: InputValidator = { @observer export class HotbarAddCommand extends React.Component { - - onSubmit(name: string) { + onSubmit = (name: string) => { if (!name.trim()) { return; } - const hotbarStore = HotbarStore.getInstance(); - - const hotbar = hotbarStore.add({ - name - }); - - hotbarStore.activeHotbarId = hotbar.id; - + HotbarStore.getInstance().add({ name }, { setActive: true }); CommandOverlay.close(); - } + }; render() { return ( @@ -58,10 +50,11 @@ export class HotbarAddCommand extends React.Component { autoFocus={true} theme="round-black" data-test-id="command-palette-hotbar-add-name" - validators={[uniqueHotbarName]} - onSubmit={(v) => this.onSubmit(v)} + validators={uniqueHotbarName} + onSubmit={this.onSubmit} dirty={true} - showValidationLine={true} /> + showValidationLine={true} + /> Please provide a new hotbar name (Press "Enter" to confirm or "Escape" to cancel)