diff --git a/packages/core/src/common/hotbars/store.ts b/packages/core/src/common/hotbars/store.ts index 709242f20e..fcac209001 100644 --- a/packages/core/src/common/hotbars/store.ts +++ b/packages/core/src/common/hotbars/store.ts @@ -8,10 +8,8 @@ import type { BaseStoreDependencies } from "../base-store/base-store"; import { BaseStore } from "../base-store/base-store"; import { toJS } from "../utils"; import type { CatalogEntity } from "../catalog"; -import { broadcastMessage } from "../ipc"; import type { Hotbar, CreateHotbarData, CreateHotbarOptions } from "./types"; import { defaultHotbarCells, getEmptyHotbar } from "./types"; -import { hotbarTooManyItemsChannel } from "../ipc/hotbar"; import type { GeneralEntity } from "../catalog-entities"; import type { Logger } from "../logger"; import assert from "assert"; @@ -24,6 +22,7 @@ export interface HotbarStoreModel { interface Dependencies extends BaseStoreDependencies { readonly catalogCatalogEntity: GeneralEntity; readonly logger: Logger; + onTooManyHotbarItems: () => void; } export class HotbarStore extends BaseStore { @@ -195,7 +194,7 @@ export class HotbarStore extends BaseStore { if (emptyCellIndex != -1) { hotbar.items[emptyCellIndex] = newItem; } else { - broadcastMessage(hotbarTooManyItemsChannel); + this.dependencies.onTooManyHotbarItems(); } } else if (0 <= cellIndex && cellIndex < hotbar.items.length) { hotbar.items[cellIndex] = newItem; diff --git a/packages/core/src/features/hotbar/store/common/on-too-many-items.ts b/packages/core/src/features/hotbar/store/common/on-too-many-items.ts new file mode 100644 index 0000000000..d6b2081bab --- /dev/null +++ b/packages/core/src/features/hotbar/store/common/on-too-many-items.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { getInjectionToken } from "@ogre-tools/injectable"; + +export const onTooManyHotbarItemsInjectionToken = getInjectionToken<() => void>({ + id: "on-too-many-hotbar-items-token", +}); diff --git a/packages/core/src/features/hotbar/store/main/on-too-many-items.injectable.ts b/packages/core/src/features/hotbar/store/main/on-too-many-items.injectable.ts new file mode 100644 index 0000000000..4d53f7ee7e --- /dev/null +++ b/packages/core/src/features/hotbar/store/main/on-too-many-items.injectable.ts @@ -0,0 +1,15 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { noop } from "../../../../common/utils"; +import { onTooManyHotbarItemsInjectionToken } from "../common/on-too-many-items"; + +const onTooManyHotbarItemsInjectable = getInjectable({ + id: "on-too-many-hotbar-items", + instantiate: () => noop, + injectionToken: onTooManyHotbarItemsInjectionToken, +}); + +export default onTooManyHotbarItemsInjectable; diff --git a/packages/core/src/features/hotbar/store/renderer/on-too-many-items.injectable.tsx b/packages/core/src/features/hotbar/store/renderer/on-too-many-items.injectable.tsx new file mode 100644 index 0000000000..b439ab39a1 --- /dev/null +++ b/packages/core/src/features/hotbar/store/renderer/on-too-many-items.injectable.tsx @@ -0,0 +1,20 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import { defaultHotbarCells } from "../../../../common/hotbars/types"; +import showErrorNotificationInjectable from "../../../../renderer/components/notifications/show-error-notification.injectable"; +import { onTooManyHotbarItemsInjectionToken } from "../common/on-too-many-items"; + +const onTooManyHotbarItemsInjectable = getInjectable({ + id: "on-too-many-hotbar-items", + instantiate: (di) => { + const showErrorNotification = di.inject(showErrorNotificationInjectable); + + return () => showErrorNotification(`Cannot have more than ${defaultHotbarCells} items pinned to a hotbar`); + }, + injectionToken: onTooManyHotbarItemsInjectionToken, +}); + +export default onTooManyHotbarItemsInjectable; diff --git a/packages/core/src/renderer/frames/root-frame/init-root-frame.injectable.ts b/packages/core/src/renderer/frames/root-frame/init-root-frame.injectable.ts index c2e6ed543e..5eb0f864c1 100644 --- a/packages/core/src/renderer/frames/root-frame/init-root-frame.injectable.ts +++ b/packages/core/src/renderer/frames/root-frame/init-root-frame.injectable.ts @@ -3,20 +3,16 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import registerIpcListenersInjectable from "../../ipc/register-ipc-listeners.injectable"; import loggerInjectable from "../../../common/logger.injectable"; import unmountRootComponentInjectable from "../../window/unmount-root-component.injectable"; const initRootFrameInjectable = getInjectable({ id: "init-root-frame", instantiate: (di) => { - const registerIpcListeners = di.inject(registerIpcListenersInjectable); const logger = di.inject(loggerInjectable); const unmountRootComponent = di.inject(unmountRootComponentInjectable); return async () => { - registerIpcListeners(); - window.addEventListener("beforeunload", () => { logger.info("[ROOT-FRAME]: Unload app"); unmountRootComponent(); diff --git a/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts b/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts deleted file mode 100644 index b57be1b155..0000000000 --- a/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable } from "@ogre-tools/injectable"; -import { defaultHotbarCells } from "../../common/hotbars/types"; -import { hotbarTooManyItemsChannel } from "../../common/ipc/hotbar"; -import showErrorNotificationInjectable from "../components/notifications/show-error-notification.injectable"; -import ipcRendererInjectable from "../utils/channel/ipc-renderer.injectable"; - -const registerIpcListenersInjectable = getInjectable({ - id: "register-ipc-listeners", - - instantiate: (di) => { - const ipcRenderer = di.inject(ipcRendererInjectable); - const showErrorNotification = di.inject(showErrorNotificationInjectable); - - return () => { - ipcRenderer.on(hotbarTooManyItemsChannel, () => { - showErrorNotification(`Cannot have more than ${defaultHotbarCells} items pinned to a hotbar`); - }); - }; - }, -}); - -export default registerIpcListenersInjectable;