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

Do a final check on making sure that the catalog hotbar item is in place

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-06-24 15:31:58 -04:00
parent b9cb18c7ce
commit f13daf68a1
3 changed files with 58 additions and 26 deletions

View File

@ -38,16 +38,12 @@ export interface HotbarItem {
}
}
export interface Hotbar {
id: string;
name: string;
items: HotbarItem[];
}
export type Hotbar = Required<HotbarCreateOptions>;
export interface HotbarCreateOptions {
id?: string;
name: string;
items?: HotbarItem[];
items?: (HotbarItem | null)[];
}
export interface HotbarStoreModel {
@ -123,18 +119,19 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
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

View File

@ -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"))) {

View File

@ -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}
/>
<small className="hint">
Please provide a new hotbar name (Press &quot;Enter&quot; to confirm or &quot;Escape&quot; to cancel)
</small>