Skip to content

Commit

Permalink
Move Hotbar types to seperate file
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <[email protected]>
  • Loading branch information
Nokel81 committed Aug 3, 2021
1 parent a6e91fc commit db90f81
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 73 deletions.
59 changes: 11 additions & 48 deletions src/common/hotbar-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HotbarCreateOptions>;

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<HotbarStoreModel> {
@observable hotbars: Hotbar[] = [];
@observable private _activeHotbarId: string;
Expand Down Expand Up @@ -89,18 +68,16 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
return this.hotbarIndex(this.activeHotbarId);
}

static getInitialItems() {
return [...Array.from(Array(defaultHotbarCells).fill(null))];
}

@action
protected fromStore(data: Partial<HotbarStoreModel> = {}) {
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;
}
Expand All @@ -116,16 +93,6 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
}
}

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);
}
Expand All @@ -140,16 +107,12 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {

@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;
}
}

Expand Down
52 changes: 52 additions & 0 deletions src/common/hotbar-types.ts
Original file line number Diff line number Diff line change
@@ -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<HotbarCreateOptions>;

export interface HotbarCreateOptions {
id?: string;
name: string;
items?: Tuple<HotbarItem | null, typeof defaultHotbarCells>;
}

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<HotbarItem | null, typeof defaultHotbarCells>,
name,
};
}
10 changes: 2 additions & 8 deletions src/migrations/hotbar-store/5.0.0-alpha.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } };
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/hotbar-store/5.0.0-alpha.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
24 changes: 11 additions & 13 deletions src/migrations/hotbar-store/5.0.0-beta.10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand All @@ -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<string, Hotbar>(); // mapping from WorkspaceId to HotBar
const workspaceHotbars = new Map<string, PartialHotbar>(); // mapping from WorkspaceId to HotBar

for (const { id, name } of workspaceStoreData.workspaces) {
migrationLog(`Creating new hotbar for ${name}`);
Expand Down Expand Up @@ -103,7 +109,7 @@ export default {
hotbar.items.push(null);
}

hotbars.push(hotbar);
hotbars.push(hotbar as Hotbar);
}

/**
Expand All @@ -123,23 +129,15 @@ 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);
} else {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/hotbar-store/5.0.0-beta.5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/hotbar/hotbar-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/hotbar/hotbar-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit db90f81

Please sign in to comment.