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

Move Hotbar types to seperate file (#3532)

This commit is contained in:
Sebastian Malton 2021-08-03 14:38:36 -04:00 committed by GitHub
parent 77077ef72c
commit 3fe8014f11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 82 additions and 73 deletions

View File

@ -22,38 +22,17 @@
import { action, comparer, observable, makeObservable } from "mobx"; import { action, comparer, observable, makeObservable } from "mobx";
import { BaseStore } from "./base-store"; import { BaseStore } from "./base-store";
import migrations from "../migrations/hotbar-store"; import migrations from "../migrations/hotbar-store";
import * as uuid from "uuid";
import isNull from "lodash/isNull"; import isNull from "lodash/isNull";
import { toJS } from "./utils"; import { toJS } from "./utils";
import { CatalogEntity } from "./catalog"; import { CatalogEntity } from "./catalog";
import { catalogEntity } from "../main/catalog-sources/general"; import { catalogEntity } from "../main/catalog-sources/general";
import { Hotbar, HotbarCreateOptions, HotbarItem, getEmptyHotbar } from "./hotbar-types";
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)[];
}
export interface HotbarStoreModel { export interface HotbarStoreModel {
hotbars: Hotbar[]; hotbars: Hotbar[];
activeHotbarId: string; activeHotbarId: string;
} }
export const defaultHotbarCells = 12; // Number is chosen to easy hit any item with keyboard
export class HotbarStore extends BaseStore<HotbarStoreModel> { export class HotbarStore extends BaseStore<HotbarStoreModel> {
@observable hotbars: Hotbar[] = []; @observable hotbars: Hotbar[] = [];
@observable private _activeHotbarId: string; @observable private _activeHotbarId: string;
@ -89,18 +68,16 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
return this.hotbarIndex(this.activeHotbarId); return this.hotbarIndex(this.activeHotbarId);
} }
static getInitialItems() {
return [...Array.from(Array(defaultHotbarCells).fill(null))];
}
@action @action
protected fromStore(data: Partial<HotbarStoreModel> = {}) { protected fromStore(data: Partial<HotbarStoreModel> = {}) {
if (!data.hotbars || !data.hotbars.length) { if (!data.hotbars || !data.hotbars.length) {
this.hotbars = [{ const hotbar = getEmptyHotbar("Default");
id: uuid.v4(), const { metadata: { uid, name, source } } = catalogEntity;
name: "Default", const initialItem = { entity: { uid, name, source } };
items: this.defaultHotbarInitialItems,
}]; hotbar.items[0] = initialItem;
this.hotbars = [hotbar];
} else { } else {
this.hotbars = data.hotbars; this.hotbars = data.hotbars;
} }
@ -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() { getActive() {
return this.getById(this.activeHotbarId); return this.getById(this.activeHotbarId);
} }
@ -140,16 +107,12 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
@action @action
add(data: HotbarCreateOptions, { setActive = false } = {}) { add(data: HotbarCreateOptions, { setActive = false } = {}) {
const { const hotbar = getEmptyHotbar(data.name, data.id);
id = uuid.v4(),
items = HotbarStore.getInitialItems(),
name,
} = data;
this.hotbars.push({ id, name, items }); this.hotbars.push(hotbar);
if (setActive) { if (setActive) {
this._activeHotbarId = id; this._activeHotbarId = hotbar.id;
} }
} }

View File

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

View File

@ -20,20 +20,14 @@
*/ */
// Cleans up a store that had the state related data stored // 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 type { MigrationDeclaration } from "../helpers";
import { catalogEntity } from "../../main/catalog-sources/general"; import { catalogEntity } from "../../main/catalog-sources/general";
import { getEmptyHotbar } from "../../common/hotbar-types";
export default { export default {
version: "5.0.0-alpha.0", version: "5.0.0-alpha.0",
run(store) { run(store) {
const hotbar: Hotbar = { const hotbar = getEmptyHotbar("default");
id: uuid.v4(),
name: "default",
items: HotbarStore.getInitialItems(),
};
const { metadata: { uid, name, source } } = catalogEntity; const { metadata: { uid, name, source } } = catalogEntity;
hotbar.items[0] = { entity: { uid, name, source } }; hotbar.items[0] = { entity: { uid, name, source } };

View File

@ -20,7 +20,7 @@
*/ */
// Cleans up a store that had the state related data stored // 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 * as uuid from "uuid";
import type { MigrationDeclaration } from "../helpers"; import type { MigrationDeclaration } from "../helpers";

View File

@ -25,7 +25,7 @@ import { isNull } from "lodash";
import path from "path"; import path from "path";
import * as uuid from "uuid"; import * as uuid from "uuid";
import type { ClusterStoreModel } from "../../common/cluster-store"; 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 { catalogEntity } from "../../main/catalog-sources/general";
import { MigrationDeclaration, migrationLog } from "../helpers"; import { MigrationDeclaration, migrationLog } from "../helpers";
import { generateNewIdFor } from "../utils"; import { generateNewIdFor } from "../utils";
@ -37,6 +37,12 @@ interface Pre500WorkspaceStoreModel {
}[]; }[];
} }
interface PartialHotbar {
id: string;
name: string;
items: (null | HotbarItem)[];
}
export default { export default {
version: "5.0.0-beta.10", version: "5.0.0-beta.10",
run(store) { run(store) {
@ -46,7 +52,7 @@ export default {
try { try {
const workspaceStoreData: Pre500WorkspaceStoreModel = fse.readJsonSync(path.join(userDataPath, "lens-workspace-store.json")); 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 { 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) { for (const { id, name } of workspaceStoreData.workspaces) {
migrationLog(`Creating new hotbar for ${name}`); migrationLog(`Creating new hotbar for ${name}`);
@ -103,7 +109,7 @@ export default {
hotbar.items.push(null); hotbar.items.push(null);
} }
hotbars.push(hotbar); hotbars.push(hotbar as Hotbar);
} }
/** /**
@ -123,11 +129,7 @@ export default {
if (freeIndex === -1) { if (freeIndex === -1) {
// making a new hotbar is less destructive if the first hotbar // making a new hotbar is less destructive if the first hotbar
// called "default" is full than overriding a hotbar item // called "default" is full than overriding a hotbar item
const hotbar = { const hotbar = getEmptyHotbar("initial");
id: uuid.v4(),
name: "initial",
items: HotbarStore.getInitialItems(),
};
hotbar.items[0] = { entity: { uid, name, source } }; hotbar.items[0] = { entity: { uid, name, source } };
hotbars.unshift(hotbar); hotbars.unshift(hotbar);
@ -135,11 +137,7 @@ export default {
defaultHotbar.items[freeIndex] = { entity: { uid, name, source } }; defaultHotbar.items[freeIndex] = { entity: { uid, name, source } };
} }
} else { } else {
const hotbar = { const hotbar = getEmptyHotbar("default");
id: uuid.v4(),
name: "default",
items: HotbarStore.getInitialItems(),
};
hotbar.items[0] = { entity: { uid, name, source } }; hotbar.items[0] = { entity: { uid, name, source } };
hotbars.unshift(hotbar); hotbars.unshift(hotbar);

View File

@ -19,7 +19,7 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 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 { catalogEntityRegistry } from "../../main/catalog";
import type { MigrationDeclaration } from "../helpers"; import type { MigrationDeclaration } from "../helpers";

View File

@ -26,13 +26,14 @@ import { observer } from "mobx-react";
import { HotbarEntityIcon } from "./hotbar-entity-icon"; import { HotbarEntityIcon } from "./hotbar-entity-icon";
import { cssNames, IClassName } from "../../utils"; import { cssNames, IClassName } from "../../utils";
import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; 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 { CatalogEntity, catalogEntityRunContext } from "../../api/catalog-entity";
import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd"; import { DragDropContext, Draggable, Droppable, DropResult } from "react-beautiful-dnd";
import { HotbarSelector } from "./hotbar-selector"; import { HotbarSelector } from "./hotbar-selector";
import { HotbarCell } from "./hotbar-cell"; import { HotbarCell } from "./hotbar-cell";
import { HotbarIcon } from "./hotbar-icon"; import { HotbarIcon } from "./hotbar-icon";
import { computed } from "mobx"; import { computed } from "mobx";
import { defaultHotbarCells, HotbarItem } from "../../../common/hotbar-types";
interface Props { interface Props {
className?: IClassName; className?: IClassName;

View File

@ -23,12 +23,13 @@ import "./hotbar-selector.scss";
import React from "react"; import React from "react";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { Badge } from "../badge"; import { Badge } from "../badge";
import { Hotbar, HotbarStore } from "../../../common/hotbar-store"; import { HotbarStore } from "../../../common/hotbar-store";
import { CommandOverlay } from "../command-palette"; import { CommandOverlay } from "../command-palette";
import { HotbarSwitchCommand } from "./hotbar-switch-command"; import { HotbarSwitchCommand } from "./hotbar-switch-command";
import { hotbarDisplayIndex } from "./hotbar-display-label"; import { hotbarDisplayIndex } from "./hotbar-display-label";
import { TooltipPosition } from "../tooltip"; import { TooltipPosition } from "../tooltip";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import type { Hotbar } from "../../../common/hotbar-types";
interface Props { interface Props {
hotbar: Hotbar; hotbar: Hotbar;