From 38de2edc506b713c0101062c086ab5648f5d065c Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 21 Feb 2023 12:15:45 -0500 Subject: [PATCH] Move cluster failed to list namespaces to injectable Signed-off-by: Sebastian Malton --- .../cluster/list-namespaces/common/channel.ts | 11 ++++++ ...er-failed-to-list-namespaces.injectable.ts | 21 ++++++++++ ...amespaces-forbidden-handler.injectable.tsx | 39 +++++++------------ .../renderer/listener.injectable.ts | 25 ++++++++++++ .../ipc/register-ipc-listeners.injectable.ts | 4 -- 5 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 packages/core/src/features/cluster/list-namespaces/common/channel.ts create mode 100644 packages/core/src/features/cluster/list-namespaces/main/emit-cluster-failed-to-list-namespaces.injectable.ts rename packages/core/src/{renderer/ipc => features/cluster/list-namespaces/renderer}/list-namespaces-forbidden-handler.injectable.tsx (59%) create mode 100644 packages/core/src/features/cluster/list-namespaces/renderer/listener.injectable.ts diff --git a/packages/core/src/features/cluster/list-namespaces/common/channel.ts b/packages/core/src/features/cluster/list-namespaces/common/channel.ts new file mode 100644 index 0000000000..a3d22534fd --- /dev/null +++ b/packages/core/src/features/cluster/list-namespaces/common/channel.ts @@ -0,0 +1,11 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import type { ClusterId } from "../../../../common/cluster-types"; +import type { MessageChannel } from "../../../../common/utils/channel/message-channel-listener-injection-token"; + +export const clusterFailedToListNamespacesChannel: MessageChannel = { + id: "cluster-failed-to-list-namespaces", +}; diff --git a/packages/core/src/features/cluster/list-namespaces/main/emit-cluster-failed-to-list-namespaces.injectable.ts b/packages/core/src/features/cluster/list-namespaces/main/emit-cluster-failed-to-list-namespaces.injectable.ts new file mode 100644 index 0000000000..22ad7fb003 --- /dev/null +++ b/packages/core/src/features/cluster/list-namespaces/main/emit-cluster-failed-to-list-namespaces.injectable.ts @@ -0,0 +1,21 @@ +/** + * 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 type { MessageChannelSender } from "../../../../common/utils/channel/message-to-channel-injection-token"; +import { sendMessageToChannelInjectionToken } from "../../../../common/utils/channel/message-to-channel-injection-token"; +import { clusterFailedToListNamespacesChannel } from "../common/channel"; + +export type EmitClusterFailedToListNamespaces = MessageChannelSender; + +const emitClusterFailedToListNamespacesInjectable = getInjectable({ + id: "emit-cluster-failed-to-list-namespaces", + instantiate: (di): EmitClusterFailedToListNamespaces => { + const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken); + + return (clusterId) => sendMessageToChannel(clusterFailedToListNamespacesChannel, clusterId); + }, +}); + +export default emitClusterFailedToListNamespacesInjectable; diff --git a/packages/core/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx b/packages/core/src/features/cluster/list-namespaces/renderer/list-namespaces-forbidden-handler.injectable.tsx similarity index 59% rename from packages/core/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx rename to packages/core/src/features/cluster/list-namespaces/renderer/list-namespaces-forbidden-handler.injectable.tsx index 75fb51fe68..41a86965f6 100644 --- a/packages/core/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx +++ b/packages/core/src/features/cluster/list-namespaces/renderer/list-namespaces-forbidden-handler.injectable.tsx @@ -3,15 +3,13 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import navigateToEntitySettingsInjectable from "../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable"; -import type { ListNamespaceForbiddenArgs } from "../../common/ipc/cluster"; -import { Button } from "../components/button"; -import type { IpcRendererEvent } from "electron"; +import navigateToEntitySettingsInjectable from "../../../../common/front-end-routing/routes/entity-settings/navigate-to-entity-settings.injectable"; +import { Button } from "../../../../renderer/components/button"; import React from "react"; -import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable"; -import { getMillisecondsFromUnixEpoch } from "../../common/utils/date/get-current-date-time"; -import getClusterByIdInjectable from "../../common/cluster-store/get-by-id.injectable"; -import showSuccessNotificationInjectable from "../components/notifications/show-success-notification.injectable"; +import { getMillisecondsFromUnixEpoch } from "../../../../common/utils/date/get-current-date-time"; +import getClusterByIdInjectable from "../../../../common/cluster-store/get-by-id.injectable"; +import showSuccessNotificationInjectable from "../../../../renderer/components/notifications/show-success-notification.injectable"; +import type { ClusterId } from "../../../../common/cluster-types"; const intervalBetweenNotifications = 1000 * 60; // 60s @@ -20,15 +18,11 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ instantiate: (di) => { const navigateToEntitySettings = di.inject(navigateToEntitySettingsInjectable); - const notificationsStore = di.inject(notificationsStoreInjectable); const getClusterById = di.inject(getClusterByIdInjectable); const notificationLastDisplayedAt = new Map(); const showSuccessNotification = di.inject(showSuccessNotificationInjectable); - return ( - event: IpcRendererEvent, - ...[clusterId]: ListNamespaceForbiddenArgs - ): void => { + return (clusterId: ClusterId) => { const lastDisplayedAt = notificationLastDisplayedAt.get(clusterId); const now = getMillisecondsFromUnixEpoch(); @@ -42,14 +36,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ return; } - const notificationId = `list-namespaces-forbidden:${clusterId}`; - - if (notificationsStore.getById(notificationId)) { - // notification is still visible - return; - } - - showSuccessNotification( + const closeNotification = showSuccessNotification( (
Add Accessible Namespaces @@ -65,18 +52,18 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ label="Go to Accessible Namespaces Settings" onClick={() => { navigateToEntitySettings(clusterId, "namespaces"); - notificationsStore.remove(notificationId); + closeNotification(); }} />
), { - id: notificationId, + id: `list-namespaces-forbidden:${clusterId}`, /** - * Set the time when the notification is closed as well so that there is at - * least a minute between closing the notification as seeing it again - */ + * Set the time when the notification is closed as well so that there is at + * least a minute between closing the notification as seeing it again + */ onClose: () => notificationLastDisplayedAt.set(clusterId, getMillisecondsFromUnixEpoch()), }, ); diff --git a/packages/core/src/features/cluster/list-namespaces/renderer/listener.injectable.ts b/packages/core/src/features/cluster/list-namespaces/renderer/listener.injectable.ts new file mode 100644 index 0000000000..9dc147e948 --- /dev/null +++ b/packages/core/src/features/cluster/list-namespaces/renderer/listener.injectable.ts @@ -0,0 +1,25 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { noop } from "../../../../common/utils"; +import { getMessageChannelListenerInjectable } from "../../../../common/utils/channel/message-channel-listener-injection-token"; +import currentlyInClusterFrameInjectable from "../../../../renderer/routes/currently-in-cluster-frame.injectable"; +import { clusterFailedToListNamespacesChannel } from "../common/channel"; +import listNamespacesForbiddenHandlerInjectable from "./list-namespaces-forbidden-handler.injectable"; + +const clusterFailedToListNamespacesListenerInjectable = getMessageChannelListenerInjectable({ + channel: clusterFailedToListNamespacesChannel, + id: "main", + handler: (di) => { + const currentlyInClusterFrame = di.inject(currentlyInClusterFrameInjectable); + + if (currentlyInClusterFrame) { + return noop; + } + + return di.inject(listNamespacesForbiddenHandlerInjectable); + }, +}); + +export default clusterFailedToListNamespacesListenerInjectable; diff --git a/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts b/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts index 0c2d407d59..b57be1b155 100644 --- a/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts +++ b/packages/core/src/renderer/ipc/register-ipc-listeners.injectable.ts @@ -4,22 +4,18 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import { defaultHotbarCells } from "../../common/hotbars/types"; -import { clusterListNamespaceForbiddenChannel } from "../../common/ipc/cluster"; import { hotbarTooManyItemsChannel } from "../../common/ipc/hotbar"; import showErrorNotificationInjectable from "../components/notifications/show-error-notification.injectable"; import ipcRendererInjectable from "../utils/channel/ipc-renderer.injectable"; -import listNamespacesForbiddenHandlerInjectable from "./list-namespaces-forbidden-handler.injectable"; const registerIpcListenersInjectable = getInjectable({ id: "register-ipc-listeners", instantiate: (di) => { - const listNamespacesForbiddenHandler = di.inject(listNamespacesForbiddenHandlerInjectable); const ipcRenderer = di.inject(ipcRendererInjectable); const showErrorNotification = di.inject(showErrorNotificationInjectable); return () => { - ipcRenderer.on(clusterListNamespaceForbiddenChannel, listNamespacesForbiddenHandler); ipcRenderer.on(hotbarTooManyItemsChannel, () => { showErrorNotification(`Cannot have more than ${defaultHotbarCells} items pinned to a hotbar`); });