From f4e2c2de77934dab9b10dbb6992c344d1137f391 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 16 Jun 2024 09:09:02 -0400 Subject: [PATCH] Update clientside plugin management to work as server --- app/components/PluginIcon.tsx | 14 ++- app/hooks/useComputed.ts | 16 ++++ app/hooks/useSettingsConfig.ts | 50 ++++------ app/index.tsx | 4 + app/utils/Logger.ts | 3 +- app/utils/PluginLoader.ts | 54 ----------- app/utils/PluginManager.ts | 116 +++++++++++++++++++++++ plugins/azure/client/index.tsx | 11 +++ plugins/github/client/index.tsx | 16 ++++ plugins/google/client/index.tsx | 11 +++ plugins/googleanalytics/client/index.tsx | 16 ++++ plugins/matomo/client/index.tsx | 16 ++++ plugins/slack/client/index.tsx | 16 ++++ plugins/webhooks/client/index.tsx | 16 ++++ server/utils/PluginManager.ts | 7 +- 15 files changed, 272 insertions(+), 94 deletions(-) create mode 100644 app/hooks/useComputed.ts delete mode 100644 app/utils/PluginLoader.ts create mode 100644 app/utils/PluginManager.ts create mode 100644 plugins/azure/client/index.tsx create mode 100644 plugins/github/client/index.tsx create mode 100644 plugins/google/client/index.tsx create mode 100644 plugins/googleanalytics/client/index.tsx create mode 100644 plugins/matomo/client/index.tsx create mode 100644 plugins/slack/client/index.tsx create mode 100644 plugins/webhooks/client/index.tsx diff --git a/app/components/PluginIcon.tsx b/app/components/PluginIcon.tsx index 53dec09aee..63b0624c51 100644 --- a/app/components/PluginIcon.tsx +++ b/app/components/PluginIcon.tsx @@ -1,6 +1,9 @@ +import { observer } from "mobx-react"; import * as React from "react"; import styled from "styled-components"; -import PluginLoader from "~/utils/PluginLoader"; +import { Hook, PluginManager } from "~/utils/PluginManager"; + +const icons = PluginManager.getHooks(Hook.Icon); type Props = { id: string; @@ -8,9 +11,12 @@ type Props = { color?: string; }; +/** + * Renders an icon defined in a plugin (Hook.Icon). + */ function PluginIcon({ id, color, size = 24 }: Props) { - const plugin = PluginLoader.plugins[id]; - const Icon = plugin?.icon; + const plugin = icons.find((p) => p.id === id); + const Icon = plugin?.value; if (Icon) { return ( @@ -32,4 +38,4 @@ const Wrapper = styled.div` height: 24px; `; -export default PluginIcon; +export default observer(PluginIcon); diff --git a/app/hooks/useComputed.ts b/app/hooks/useComputed.ts new file mode 100644 index 0000000000..187fb8742b --- /dev/null +++ b/app/hooks/useComputed.ts @@ -0,0 +1,16 @@ +import { computed } from "mobx"; +import { type DependencyList, useMemo } from "react"; + +/** + * Hook around MobX computed function that runs computation whenever observable values change. + * + * @param callback Function which returns a memorized value. + * @param inputs Dependency list for useMemo. + */ +export function useComputed( + callback: () => T, + inputs: DependencyList = [] +): T { + const value = useMemo(() => computed(callback), inputs); + return value.get(); +} diff --git a/app/hooks/useSettingsConfig.ts b/app/hooks/useSettingsConfig.ts index c97dfdfc9f..85a244720b 100644 --- a/app/hooks/useSettingsConfig.ts +++ b/app/hooks/useSettingsConfig.ts @@ -1,4 +1,3 @@ -import sortBy from "lodash/sortBy"; import { EmailIcon, ProfileIcon, @@ -20,10 +19,11 @@ import React, { ComponentProps } from "react"; import { useTranslation } from "react-i18next"; import { integrationSettingsPath } from "@shared/utils/routeHelpers"; import ZapierIcon from "~/components/Icons/ZapierIcon"; -import PluginLoader from "~/utils/PluginLoader"; +import { Hook, PluginManager } from "~/utils/PluginManager"; import isCloudHosted from "~/utils/isCloudHosted"; import lazy from "~/utils/lazyWithRetry"; import { settingsPath } from "~/utils/routeHelpers"; +import { useComputed } from "./useComputed"; import useCurrentTeam from "./useCurrentTeam"; import useCurrentUser from "./useCurrentUser"; import usePolicy from "./usePolicy"; @@ -59,7 +59,7 @@ const useSettingsConfig = () => { const can = usePolicy(team); const { t } = useTranslation(); - const config = React.useMemo(() => { + const config = useComputed(() => { const items: ConfigItem[] = [ // Account { @@ -187,37 +187,19 @@ const useSettingsConfig = () => { ]; // Plugins - const insertIndex = items.findIndex((i) => i.group === t("Integrations")); - items.splice( - insertIndex, - 0, - ...(sortBy( - Object.values(PluginLoader.plugins), - (plugin) => plugin.config?.priority ?? 0 - ).map((plugin) => { - const hasSettings = !!plugin.settings; - const enabledInDeployment = - !plugin.config?.deployments || - plugin.config.deployments.length === 0 || - (plugin.config.deployments.includes("community") && !isCloudHosted) || - (plugin.config.deployments.includes("cloud") && isCloudHosted) || - (plugin.config.deployments.includes("enterprise") && !isCloudHosted); - - return { - name: t(plugin.config.name), - path: integrationSettingsPath(plugin.id), - // TODO: Remove hardcoding of plugin id here - group: - plugin.id === "collections" ? t("Workspace") : t("Integrations"), - component: plugin.settings, - enabled: - enabledInDeployment && - hasSettings && - (plugin.config.roles?.includes(user.role) || can.update), - icon: plugin.icon, - }; - }) as ConfigItem[]) - ); + PluginManager.getHooks(Hook.Settings).forEach((plugin) => { + const insertIndex = items.findIndex( + (i) => i.group === t(plugin.value.group ?? "Integrations") + ); + items.splice(insertIndex, 0, { + name: t(plugin.name), + path: integrationSettingsPath(plugin.id), + group: t(plugin.value.group), + component: plugin.value.component, + enabled: plugin.roles?.includes(user.role) || can.update, + icon: plugin.value.icon, + } as ConfigItem); + }); return items; }, [t, can.createApiKey, can.update, can.createImport, can.createExport]); diff --git a/app/index.tsx b/app/index.tsx index 406b37d6b3..2f8c8c5d23 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -23,9 +23,13 @@ import LazyPolyfill from "./components/LazyPolyfills"; import PageScroll from "./components/PageScroll"; import Routes from "./routes"; import Logger from "./utils/Logger"; +import { PluginManager } from "./utils/PluginManager"; import history from "./utils/history"; import { initSentry } from "./utils/sentry"; +// Load plugins as soon as possible +void PluginManager.loadPlugins(); + initI18n(env.DEFAULT_LANGUAGE); const element = window.document.getElementById("root"); diff --git a/app/utils/Logger.ts b/app/utils/Logger.ts index ef371ff379..a3b6ba26d4 100644 --- a/app/utils/Logger.ts +++ b/app/utils/Logger.ts @@ -8,7 +8,8 @@ type LogCategory = | "editor" | "router" | "collaboration" - | "misc"; + | "misc" + | "plugins"; type Extra = Record; diff --git a/app/utils/PluginLoader.ts b/app/utils/PluginLoader.ts deleted file mode 100644 index 6593a40792..0000000000 --- a/app/utils/PluginLoader.ts +++ /dev/null @@ -1,54 +0,0 @@ -import React from "react"; -import { UserRole } from "@shared/types"; - -interface Plugin { - id: string; - config: { - name: string; - description: string; - roles?: UserRole[]; - deployments?: string[]; - priority?: number; - }; - settings: React.FC; - icon: React.FC<{ size?: number; fill?: string }>; -} - -export default class PluginLoader { - private static pluginsCache: { [id: string]: Plugin }; - - public static get plugins(): { [id: string]: Plugin } { - if (this.pluginsCache) { - return this.pluginsCache; - } - const plugins = {}; - - function importAll(r: any, property: string) { - Object.keys(r).forEach((key: string) => { - const id = key.split("/")[3]; - plugins[id] = plugins[id] || { - id, - }; - plugins[id][property] = r[key].default ?? React.lazy(r[key]); - }); - } - - importAll( - import.meta.glob("../../plugins/*/client/Settings.{ts,js,tsx,jsx}"), - "settings" - ); - importAll( - import.meta.glob("../../plugins/*/client/Icon.{ts,js,tsx,jsx}", { - eager: true, - }), - "icon" - ); - importAll( - import.meta.glob("../../plugins/*/plugin.json", { eager: true }), - "config" - ); - - this.pluginsCache = plugins; - return plugins; - } -} diff --git a/app/utils/PluginManager.ts b/app/utils/PluginManager.ts new file mode 100644 index 0000000000..731278a9b3 --- /dev/null +++ b/app/utils/PluginManager.ts @@ -0,0 +1,116 @@ +import isArray from "lodash/isArray"; +import sortBy from "lodash/sortBy"; +import { observable } from "mobx"; +import Logger from "./Logger"; +import isCloudHosted from "./isCloudHosted"; + +/** + * The different types of client plugins that can be registered. + */ +export enum Hook { + Settings = "settings", + Icon = "icon", +} + +/** + * A map of plugin types to their values, each plugin type has a different shape of value. + */ +type PluginValueMap = { + [Hook.Settings]: { + group: string; + icon: React.ElementType; + component: React.LazyExoticComponent>; + }; + [Hook.Icon]: React.ElementType; +}; + +export type Plugin = { + /** A unique identifier for the plugin */ + id: string; + /** Plugin type */ + type: T; + /** The plugin's display name */ + name: string; + /** A brief description of the plugin */ + description?: string; + /** The plugin content */ + value: PluginValueMap[T]; + /** Priority will affect order in menus and execution. Lower is earlier. */ + priority?: number; + /** The deployments this plugin is enabled for (default: all) */ + deployments?: string[]; + /** The roles this plugin is enabled for. (default: admin) */ + roles?: string[]; +}; + +/** + * Client plugin manager. + */ +export class PluginManager { + /** + * Add plugins + * @param plugins + */ + public static add(plugins: Array> | Plugin) { + if (isArray(plugins)) { + return plugins.forEach((plugin) => this.register(plugin)); + } + + this.register(plugins); + } + + private static register(plugin: Plugin) { + const enabledInDeployment = + !plugin?.deployments || + plugin.deployments.length === 0 || + (plugin.deployments.includes("cloud") && isCloudHosted) || + (plugin.deployments.includes("community") && !isCloudHosted) || + (plugin.deployments.includes("enterprise") && !isCloudHosted); + if (!enabledInDeployment) { + return; + } + + if (!this.plugins.has(plugin.type)) { + this.plugins.set(plugin.type, []); + } + + this.plugins + .get(plugin.type)! + .push({ ...plugin, priority: plugin.priority ?? 0 }); + + Logger.debug( + "plugins", + `Plugin(type=${plugin.type}) registered ${plugin.name} ${ + plugin.description ? `(${plugin.description})` : "" + }` + ); + } + + /** + * Returns all the plugins of a given type in order of priority. + * + * @param type The type of plugin to filter by + * @returns A list of plugins + */ + public static getHooks(type: T) { + return sortBy(this.plugins.get(type) || [], "priority") as Plugin[]; + } + + /** + * Load plugin client components, must be in `//client/index.ts` + */ + public static async loadPlugins() { + if (this.loaded) { + return; + } + + const r = import.meta.glob("../../plugins/*/client/index.{ts,js,tsx,jsx}"); + await Promise.all(Object.keys(r).map((key: string) => r[key]())); + + this.loaded = true; + } + + private static plugins = observable.map[]>(); + + private static loaded = false; +} diff --git a/plugins/azure/client/index.tsx b/plugins/azure/client/index.tsx new file mode 100644 index 0000000000..c7e5c49c5e --- /dev/null +++ b/plugins/azure/client/index.tsx @@ -0,0 +1,11 @@ +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Icon, + value: Icon, + }, +]); diff --git a/plugins/github/client/index.tsx b/plugins/github/client/index.tsx new file mode 100644 index 0000000000..8700687cd2 --- /dev/null +++ b/plugins/github/client/index.tsx @@ -0,0 +1,16 @@ +import * as React from "react"; +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Settings, + value: { + group: "Integrations", + icon: Icon, + component: React.lazy(() => import("./Settings")), + }, + }, +]); diff --git a/plugins/google/client/index.tsx b/plugins/google/client/index.tsx new file mode 100644 index 0000000000..c7e5c49c5e --- /dev/null +++ b/plugins/google/client/index.tsx @@ -0,0 +1,11 @@ +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Icon, + value: Icon, + }, +]); diff --git a/plugins/googleanalytics/client/index.tsx b/plugins/googleanalytics/client/index.tsx new file mode 100644 index 0000000000..8700687cd2 --- /dev/null +++ b/plugins/googleanalytics/client/index.tsx @@ -0,0 +1,16 @@ +import * as React from "react"; +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Settings, + value: { + group: "Integrations", + icon: Icon, + component: React.lazy(() => import("./Settings")), + }, + }, +]); diff --git a/plugins/matomo/client/index.tsx b/plugins/matomo/client/index.tsx new file mode 100644 index 0000000000..8700687cd2 --- /dev/null +++ b/plugins/matomo/client/index.tsx @@ -0,0 +1,16 @@ +import * as React from "react"; +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Settings, + value: { + group: "Integrations", + icon: Icon, + component: React.lazy(() => import("./Settings")), + }, + }, +]); diff --git a/plugins/slack/client/index.tsx b/plugins/slack/client/index.tsx new file mode 100644 index 0000000000..8700687cd2 --- /dev/null +++ b/plugins/slack/client/index.tsx @@ -0,0 +1,16 @@ +import * as React from "react"; +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Settings, + value: { + group: "Integrations", + icon: Icon, + component: React.lazy(() => import("./Settings")), + }, + }, +]); diff --git a/plugins/webhooks/client/index.tsx b/plugins/webhooks/client/index.tsx new file mode 100644 index 0000000000..8700687cd2 --- /dev/null +++ b/plugins/webhooks/client/index.tsx @@ -0,0 +1,16 @@ +import * as React from "react"; +import { Hook, PluginManager } from "~/utils/PluginManager"; +import config from "../plugin.json"; +import Icon from "./Icon"; + +export default PluginManager.add([ + { + ...config, + type: Hook.Settings, + value: { + group: "Integrations", + icon: Icon, + component: React.lazy(() => import("./Settings")), + }, + }, +]); diff --git a/server/utils/PluginManager.ts b/server/utils/PluginManager.ts index 4634e3ae08..2db8d7010a 100644 --- a/server/utils/PluginManager.ts +++ b/server/utils/PluginManager.ts @@ -58,10 +58,15 @@ export type Plugin = { priority?: number; }; +/** + * Server plugin manager. + */ export class PluginManager { private static plugins = new Map[]>(); + /** - * Add plugins + * Add plugins to the manager. + * * @param plugins */ public static add(plugins: Array> | Plugin) {