import { action, comparer, computed, observable, toJS } from "mobx"; import { BaseStore } from "./base-store"; import migrations from "../migrations/hotbar-store"; import { v4 as uuid } from "uuid"; export interface HotbarItem { entity: { uid: string; }; params?: { [key: string]: string; } } export interface Hotbar { id: string; name: string; items: HotbarItem[]; } export interface HotbarStoreModel { hotbars: Hotbar[]; activeHotbarId: string; } export class HotbarStore extends BaseStore { @observable hotbars: Hotbar[] = []; @observable private _activeHotbarId: string; private constructor() { super({ configName: "lens-hotbar-store", accessPropertiesByDotNotation: false, // To make dots safe in cluster context names syncOptions: { equals: comparer.structural, }, migrations, }); } get activeHotbarId() { return this._activeHotbarId; } set activeHotbarId(id: string) { if (this.getByid(id)) { this._activeHotbarId = id; } } get activeHotbarIndex() { return this.hotbars.findIndex((hotbar) => hotbar.id === this.activeHotbarId); } @action protected async fromStore(data: Partial = {}) { if (data.hotbars?.length === 0) { this.hotbars = [{ id: uuid(), name: "Default", items: [] }]; } else { this.hotbars = data.hotbars; } if (data.activeHotbarId) { if (this.getByid(data.activeHotbarId)) { this.activeHotbarId = data.activeHotbarId; } } if (!this.activeHotbarId) { this.activeHotbarId = this.hotbars[0].id; } } getActive() { return this.getByid(this.activeHotbarId); } getByName(name: string) { return this.hotbars.find((hotbar) => hotbar.name === name); } getByid(id: string) { return this.hotbars.find((hotbar) => hotbar.id === id); } add(hotbar: Partial) { hotbar.id = uuid(); if (!hotbar.items) { hotbar.items = []; } this.hotbars.push(hotbar as Hotbar); return hotbar as Hotbar; } remove(hotbar: Hotbar) { this.hotbars = this.hotbars.filter((h) => h !== hotbar); if (this.activeHotbarId === hotbar.id) { this.activeHotbarId = this.hotbars[0].id; } } toJSON(): HotbarStoreModel { const model: HotbarStoreModel = { hotbars: this.hotbars, activeHotbarId: this.activeHotbarId }; return toJS(model, { recurseEverything: true, }); } } export const hotbarStore = HotbarStore.getInstance();