From a185227ca26659732990cab7990b82c22774bda4 Mon Sep 17 00:00:00 2001 From: Jim Ehrismann Date: Fri, 7 Jan 2022 19:26:33 -0500 Subject: [PATCH] converting app-preferences to use di Signed-off-by: Jim Ehrismann --- src/extensions/common-api/registrations.ts | 2 +- .../extension-loader/extension-loader.ts | 1 - src/extensions/lens-renderer-extension.ts | 3 +- .../registries/app-preference-registry.ts | 32 --------- src/extensions/registries/index.ts | 1 - .../app-preference-registration.d.ts | 18 +++++ .../app-preferences.injectable.ts | 34 +++++++++ .../app-preferences/get-app-preferences.ts | 49 +++++++++++++ .../components/+preferences/application.tsx | 32 ++++++--- .../+preferences/extension-settings.tsx | 4 +- .../components/+preferences/extensions.tsx | 26 +++++-- .../components/+preferences/preferences.tsx | 69 ++++++++++--------- .../components/+preferences/telemetry.tsx | 25 +++++-- src/renderer/initializers/registries.ts | 1 - 14 files changed, 211 insertions(+), 86 deletions(-) delete mode 100644 src/extensions/registries/app-preference-registry.ts create mode 100644 src/renderer/components/+preferences/app-preferences/app-preference-registration.d.ts create mode 100644 src/renderer/components/+preferences/app-preferences/app-preferences.injectable.ts create mode 100644 src/renderer/components/+preferences/app-preferences/get-app-preferences.ts diff --git a/src/extensions/common-api/registrations.ts b/src/extensions/common-api/registrations.ts index be1f24d025..666ed01a26 100644 --- a/src/extensions/common-api/registrations.ts +++ b/src/extensions/common-api/registrations.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -export type { AppPreferenceRegistration, AppPreferenceComponents } from "../registries/app-preference-registry"; +export type { AppPreferenceRegistration, AppPreferenceComponents } from "../../renderer/components/+preferences/app-preferences/app-preference-registration"; export type { KubeObjectDetailRegistration, KubeObjectDetailComponents } from "../registries/kube-object-detail-registry"; export type { KubeObjectMenuRegistration, KubeObjectMenuComponents } from "../registries/kube-object-menu-registry"; export type { KubeObjectStatusRegistration } from "../registries/kube-object-status-registry"; diff --git a/src/extensions/extension-loader/extension-loader.ts b/src/extensions/extension-loader/extension-loader.ts index 675786dfe3..aa89fa6c7c 100644 --- a/src/extensions/extension-loader/extension-loader.ts +++ b/src/extensions/extension-loader/extension-loader.ts @@ -252,7 +252,6 @@ export class ExtensionLoader { return this.autoInitExtensions(async (extension: LensRendererExtension) => { const removeItems = [ registries.GlobalPageRegistry.getInstance().add(extension.globalPages, extension), - registries.AppPreferenceRegistry.getInstance().add(extension.appPreferences), registries.EntitySettingRegistry.getInstance().add(extension.entitySettings), registries.StatusBarRegistry.getInstance().add(extension.statusBarItems), registries.CatalogEntityDetailRegistry.getInstance().add(extension.catalogEntityDetailItems), diff --git a/src/extensions/lens-renderer-extension.ts b/src/extensions/lens-renderer-extension.ts index 9504b7b70a..26d2f2d810 100644 --- a/src/extensions/lens-renderer-extension.ts +++ b/src/extensions/lens-renderer-extension.ts @@ -15,13 +15,14 @@ import type { KubernetesCluster } from "../common/catalog-entities"; import type { WelcomeMenuRegistration } from "../renderer/components/+welcome/welcome-menu-items/welcome-menu-registration"; import type { WelcomeBannerRegistration } from "../renderer/components/+welcome/welcome-banner-items/welcome-banner-registration"; import type { CommandRegistration } from "../renderer/components/command-palette/registered-commands/commands"; +import type { AppPreferenceRegistration } from "../renderer/components/+preferences/app-preferences/app-preference-registration"; export class LensRendererExtension extends LensExtension { globalPages: registries.PageRegistration[] = []; clusterPages: registries.PageRegistration[] = []; clusterPageMenus: registries.ClusterPageMenuRegistration[] = []; kubeObjectStatusTexts: registries.KubeObjectStatusRegistration[] = []; - appPreferences: registries.AppPreferenceRegistration[] = []; + appPreferences: AppPreferenceRegistration[] = []; entitySettings: registries.EntitySettingRegistration[] = []; statusBarItems: registries.StatusBarRegistration[] = []; kubeObjectDetailItems: registries.KubeObjectDetailRegistration[] = []; diff --git a/src/extensions/registries/app-preference-registry.ts b/src/extensions/registries/app-preference-registry.ts deleted file mode 100644 index f533418ca6..0000000000 --- a/src/extensions/registries/app-preference-registry.ts +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -import type React from "react"; -import { BaseRegistry } from "./base-registry"; - -export interface AppPreferenceComponents { - Hint: React.ComponentType; - Input: React.ComponentType; -} - -export interface AppPreferenceRegistration { - title: string; - id?: string; - showInPreferencesTab?: string; - components: AppPreferenceComponents; -} - -export interface RegisteredAppPreference extends AppPreferenceRegistration { - id: string; -} - -export class AppPreferenceRegistry extends BaseRegistry { - getRegisteredItem(item: AppPreferenceRegistration): RegisteredAppPreference { - return { - id: item.id || item.title.toLowerCase().replace(/[^0-9a-zA-Z]+/g, "-"), - ...item, - }; - } -} diff --git a/src/extensions/registries/index.ts b/src/extensions/registries/index.ts index 2d49670973..477f406b2c 100644 --- a/src/extensions/registries/index.ts +++ b/src/extensions/registries/index.ts @@ -7,7 +7,6 @@ export * from "./page-registry"; export * from "./page-menu-registry"; -export * from "./app-preference-registry"; export * from "./status-bar-registry"; export * from "./kube-object-detail-registry"; export * from "./kube-object-menu-registry"; diff --git a/src/renderer/components/+preferences/app-preferences/app-preference-registration.d.ts b/src/renderer/components/+preferences/app-preferences/app-preference-registration.d.ts new file mode 100644 index 0000000000..6e20c14f86 --- /dev/null +++ b/src/renderer/components/+preferences/app-preferences/app-preference-registration.d.ts @@ -0,0 +1,18 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import type React from "react"; + +export interface AppPreferenceComponents { + Hint: React.ComponentType; + Input: React.ComponentType; +} + +export interface AppPreferenceRegistration { + title: string; + id?: string; + showInPreferencesTab?: string; + components: AppPreferenceComponents; +} diff --git a/src/renderer/components/+preferences/app-preferences/app-preferences.injectable.ts b/src/renderer/components/+preferences/app-preferences/app-preferences.injectable.ts new file mode 100644 index 0000000000..eba89793ea --- /dev/null +++ b/src/renderer/components/+preferences/app-preferences/app-preferences.injectable.ts @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; +import rendererExtensionsInjectable from "../../../../extensions/renderer-extensions.injectable"; +import { getAppPreferences } from "./get-app-preferences"; + +const appPreferencesInjectable = getInjectable({ + instantiate: (di) => + getAppPreferences({ + extensions: di.inject(rendererExtensionsInjectable), + }), + + lifecycle: lifecycleEnum.singleton, +}); + +export default appPreferencesInjectable; diff --git a/src/renderer/components/+preferences/app-preferences/get-app-preferences.ts b/src/renderer/components/+preferences/app-preferences/get-app-preferences.ts new file mode 100644 index 0000000000..3557bf55bf --- /dev/null +++ b/src/renderer/components/+preferences/app-preferences/get-app-preferences.ts @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import { computed, IComputedValue } from "mobx"; +import type { LensRendererExtension } from "../../../../extensions/lens-renderer-extension"; +import type { AppPreferenceRegistration } from "./app-preference-registration"; + +interface Dependencies { + extensions: IComputedValue; +} + +interface RegisteredAppPreference extends AppPreferenceRegistration { + id: string; +} + +function getRegisteredItem(item: AppPreferenceRegistration): RegisteredAppPreference { + return { + id: item.id || item.title.toLowerCase().replace(/[^0-9a-zA-Z]+/g, "-"), + ...item, + }; +} + + +export const getAppPreferences = ({ extensions }: Dependencies) => { + return computed(() => { + return [ + ...extensions.get() + .flatMap((extension) => extension.appPreferences) + .map(pref => getRegisteredItem(pref)), + ];}, + ); +}; diff --git a/src/renderer/components/+preferences/application.tsx b/src/renderer/components/+preferences/application.tsx index 99edcfe84e..8e2b09a36c 100644 --- a/src/renderer/components/+preferences/application.tsx +++ b/src/renderer/components/+preferences/application.tsx @@ -14,10 +14,12 @@ import { isWindows } from "../../../common/vars"; import { Switch } from "../switch"; import moment from "moment-timezone"; import { CONSTANTS, defaultExtensionRegistryUrl, ExtensionRegistryLocation } from "../../../common/user-store/preferences-helpers"; -import { action } from "mobx"; +import { action, IComputedValue } from "mobx"; import { isUrl } from "../input/input_validators"; -import { AppPreferenceRegistry } from "../../../extensions/registries"; import { ExtensionSettings } from "./extension-settings"; +import type { AppPreferenceRegistration } from "./app-preferences/app-preference-registration"; +import { withInjectables } from "@ogre-tools/injectable-react"; +import appPreferencesInjectable from "./app-preferences/app-preferences.injectable"; const timezoneOptions: SelectOption[] = moment.tz.names().map(zone => ({ label: zone, @@ -28,7 +30,11 @@ const updateChannelOptions: SelectOption[] = Array.from( ([value, { label }]) => ({ value, label }), ); -export const Application = observer(() => { +interface Dependencies { + appPreferenceItems: IComputedValue +} + +const NonInjectedApplication: React.FC = ({ appPreferenceItems }) => { const userStore = UserStore.getInstance(); const defaultShell = process.env.SHELL || process.env.PTYSHELL @@ -40,7 +46,7 @@ export const Application = observer(() => { const [customUrl, setCustomUrl] = React.useState(userStore.extensionRegistryUrl.customUrl || ""); const [shell, setShell] = React.useState(userStore.shell || ""); - const extensionSettings = AppPreferenceRegistry.getInstance().getItems().filter((preference) => preference.showInPreferencesTab === "application"); + const extensionSettings = appPreferenceItems.get().filter((preference) => preference.showInPreferencesTab === "application"); const themeStore = ThemeStore.getInstance(); return ( @@ -125,10 +131,10 @@ export const Application = observer(() => { /> -
+
- + userStore.openAtLogin = !userStore.openAtLogin}> Automatically start Lens on login @@ -141,7 +147,7 @@ export const Application = observer(() => { ))}
- +