Changes plugin interface from 'roles' to 'enabled' method for more flexibility

This commit is contained in:
Tom Moor
2024-06-24 08:33:48 -04:00
parent 6bb8a3d935
commit 1451f70b9e
6 changed files with 19 additions and 10 deletions

View File

@@ -188,17 +188,21 @@ const useSettingsConfig = () => {
// Plugins // Plugins
PluginManager.getHooks(Hook.Settings).forEach((plugin) => { PluginManager.getHooks(Hook.Settings).forEach((plugin) => {
const group = plugin.value.group ?? "Integrations";
const insertIndex = plugin.value.after const insertIndex = plugin.value.after
? items.findIndex((i) => i.name === t(plugin.value.after!)) + 1 ? items.findIndex((i) => i.name === t(plugin.value.after!)) + 1
: items.findIndex( : items.findIndex((i) => i.group === t(group));
(i) => i.group === t(plugin.value.group ?? "Integrations")
);
items.splice(insertIndex, 0, { items.splice(insertIndex, 0, {
name: t(plugin.name), name: t(plugin.name),
path: integrationSettingsPath(plugin.id), path:
group: t(plugin.value.group), group === "Integrations"
? integrationSettingsPath(plugin.id)
: settingsPath(plugin.id),
group: t(group),
component: plugin.value.component, component: plugin.value.component,
enabled: plugin.roles?.includes(user.role) || can.update, enabled: plugin.value.enabled
? plugin.value.enabled(team, user)
: can.update,
icon: plugin.value.icon, icon: plugin.value.icon,
} as ConfigItem); } as ConfigItem);
}); });

View File

@@ -1,6 +1,8 @@
import isArray from "lodash/isArray"; import isArray from "lodash/isArray";
import sortBy from "lodash/sortBy"; import sortBy from "lodash/sortBy";
import { action, observable } from "mobx"; import { action, observable } from "mobx";
import Team from "~/models/Team";
import User from "~/models/User";
import { useComputed } from "~/hooks/useComputed"; import { useComputed } from "~/hooks/useComputed";
import Logger from "./Logger"; import Logger from "./Logger";
import isCloudHosted from "./isCloudHosted"; import isCloudHosted from "./isCloudHosted";
@@ -26,6 +28,8 @@ type PluginValueMap = {
icon: React.ElementType; icon: React.ElementType;
/** The settings screen somponent, should be lazy loaded. */ /** The settings screen somponent, should be lazy loaded. */
component: React.LazyExoticComponent<React.ComponentType>; component: React.LazyExoticComponent<React.ComponentType>;
/** Whether the plugin is enabled in the current context. */
enabled?: (team: Team, user: User) => boolean;
}; };
[Hook.Icon]: React.ElementType; [Hook.Icon]: React.ElementType;
}; };
@@ -45,8 +49,6 @@ export type Plugin<T extends Hook> = {
priority?: number; priority?: number;
/** The deployments this plugin is enabled for (default: all) */ /** The deployments this plugin is enabled for (default: all) */
deployments?: string[]; deployments?: string[];
/** The roles this plugin is enabled for. (default: admin) */
roles?: string[];
}; };
/** /**

View File

@@ -1,4 +1,5 @@
import * as React from "react"; import * as React from "react";
import { UserRole } from "@shared/types";
import { Hook, PluginManager } from "~/utils/PluginManager"; import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json"; import config from "../plugin.json";
import Icon from "./Icon"; import Icon from "./Icon";
@@ -11,6 +12,7 @@ PluginManager.add([
group: "Integrations", group: "Integrations",
icon: Icon, icon: Icon,
component: React.lazy(() => import("./Settings")), component: React.lazy(() => import("./Settings")),
enabled: (_, user) => user.role === UserRole.Admin,
}, },
}, },
]); ]);

View File

@@ -1,7 +1,6 @@
{ {
"id": "matomo", "id": "matomo",
"name": "Matomo", "name": "Matomo",
"roles": ["admin"],
"priority": 40, "priority": 40,
"description": "Adds support for reporting analytics to a Matomo server.", "description": "Adds support for reporting analytics to a Matomo server.",
"deployments": ["community", "enterprise"] "deployments": ["community", "enterprise"]

View File

@@ -1,4 +1,5 @@
import * as React from "react"; import * as React from "react";
import { UserRole } from "@shared/types";
import { Hook, PluginManager } from "~/utils/PluginManager"; import { Hook, PluginManager } from "~/utils/PluginManager";
import config from "../plugin.json"; import config from "../plugin.json";
import Icon from "./Icon"; import Icon from "./Icon";
@@ -11,6 +12,8 @@ PluginManager.add([
group: "Integrations", group: "Integrations",
icon: Icon, icon: Icon,
component: React.lazy(() => import("./Settings")), component: React.lazy(() => import("./Settings")),
enabled: (_, user) =>
[UserRole.Member, UserRole.Admin].includes(user.role),
}, },
}, },
{ {

View File

@@ -2,6 +2,5 @@
"id": "slack", "id": "slack",
"name": "Slack", "name": "Slack",
"priority": 20, "priority": 20,
"roles": ["admin", "member"],
"description": "Adds a Slack authentication provider, support for the /outline slash command, and link unfurling." "description": "Adds a Slack authentication provider, support for the /outline slash command, and link unfurling."
} }