diff --git a/src/common/hotbar-store.ts b/src/common/hotbar-store.ts index 0faa165bf7..6e771473fb 100644 --- a/src/common/hotbar-store.ts +++ b/src/common/hotbar-store.ts @@ -22,38 +22,17 @@ import { action, comparer, observable, makeObservable } from "mobx"; import { BaseStore } from "./base-store"; import migrations from "../migrations/hotbar-store"; -import * as uuid from "uuid"; import isNull from "lodash/isNull"; import { toJS } from "./utils"; import { CatalogEntity } from "./catalog"; import { catalogEntity } from "../main/catalog-sources/general"; - -export interface HotbarItem { - entity: { - uid: string; - name?: string; - source?: string; - }; - params?: { - [key: string]: string; - } -} - -export type Hotbar = Required; - -export interface HotbarCreateOptions { - id?: string; - name: string; - items?: (HotbarItem | null)[]; -} +import { Hotbar, HotbarCreateOptions, HotbarItem, getEmptyHotbar } from "./hotbar-types"; export interface HotbarStoreModel { hotbars: Hotbar[]; activeHotbarId: string; } -export const defaultHotbarCells = 12; // Number is chosen to easy hit any item with keyboard - export class HotbarStore extends BaseStore { @observable hotbars: Hotbar[] = []; @observable private _activeHotbarId: string; @@ -89,18 +68,16 @@ export class HotbarStore extends BaseStore { return this.hotbarIndex(this.activeHotbarId); } - static getInitialItems() { - return [...Array.from(Array(defaultHotbarCells).fill(null))]; - } - @action protected fromStore(data: Partial = {}) { if (!data.hotbars || !data.hotbars.length) { - this.hotbars = [{ - id: uuid.v4(), - name: "Default", - items: this.defaultHotbarInitialItems, - }]; + const hotbar = getEmptyHotbar("Default"); + const { metadata: { uid, name, source } } = catalogEntity; + const initialItem = { entity: { uid, name, source } }; + + hotbar.items[0] = initialItem; + + this.hotbars = [hotbar]; } else { this.hotbars = data.hotbars; } @@ -116,16 +93,6 @@ export class HotbarStore extends BaseStore { } } - get defaultHotbarInitialItems() { - const { metadata: { uid, name, source } } = catalogEntity; - const initialItem = { entity: { uid, name, source }}; - - return [ - initialItem, - ...Array.from(Array(defaultHotbarCells - 1).fill(null)) - ]; - } - getActive() { return this.getById(this.activeHotbarId); } @@ -140,16 +107,12 @@ export class HotbarStore extends BaseStore { @action add(data: HotbarCreateOptions, { setActive = false } = {}) { - const { - id = uuid.v4(), - items = HotbarStore.getInitialItems(), - name, - } = data; + const hotbar = getEmptyHotbar(data.name, data.id); - this.hotbars.push({ id, name, items }); + this.hotbars.push(hotbar); if (setActive) { - this._activeHotbarId = id; + this._activeHotbarId = hotbar.id; } } diff --git a/src/common/hotbar-types.ts b/src/common/hotbar-types.ts new file mode 100644 index 0000000000..f8fc5a225b --- /dev/null +++ b/src/common/hotbar-types.ts @@ -0,0 +1,52 @@ +/** + * 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 * as uuid from "uuid"; +import type { Tuple } from "./utils"; + +export interface HotbarItem { + entity: { + uid: string; + name?: string; + source?: string; + }; + params?: { + [key: string]: string; + } +} + +export type Hotbar = Required; + +export interface HotbarCreateOptions { + id?: string; + name: string; + items?: Tuple; +} + +export const defaultHotbarCells = 12; // Number is chosen to easy hit any item with keyboard + +export function getEmptyHotbar(name: string, id: string = uuid.v4()): Hotbar { + return { + id, + items: Array(defaultHotbarCells).fill(null) as Tuple, + name, + }; +} diff --git a/src/migrations/hotbar-store/5.0.0-alpha.0.ts b/src/migrations/hotbar-store/5.0.0-alpha.0.ts index 1a61ce8ef3..4ba145b662 100644 --- a/src/migrations/hotbar-store/5.0.0-alpha.0.ts +++ b/src/migrations/hotbar-store/5.0.0-alpha.0.ts @@ -20,20 +20,14 @@ */ // Cleans up a store that had the state related data stored -import { Hotbar, HotbarStore } from "../../common/hotbar-store"; -import * as uuid from "uuid"; import type { MigrationDeclaration } from "../helpers"; import { catalogEntity } from "../../main/catalog-sources/general"; +import { getEmptyHotbar } from "../../common/hotbar-types"; export default { version: "5.0.0-alpha.0", run(store) { - const hotbar: Hotbar = { - id: uuid.v4(), - name: "default", - items: HotbarStore.getInitialItems(), - }; - + const hotbar = getEmptyHotbar("default"); const { metadata: { uid, name, source } } = catalogEntity; hotbar.items[0] = { entity: { uid, name, source } }; diff --git a/src/migrations/hotbar-store/5.0.0-alpha.2.ts b/src/migrations/hotbar-store/5.0.0-alpha.2.ts index e049a6d731..0a662b466a 100644 --- a/src/migrations/hotbar-store/5.0.0-alpha.2.ts +++ b/src/migrations/hotbar-store/5.0.0-alpha.2.ts @@ -20,7 +20,7 @@ */ // Cleans up a store that had the state related data stored -import type { Hotbar } from "../../common/hotbar-store"; +import type { Hotbar } from "../../common/hotbar-types"; import * as uuid from "uuid"; import type { MigrationDeclaration } from "../helpers"; 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 3dd635effc..7d7abc01b7 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.10.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.10.ts @@ -25,7 +25,7 @@ import { isNull } from "lodash"; import path from "path"; import * as uuid from "uuid"; import type { ClusterStoreModel } from "../../common/cluster-store"; -import { defaultHotbarCells, Hotbar, HotbarStore } from "../../common/hotbar-store"; +import { defaultHotbarCells, getEmptyHotbar, Hotbar, HotbarItem } from "../../common/hotbar-types"; import { catalogEntity } from "../../main/catalog-sources/general"; import { MigrationDeclaration, migrationLog } from "../helpers"; import { generateNewIdFor } from "../utils"; @@ -37,6 +37,12 @@ interface Pre500WorkspaceStoreModel { }[]; } +interface PartialHotbar { + id: string; + name: string; + items: (null | HotbarItem)[]; +} + export default { version: "5.0.0-beta.10", run(store) { @@ -46,7 +52,7 @@ export default { try { const workspaceStoreData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json")); const { clusters }: ClusterStoreModel = fse.readJSONSync(path.join(userDataPath, "lens-cluster-store.json")); - const workspaceHotbars = new Map(); // mapping from WorkspaceId to HotBar + const workspaceHotbars = new Map(); // mapping from WorkspaceId to HotBar for (const { id, name } of workspaceStoreData.workspaces) { migrationLog(`Creating new hotbar for ${name}`); @@ -103,7 +109,7 @@ export default { hotbar.items.push(null); } - hotbars.push(hotbar); + hotbars.push(hotbar as Hotbar); } /** @@ -123,11 +129,7 @@ export default { if (freeIndex === -1) { // making a new hotbar is less destructive if the first hotbar // called "default" is full than overriding a hotbar item - const hotbar = { - id: uuid.v4(), - name: "initial", - items: HotbarStore.getInitialItems(), - }; + const hotbar = getEmptyHotbar("initial"); hotbar.items[0] = { entity: { uid, name, source } }; hotbars.unshift(hotbar); @@ -135,11 +137,7 @@ export default { defaultHotbar.items[freeIndex] = { entity: { uid, name, source } }; } } else { - const hotbar = { - id: uuid.v4(), - name: "default", - items: HotbarStore.getInitialItems(), - }; + const hotbar = getEmptyHotbar("default"); hotbar.items[0] = { entity: { uid, name, source } }; hotbars.unshift(hotbar); diff --git a/src/migrations/hotbar-store/5.0.0-beta.5.ts b/src/migrations/hotbar-store/5.0.0-beta.5.ts index 8d589e9543..f9cfd77262 100644 --- a/src/migrations/hotbar-store/5.0.0-beta.5.ts +++ b/src/migrations/hotbar-store/5.0.0-beta.5.ts @@ -19,7 +19,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import type { Hotbar } from "../../common/hotbar-store"; +import type { Hotbar } from "../../common/hotbar-types"; import { catalogEntityRegistry } from "../../main/catalog"; import type { MigrationDeclaration } from "../helpers"; diff --git a/src/renderer/components/hotbar/hotbar-menu.tsx b/src/renderer/components/hotbar/hotbar-menu.tsx index 56fa7a25fc..5df1f48049 100644 --- a/src/renderer/components/hotbar/hotbar-menu.tsx +++ b/src/renderer/components/hotbar/hotbar-menu.tsx @@ -26,13 +26,14 @@ import { observer } from "mobx-react"; import { HotbarEntityIcon } from "./hotbar-entity-icon"; import { cssNames, IClassName } from "../../utils"; import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; -import { defaultHotbarCells, HotbarItem, HotbarStore } from "../../../common/hotbar-store"; +import { HotbarStore } from "../../../common/hotbar-store"; import { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity"; import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd"; import { HotbarSelector } from "./hotbar-selector"; import { HotbarCell } from "./hotbar-cell"; import { HotbarIcon } from "./hotbar-icon"; import { computed } from "mobx"; +import { defaultHotbarCells, HotbarItem } from "../../../common/hotbar-types"; interface Props { className?: IClassName; diff --git a/src/renderer/components/hotbar/hotbar-selector.tsx b/src/renderer/components/hotbar/hotbar-selector.tsx index cc9617aa47..0144f2befd 100644 --- a/src/renderer/components/hotbar/hotbar-selector.tsx +++ b/src/renderer/components/hotbar/hotbar-selector.tsx @@ -23,12 +23,13 @@ import "./hotbar-selector.scss"; import React from "react"; import { Icon } from "../icon"; import { Badge } from "../badge"; -import { Hotbar, HotbarStore } from "../../../common/hotbar-store"; +import { HotbarStore } from "../../../common/hotbar-store"; import { CommandOverlay } from "../command-palette"; import { HotbarSwitchCommand } from "./hotbar-switch-command"; import { hotbarDisplayIndex } from "./hotbar-display-label"; import { TooltipPosition } from "../tooltip"; import { observer } from "mobx-react"; +import type { Hotbar } from "../../../common/hotbar-types"; interface Props { hotbar: Hotbar;