mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
72 lines
1.5 KiB
TypeScript
72 lines
1.5 KiB
TypeScript
import { action, comparer, observable, toJS } from "mobx";
|
|
import { BaseStore } from "./base-store";
|
|
import migrations from "../migrations/hotbar-store";
|
|
|
|
export interface HotbarItem {
|
|
entity: {
|
|
uid: string;
|
|
};
|
|
params?: {
|
|
[key: string]: string;
|
|
}
|
|
}
|
|
|
|
export interface Hotbar {
|
|
name: string;
|
|
items: HotbarItem[];
|
|
}
|
|
|
|
export interface HotbarStoreModel {
|
|
hotbars: Hotbar[];
|
|
}
|
|
|
|
export class HotbarStore extends BaseStore<HotbarStoreModel> {
|
|
@observable hotbars: Hotbar[] = [];
|
|
|
|
private constructor() {
|
|
super({
|
|
configName: "lens-hotbar-store",
|
|
accessPropertiesByDotNotation: false, // To make dots safe in cluster context names
|
|
syncOptions: {
|
|
equals: comparer.structural,
|
|
},
|
|
migrations,
|
|
});
|
|
}
|
|
|
|
@action protected async fromStore(data: Partial<HotbarStoreModel> = {}) {
|
|
if (data.hotbars?.length === 0) {
|
|
this.hotbars = [{
|
|
name: "default",
|
|
items: []
|
|
}];
|
|
} else {
|
|
this.hotbars = data.hotbars;
|
|
}
|
|
}
|
|
|
|
getByName(name: string) {
|
|
return this.hotbars.find((hotbar) => hotbar.name === name);
|
|
}
|
|
|
|
add(hotbar: Hotbar) {
|
|
this.hotbars.push(hotbar);
|
|
}
|
|
|
|
remove(hotbar: Hotbar) {
|
|
this.hotbars = this.hotbars.filter((h) => h !== hotbar);
|
|
}
|
|
|
|
toJSON(): HotbarStoreModel {
|
|
const model: HotbarStoreModel = {
|
|
hotbars: this.hotbars
|
|
};
|
|
|
|
return toJS(model, {
|
|
recurseEverything: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
export const hotbarStore = HotbarStore.getInstance<HotbarStore>();
|