From 1b46ccd18b6be1e194763bea39ef8281ddd033d2 Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Thu, 19 May 2022 11:31:22 +0300 Subject: [PATCH] Make notifications unit testable in behaviours Signed-off-by: Janne Savolainen --- ...acters-in-page-registrations.test.tsx.snap | 11 +- .../navigate-to-extension-page.test.tsx.snap | 20 ++- .../navigating-between-routes.test.tsx.snap | 6 + ...ation-using-application-menu.test.tsx.snap | 11 +- .../order-of-sidebar-items.test.tsx.snap | 6 + ...-and-tab-navigation-for-core.test.tsx.snap | 21 +++ ...ab-navigation-for-extensions.test.tsx.snap | 24 ++++ .../visibility-of-sidebar-items.test.tsx.snap | 6 + ...gation-using-application-menu.test.ts.snap | 11 +- .../navigation-to-helm-charts.test.ts.snap | 3 + .../closing-preferences.test.tsx.snap | 24 ++++ ...on-to-application-preferences.test.ts.snap | 6 + ...igation-to-editor-preferences.test.ts.snap | 6 + ...tension-specific-preferences.test.tsx.snap | 9 ++ ...ion-to-kubernetes-preferences.test.ts.snap | 37 +++-- ...vigation-to-proxy-preferences.test.ts.snap | 6 + ...ion-to-telemetry-preferences.test.tsx.snap | 15 ++ ...ation-to-terminal-preferences.test.ts.snap | 6 + ...gation-using-application-menu.test.ts.snap | 11 +- .../navigation-using-tray.test.ts.snap | 9 +- .../installing-update-using-tray.test.ts.snap | 54 ++++++-- ...gation-using-application-menu.test.ts.snap | 11 +- .../notifications-store.injectable.ts | 13 ++ .../notifications/notifications.store.tsx | 2 - .../notifications/notifications.tsx | 128 +++++++++++------- .../test-utils/get-application-builder.tsx | 4 +- ...amespaces-forbidden-handler.injectable.tsx | 4 +- src/renderer/ipc/register-listeners.tsx | 3 +- .../about-port-forwarding.injectable.ts | 6 +- ...notify-error-port-forwarding.injectable.ts | 6 +- .../port-forward/port-forward-notify.tsx | 7 +- 31 files changed, 403 insertions(+), 83 deletions(-) create mode 100644 src/renderer/components/notifications/notifications-store.injectable.ts diff --git a/src/behaviours/__snapshots__/extension-special-characters-in-page-registrations.test.tsx.snap b/src/behaviours/__snapshots__/extension-special-characters-in-page-registrations.test.tsx.snap index 3b43a51f66..80b0028469 100644 --- a/src/behaviours/__snapshots__/extension-special-characters-in-page-registrations.test.tsx.snap +++ b/src/behaviours/__snapshots__/extension-special-characters-in-page-registrations.test.tsx.snap @@ -1,11 +1,20 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`extension special characters in page registrations renders 1`] = `
`; +exports[`extension special characters in page registrations renders 1`] = ` +
+
+
+`; exports[`extension special characters in page registrations when navigating to route with ID having special characters renders 1`] = `
Some page
+
`; diff --git a/src/behaviours/__snapshots__/navigate-to-extension-page.test.tsx.snap b/src/behaviours/__snapshots__/navigate-to-extension-page.test.tsx.snap index edab04b903..c96763fe6e 100644 --- a/src/behaviours/__snapshots__/navigate-to-extension-page.test.tsx.snap +++ b/src/behaviours/__snapshots__/navigate-to-extension-page.test.tsx.snap @@ -1,12 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`navigate to extension page renders 1`] = `
`; +exports[`navigate to extension page renders 1`] = ` +
+
+
+`; exports[`navigate to extension page when extension navigates to child route renders 1`] = `
Child page
+
`; @@ -31,6 +40,9 @@ exports[`navigate to extension page when extension navigates to route with param Some button
+
`; @@ -55,6 +67,9 @@ exports[`navigate to extension page when extension navigates to route without pa Some button
+
`; @@ -79,5 +94,8 @@ exports[`navigate to extension page when extension navigates to route without pa Some button
+
`; diff --git a/src/behaviours/__snapshots__/navigating-between-routes.test.tsx.snap b/src/behaviours/__snapshots__/navigating-between-routes.test.tsx.snap index 90ff615b2b..10e9eb2d39 100644 --- a/src/behaviours/__snapshots__/navigating-between-routes.test.tsx.snap +++ b/src/behaviours/__snapshots__/navigating-between-routes.test.tsx.snap @@ -8,6 +8,9 @@ exports[`navigating between routes given route with optional path parameters whe "someOtherParameter": "some-other-value" } +
`; @@ -16,5 +19,8 @@ exports[`navigating between routes given route without path parameters when navi
Some component
+
`; diff --git a/src/behaviours/add-cluster/__snapshots__/navigation-using-application-menu.test.tsx.snap b/src/behaviours/add-cluster/__snapshots__/navigation-using-application-menu.test.tsx.snap index d19612eac3..0fd00133aa 100644 --- a/src/behaviours/add-cluster/__snapshots__/navigation-using-application-menu.test.tsx.snap +++ b/src/behaviours/add-cluster/__snapshots__/navigation-using-application-menu.test.tsx.snap @@ -1,6 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`add-cluster - navigation using application menu renders 1`] = `
`; +exports[`add-cluster - navigation using application menu renders 1`] = ` +
+
+
+`; exports[`add-cluster - navigation using application menu when navigating to add cluster using application menu renders 1`] = `
@@ -85,5 +91,8 @@ exports[`add-cluster - navigation using application menu when navigating to add
+
`; diff --git a/src/behaviours/cluster/__snapshots__/order-of-sidebar-items.test.tsx.snap b/src/behaviours/cluster/__snapshots__/order-of-sidebar-items.test.tsx.snap index 092337ec82..9af01f0969 100644 --- a/src/behaviours/cluster/__snapshots__/order-of-sidebar-items.test.tsx.snap +++ b/src/behaviours/cluster/__snapshots__/order-of-sidebar-items.test.tsx.snap @@ -328,6 +328,9 @@ exports[`cluster - order of sidebar items when rendered renders 1`] = `
+
`; @@ -723,5 +726,8 @@ exports[`cluster - order of sidebar items when rendered when parent is expanded +
`; diff --git a/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-core.test.tsx.snap b/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-core.test.tsx.snap index ad1f7a8d4c..06d1a1e210 100644 --- a/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-core.test.tsx.snap +++ b/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-core.test.tsx.snap @@ -293,6 +293,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations +
`; @@ -589,6 +592,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations +
`; @@ -909,6 +915,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations +
`; @@ -1234,6 +1243,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations
+
`; @@ -1534,6 +1546,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations
+
`; @@ -1854,6 +1869,9 @@ exports[`cluster - sidebar and tab navigation for core given core registrations
+
`; @@ -2150,5 +2168,8 @@ exports[`cluster - sidebar and tab navigation for core given core registrations +
`; diff --git a/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-extensions.test.tsx.snap b/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-extensions.test.tsx.snap index be9c321cd5..19cb615cce 100644 --- a/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-extensions.test.tsx.snap +++ b/src/behaviours/cluster/__snapshots__/sidebar-and-tab-navigation-for-extensions.test.tsx.snap @@ -293,6 +293,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -589,6 +592,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -929,6 +935,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -1313,6 +1322,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -1697,6 +1709,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -2036,6 +2051,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -2376,6 +2394,9 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; @@ -2672,5 +2693,8 @@ exports[`cluster - sidebar and tab navigation for extensions given extension wit +
`; diff --git a/src/behaviours/cluster/__snapshots__/visibility-of-sidebar-items.test.tsx.snap b/src/behaviours/cluster/__snapshots__/visibility-of-sidebar-items.test.tsx.snap index 4f28c6ecef..cc44f56496 100644 --- a/src/behaviours/cluster/__snapshots__/visibility-of-sidebar-items.test.tsx.snap +++ b/src/behaviours/cluster/__snapshots__/visibility-of-sidebar-items.test.tsx.snap @@ -261,6 +261,9 @@ exports[`cluster - visibility of sidebar items given kube resource for route is +
`; @@ -573,5 +576,8 @@ exports[`cluster - visibility of sidebar items given kube resource for route is +
`; diff --git a/src/behaviours/extensions/__snapshots__/navigation-using-application-menu.test.ts.snap b/src/behaviours/extensions/__snapshots__/navigation-using-application-menu.test.ts.snap index 3a1b1309dd..c14ccb6160 100644 --- a/src/behaviours/extensions/__snapshots__/navigation-using-application-menu.test.ts.snap +++ b/src/behaviours/extensions/__snapshots__/navigation-using-application-menu.test.ts.snap @@ -1,6 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`extensions - navigation using application menu renders 1`] = `
`; +exports[`extensions - navigation using application menu renders 1`] = ` +
+
+
+`; exports[`extensions - navigation using application menu when navigating to extensions using application menu renders 1`] = `
@@ -118,5 +124,8 @@ exports[`extensions - navigation using application menu when navigating to exten
+
`; diff --git a/src/behaviours/helm-charts/__snapshots__/navigation-to-helm-charts.test.ts.snap b/src/behaviours/helm-charts/__snapshots__/navigation-to-helm-charts.test.ts.snap index 4f1555049d..e323205008 100644 --- a/src/behaviours/helm-charts/__snapshots__/navigation-to-helm-charts.test.ts.snap +++ b/src/behaviours/helm-charts/__snapshots__/navigation-to-helm-charts.test.ts.snap @@ -454,5 +454,8 @@ exports[`helm-charts - navigation to Helm charts when navigating to Helm charts +
`; diff --git a/src/behaviours/preferences/__snapshots__/closing-preferences.test.tsx.snap b/src/behaviours/preferences/__snapshots__/closing-preferences.test.tsx.snap index cb4973203d..740e01edbb 100644 --- a/src/behaviours/preferences/__snapshots__/closing-preferences.test.tsx.snap +++ b/src/behaviours/preferences/__snapshots__/closing-preferences.test.tsx.snap @@ -537,6 +537,9 @@ exports[`preferences - closing-preferences given accessing preferences directly +
`; @@ -679,6 +682,9 @@ exports[`preferences - closing-preferences given accessing preferences directly +
`; @@ -687,6 +693,9 @@ exports[`preferences - closing-preferences given accessing preferences directly
Some front page
+
`; @@ -695,6 +704,9 @@ exports[`preferences - closing-preferences given accessing preferences directly
Some front page
+
`; @@ -1235,6 +1247,9 @@ exports[`preferences - closing-preferences given already in a page and then navi +
`; @@ -1377,6 +1392,9 @@ exports[`preferences - closing-preferences given already in a page and then navi +
`; @@ -1519,6 +1537,9 @@ exports[`preferences - closing-preferences given already in a page and then navi +
`; @@ -1661,5 +1682,8 @@ exports[`preferences - closing-preferences given already in a page and then navi +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-application-preferences.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-to-application-preferences.test.ts.snap index f67337e80c..57e68c4ea5 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-application-preferences.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-application-preferences.test.ts.snap @@ -199,6 +199,9 @@ exports[`preferences - navigation to application preferences given in some child +
`; @@ -727,5 +730,8 @@ exports[`preferences - navigation to application preferences given in some child +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-editor-preferences.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-to-editor-preferences.test.ts.snap index 4e92ac4f95..8daf6eb997 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-editor-preferences.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-editor-preferences.test.ts.snap @@ -525,6 +525,9 @@ exports[`preferences - navigation to editor preferences given in preferences, wh +
`; @@ -935,5 +938,8 @@ exports[`preferences - navigation to editor preferences given in preferences, wh +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-extension-specific-preferences.test.tsx.snap b/src/behaviours/preferences/__snapshots__/navigation-to-extension-specific-preferences.test.tsx.snap index d3f42e6d63..609350c455 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-extension-specific-preferences.test.tsx.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-extension-specific-preferences.test.tsx.snap @@ -525,6 +525,9 @@ exports[`preferences - navigation to extension specific preferences given in pre +
`; @@ -1065,6 +1068,9 @@ exports[`preferences - navigation to extension specific preferences given in pre +
`; @@ -1239,5 +1245,8 @@ exports[`preferences - navigation to extension specific preferences given in pre +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-kubernetes-preferences.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-to-kubernetes-preferences.test.ts.snap index 2e9b7722aa..c962cfb2ab 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-kubernetes-preferences.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-kubernetes-preferences.test.ts.snap @@ -525,6 +525,9 @@ exports[`preferences - navigation to kubernetes preferences given in preferences +
`; @@ -836,7 +839,7 @@ exports[`preferences - navigation to kubernetes preferences given in preferences class="flex gaps" >
+
@@ -969,5 +985,8 @@ exports[`preferences - navigation to kubernetes preferences given in preferences
+
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-proxy-preferences.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-to-proxy-preferences.test.ts.snap index 8c6507ef0a..2718c892e7 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-proxy-preferences.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-proxy-preferences.test.ts.snap @@ -525,6 +525,9 @@ exports[`preferences - navigation to proxy preferences given in preferences, whe +
`; @@ -727,5 +730,8 @@ exports[`preferences - navigation to proxy preferences given in preferences, whe +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-telemetry-preferences.test.tsx.snap b/src/behaviours/preferences/__snapshots__/navigation-to-telemetry-preferences.test.tsx.snap index 80f5b61bb1..0187d3573e 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-telemetry-preferences.test.tsx.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-telemetry-preferences.test.tsx.snap @@ -185,6 +185,9 @@ exports[`preferences - navigation to telemetry preferences given URL for Sentry +
`; @@ -713,6 +716,9 @@ exports[`preferences - navigation to telemetry preferences given in preferences, +
`; @@ -1253,6 +1259,9 @@ exports[`preferences - navigation to telemetry preferences given in preferences, +
`; @@ -1429,6 +1438,9 @@ exports[`preferences - navigation to telemetry preferences given in preferences, +
`; @@ -1568,5 +1580,8 @@ exports[`preferences - navigation to telemetry preferences given no URL for Sent +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-to-terminal-preferences.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-to-terminal-preferences.test.ts.snap index 5e5934c3bb..8cd1678235 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-to-terminal-preferences.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-to-terminal-preferences.test.ts.snap @@ -525,6 +525,9 @@ exports[`preferences - navigation to terminal preferences given in preferences, +
`; @@ -845,5 +848,8 @@ exports[`preferences - navigation to terminal preferences given in preferences, +
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-using-application-menu.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-using-application-menu.test.ts.snap index 141279f4e4..f49e4d460a 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-using-application-menu.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-using-application-menu.test.ts.snap @@ -1,6 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`preferences - navigation using application menu renders 1`] = `
`; +exports[`preferences - navigation using application menu renders 1`] = ` +
+
+
+`; exports[`preferences - navigation using application menu when navigating to preferences using application menu renders 1`] = `
@@ -527,5 +533,8 @@ exports[`preferences - navigation using application menu when navigating to pref
+
`; diff --git a/src/behaviours/preferences/__snapshots__/navigation-using-tray.test.ts.snap b/src/behaviours/preferences/__snapshots__/navigation-using-tray.test.ts.snap index 0abb2bd928..398e4d4b05 100644 --- a/src/behaviours/preferences/__snapshots__/navigation-using-tray.test.ts.snap +++ b/src/behaviours/preferences/__snapshots__/navigation-using-tray.test.ts.snap @@ -2,7 +2,11 @@ exports[`show-about-using-tray renders 1`] = ` -
+
+
+
`; @@ -532,6 +536,9 @@ exports[`show-about-using-tray when navigating using tray renders 1`] = `
+
`; diff --git a/src/behaviours/update-app/__snapshots__/installing-update-using-tray.test.ts.snap b/src/behaviours/update-app/__snapshots__/installing-update-using-tray.test.ts.snap index f2a174da52..75d9716a6d 100644 --- a/src/behaviours/update-app/__snapshots__/installing-update-using-tray.test.ts.snap +++ b/src/behaviours/update-app/__snapshots__/installing-update-using-tray.test.ts.snap @@ -2,54 +2,90 @@ exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered when download fails renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered when download succeeds renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered when download succeeds when user answers not to install the update renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered when download succeeds when user answers to install the update renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when new update is discovered when download succeeds when user disregards the question and installs the update using tray renders 1`] = ` -
+
+
+
`; exports[`installing update using tray given no update is already downloaded, and "latest" update channel is selected, when started when user checks for updates using tray when no new update is discovered renders 1`] = ` -
+
+
+
`; diff --git a/src/behaviours/welcome/__snapshots__/navigation-using-application-menu.test.ts.snap b/src/behaviours/welcome/__snapshots__/navigation-using-application-menu.test.ts.snap index d59f7d040a..05eee498bf 100644 --- a/src/behaviours/welcome/__snapshots__/navigation-using-application-menu.test.ts.snap +++ b/src/behaviours/welcome/__snapshots__/navigation-using-application-menu.test.ts.snap @@ -1,6 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`welcome - navigation using application menu renders 1`] = `
`; +exports[`welcome - navigation using application menu renders 1`] = ` +
+
+
+`; exports[`welcome - navigation using application menu when navigating to welcome using application menu renders 1`] = `
@@ -87,5 +93,8 @@ exports[`welcome - navigation using application menu when navigating to welcome
+
`; diff --git a/src/renderer/components/notifications/notifications-store.injectable.ts b/src/renderer/components/notifications/notifications-store.injectable.ts new file mode 100644 index 0000000000..1b14abef96 --- /dev/null +++ b/src/renderer/components/notifications/notifications-store.injectable.ts @@ -0,0 +1,13 @@ +/** + * 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 { NotificationsStore } from "./notifications.store"; + +const notificationsStoreInjectable = getInjectable({ + id: "notifications-store", + instantiate: () => new NotificationsStore(), +}); + +export default notificationsStoreInjectable; diff --git a/src/renderer/components/notifications/notifications.store.tsx b/src/renderer/components/notifications/notifications.store.tsx index 9202a19e31..25774bc9ce 100644 --- a/src/renderer/components/notifications/notifications.store.tsx +++ b/src/renderer/components/notifications/notifications.store.tsx @@ -91,5 +91,3 @@ export class NotificationsStore { } } } - -export const notificationsStore = new NotificationsStore(); diff --git a/src/renderer/components/notifications/notifications.tsx b/src/renderer/components/notifications/notifications.tsx index 521a61e638..bd75619275 100644 --- a/src/renderer/components/notifications/notifications.tsx +++ b/src/renderer/components/notifications/notifications.tsx @@ -10,61 +10,25 @@ import { reaction } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { JsonApiErrorParsed } from "../../../common/k8s-api/json-api"; import { cssNames, prevDefault } from "../../utils"; -import type { Notification, NotificationMessage } from "./notifications.store"; -import { notificationsStore, NotificationStatus } from "./notifications.store"; +import type { Notification, NotificationMessage, NotificationsStore } from "./notifications.store"; +import { NotificationStatus } from "./notifications.store"; import { Animate } from "../animate"; import { Icon } from "../icon"; +import { withInjectables } from "@ogre-tools/injectable-react"; +import { asLegacyGlobalForExtensionApi } from "../../../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api"; +import notificationsStoreInjectable from "./notifications-store.injectable"; + +interface Dependencies { + store: NotificationsStore; +} @observer -export class Notifications extends React.Component { +class NonInjectedNotifications extends React.Component { public elem: HTMLDivElement | null = null; - static ok(message: NotificationMessage) { - return notificationsStore.add({ - message, - timeout: 2_500, - status: NotificationStatus.OK, - }); - } - - static checkedError(message: unknown, fallback: string, customOpts?: Partial>) { - if (typeof message === "string" || message instanceof Error || message instanceof JsonApiErrorParsed) { - return Notifications.error(message, customOpts); - } - - console.warn("Unknown notification error message, falling back to default", message); - - return Notifications.error(fallback, customOpts); - } - - static error(message: NotificationMessage, customOpts: Partial> = {}) { - return notificationsStore.add({ - message, - timeout: 10_000, - status: NotificationStatus.ERROR, - ...customOpts, - }); - } - - static shortInfo(message: NotificationMessage, customOpts: Partial> = {}) { - return this.info(message, { - timeout: 5_000, - ...customOpts, - }); - } - - static info(message: NotificationMessage, customOpts: Partial> = {}) { - return notificationsStore.add({ - status: NotificationStatus.INFO, - timeout: 0, - message, - ...customOpts, - }); - } - componentDidMount() { disposeOnUnmount(this, [ - reaction(() => notificationsStore.notifications.length, () => { + reaction(() => this.props.store.notifications.length, () => { this.scrollToLastNotification(); }, { delay: 250 }), ]); @@ -74,7 +38,7 @@ export class Notifications extends React.Component { if (!this.elem) { return; } - this.elem.scrollTo({ + this.elem.scrollTo?.({ top: this.elem.scrollHeight, behavior: "smooth", }); @@ -91,7 +55,7 @@ export class Notifications extends React.Component { } render() { - const { notifications, remove, addAutoHideTimer, removeAutoHideTimer } = notificationsStore; + const { notifications, remove, addAutoHideTimer, removeAutoHideTimer } = this.props.store; return (
this.elem = e}> @@ -114,6 +78,7 @@ export class Notifications extends React.Component { { remove(id); onClose?.(); @@ -128,3 +93,68 @@ export class Notifications extends React.Component { ); } } + +export const Notifications = withInjectables( + NonInjectedNotifications, + + { + getProps: (di) => ({ + store: di.inject(notificationsStoreInjectable), + }), + }, +) as React.FC & { + ok: (message: NotificationMessage) => () => void; + checkedError: (message: unknown, fallback: string, customOpts?: Partial>) => () => void; + error: (message: NotificationMessage, customOpts?: Partial>) => () => void; + shortInfo: (message: NotificationMessage, customOpts?: Partial>) => () => void; + info: (message: NotificationMessage, customOpts?: Partial>) => () => void; +}; + +/** + * @deprecated + */ +const _notificationStore = asLegacyGlobalForExtensionApi(notificationsStoreInjectable); + +Notifications.ok = (message: NotificationMessage) => { + return _notificationStore.add({ + message, + timeout: 2_500, + status: NotificationStatus.OK, + }); +}; + +Notifications.checkedError = (message, fallback, customOpts = {}) => { + if (typeof message === "string" || message instanceof Error || message instanceof JsonApiErrorParsed) { + return Notifications.error(message, customOpts); + } + + console.warn("Unknown notification error message, falling back to default", message); + + return Notifications.error(fallback, customOpts); +}; + +Notifications.error = (message, customOpts= {}) => { + return _notificationStore.add({ + message, + timeout: 10_000, + status: NotificationStatus.ERROR, + ...customOpts, + }); +}; + +Notifications.shortInfo = (message, customOpts = {}) => { + return Notifications.info(message, { + timeout: 5_000, + ...customOpts, + }); +}; + +Notifications.info = (message, customOpts = {}) => { + return _notificationStore.add({ + status: NotificationStatus.INFO, + timeout: 0, + message, + ...customOpts, + }); +}; + diff --git a/src/renderer/components/test-utils/get-application-builder.tsx b/src/renderer/components/test-utils/get-application-builder.tsx index b518ca311b..19764b8408 100644 --- a/src/renderer/components/test-utils/get-application-builder.tsx +++ b/src/renderer/components/test-utils/get-application-builder.tsx @@ -44,10 +44,10 @@ import { flushPromises } from "../../../common/test-utils/flush-promises"; import type { NamespaceStore } from "../+namespaces/store"; import namespaceStoreInjectable from "../+namespaces/store.injectable"; import historyInjectable from "../../navigation/history.injectable"; -import trayMenuItemsInjectable from "../../../main/tray/tray-menu-item/tray-menu-items.injectable"; import type { TrayMenuItem } from "../../../main/tray/tray-menu-item/tray-menu-item-injection-token"; import electronTrayInjectable from "../../../main/tray/electron-tray/electron-tray.injectable"; import applicationWindowInjectable from "../../../main/start-main-application/lens-window/application-window/application-window.injectable"; +import { Notifications } from "../notifications/notifications"; type Callback = (dis: DiContainers) => void | Promise; @@ -395,6 +395,8 @@ export const getApplicationBuilder = () => { return ; }} + + , ); diff --git a/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx b/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx index c787899e84..22434df1b6 100644 --- a/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx +++ b/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx @@ -5,17 +5,19 @@ 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 { Notifications, notificationsStore } from "../components/notifications"; +import { Notifications } from "../components/notifications"; import { ClusterStore } from "../../common/cluster-store/cluster-store"; import { Button } from "../components/button"; import type { IpcRendererEvent } from "electron"; import React from "react"; +import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable"; const listNamespacesForbiddenHandlerInjectable = getInjectable({ id: "list-namespaces-forbidden-handler", instantiate: (di) => { const navigateToEntitySettings = di.inject(navigateToEntitySettingsInjectable); + const notificationsStore = di.inject(notificationsStoreInjectable); const notificationLastDisplayedAt = new Map(); const intervalBetweenNotifications = 1000 * 60; // 60s diff --git a/src/renderer/ipc/register-listeners.tsx b/src/renderer/ipc/register-listeners.tsx index a9e0e76d2d..f69feb6d6b 100644 --- a/src/renderer/ipc/register-listeners.tsx +++ b/src/renderer/ipc/register-listeners.tsx @@ -8,7 +8,7 @@ import type { IpcRendererEvent } from "electron"; import { ipcRenderer } from "electron"; import type { UpdateAvailableFromMain, BackchannelArg } from "../../common/ipc"; import { areArgsUpdateAvailableFromMain, UpdateAvailableChannel, onCorrect, ipcRendererOn, AutoUpdateChecking, AutoUpdateNoUpdateAvailable } from "../../common/ipc"; -import { Notifications, notificationsStore } from "../components/notifications"; +import { Notifications } from "../components/notifications"; import { Button } from "../components/button"; import { isMac } from "../../common/vars"; import { defaultHotbarCells } from "../../common/hotbars/types"; @@ -16,7 +16,6 @@ import { type ListNamespaceForbiddenArgs, clusterListNamespaceForbiddenChannel, import { hotbarTooManyItemsChannel } from "../../common/ipc/hotbar"; function sendToBackchannel(backchannel: string, notificationId: string, data: BackchannelArg): void { - notificationsStore.remove(notificationId); ipcRenderer.send(backchannel, data); } diff --git a/src/renderer/port-forward/about-port-forwarding.injectable.ts b/src/renderer/port-forward/about-port-forwarding.injectable.ts index 4656310964..29dba28962 100644 --- a/src/renderer/port-forward/about-port-forwarding.injectable.ts +++ b/src/renderer/port-forward/about-port-forwarding.injectable.ts @@ -7,18 +7,22 @@ import { aboutPortForwarding } from "./port-forward-notify"; import navigateToPortForwardsInjectable from "../../common/front-end-routing/routes/cluster/network/port-forwards/navigate-to-port-forwards.injectable"; import hostedClusterIdInjectable from "../../common/cluster-store/hosted-cluster-id.injectable"; import assert from "assert"; +import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable"; const aboutPortForwardingInjectable = getInjectable({ id: "about-port-forwarding", instantiate: (di) => { const hostedClusterId = di.inject(hostedClusterIdInjectable); + const notificationsStore = di.inject(notificationsStoreInjectable); + const navigateToPortForwards = di.inject(navigateToPortForwardsInjectable); assert(hostedClusterId, "Only allowed to notify about port forward errors within a cluster frame"); return aboutPortForwarding({ - navigateToPortForwards: di.inject(navigateToPortForwardsInjectable), + navigateToPortForwards, hostedClusterId, + notificationsStore, }); }, }); diff --git a/src/renderer/port-forward/notify-error-port-forwarding.injectable.ts b/src/renderer/port-forward/notify-error-port-forwarding.injectable.ts index 9d4cd5caa7..a0f1ed714f 100644 --- a/src/renderer/port-forward/notify-error-port-forwarding.injectable.ts +++ b/src/renderer/port-forward/notify-error-port-forwarding.injectable.ts @@ -7,18 +7,22 @@ import { notifyErrorPortForwarding } from "./port-forward-notify"; import navigateToPortForwardsInjectable from "../../common/front-end-routing/routes/cluster/network/port-forwards/navigate-to-port-forwards.injectable"; import hostedClusterIdInjectable from "../../common/cluster-store/hosted-cluster-id.injectable"; import assert from "assert"; +import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable"; const notifyErrorPortForwardingInjectable = getInjectable({ id: "notify-error-port-forwarding", instantiate: (di) => { const hostedClusterId = di.inject(hostedClusterIdInjectable); + const notificationsStore = di.inject(notificationsStoreInjectable); + const navigateToPortForwards = di.inject(navigateToPortForwardsInjectable); assert(hostedClusterId, "Only allowed to notify about port forward errors within a cluster frame"); return notifyErrorPortForwarding({ - navigateToPortForwards: di.inject(navigateToPortForwardsInjectable), + navigateToPortForwards, hostedClusterId, + notificationsStore, }); }, }); diff --git a/src/renderer/port-forward/port-forward-notify.tsx b/src/renderer/port-forward/port-forward-notify.tsx index 086771ad46..40dc295602 100644 --- a/src/renderer/port-forward/port-forward-notify.tsx +++ b/src/renderer/port-forward/port-forward-notify.tsx @@ -5,17 +5,20 @@ import React from "react"; import { Button } from "../components/button"; -import { Notifications, notificationsStore } from "../components/notifications"; +import type { NotificationsStore } from "../components/notifications"; +import { Notifications } from "../components/notifications"; import type { NavigateToPortForwards } from "../../common/front-end-routing/routes/cluster/network/port-forwards/navigate-to-port-forwards.injectable"; interface AboutPortForwardingDependencies { navigateToPortForwards: NavigateToPortForwards; hostedClusterId: string; + notificationsStore: NotificationsStore; } export const aboutPortForwarding = ({ navigateToPortForwards, hostedClusterId, + notificationsStore, }: AboutPortForwardingDependencies) => () => { const notificationId = `port-forward-notification-${hostedClusterId}`; @@ -49,12 +52,14 @@ export const aboutPortForwarding = ({ interface NotifyErrorPortForwardingDependencies { navigateToPortForwards: NavigateToPortForwards; hostedClusterId: string; + notificationsStore: NotificationsStore; } export const notifyErrorPortForwarding = ({ navigateToPortForwards, hostedClusterId, + notificationsStore, }: NotifyErrorPortForwardingDependencies) => (msg: string) => { const notificationId = `port-forward-error-notification-${hostedClusterId}`;