From d20c1dbc703210ec35cef01dda030e54f94c555c Mon Sep 17 00:00:00 2001 From: Masterjoona <69722179+Masterjoona@users.noreply.github.com> Date: Sat, 27 Jul 2024 14:41:17 +0300 Subject: [PATCH 1/3] Add plugin toggling to venbot embeds --- src/components/PluginSettings/index.tsx | 72 ++---------------- .../PluginSettings/togglePluginEnabled.ts | 75 +++++++++++++++++++ src/plugins/_core/supportHelper.tsx | 31 +++++++- 3 files changed, 111 insertions(+), 67 deletions(-) create mode 100644 src/components/PluginSettings/togglePluginEnabled.ts diff --git a/src/components/PluginSettings/index.tsx b/src/components/PluginSettings/index.tsx index 38ddc4a90..e13761530 100644 --- a/src/components/PluginSettings/index.tsx +++ b/src/components/PluginSettings/index.tsx @@ -19,7 +19,6 @@ import "./styles.css"; import * as DataStore from "@api/DataStore"; -import { showNotice } from "@api/Notices"; import { Settings, useSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import { CogWheel, InfoIcon } from "@components/Icons"; @@ -27,38 +26,24 @@ import { openPluginModal } from "@components/PluginSettings/PluginModal"; import { AddonCard } from "@components/VencordSettings/AddonCard"; import { SettingsTab } from "@components/VencordSettings/shared"; import { ChangeList } from "@utils/ChangeList"; -import { proxyLazy } from "@utils/lazy"; -import { Logger } from "@utils/Logger"; import { Margins } from "@utils/margins"; import { classes, isObjectEmpty } from "@utils/misc"; import { useAwaiter } from "@utils/react"; import { Plugin } from "@utils/types"; import { findByPropsLazy } from "@webpack"; -import { Alerts, Button, Card, Forms, lodash, Parser, React, Select, Text, TextInput, Toasts, Tooltip, useMemo } from "@webpack/common"; +import { Alerts, Button, Card, Forms, lodash, Parser, React, Select, Text, TextInput, Tooltip, useMemo } from "@webpack/common"; import Plugins, { ExcludedPlugins } from "~plugins"; -// Avoid circular dependency -const { startDependenciesRecursive, startPlugin, stopPlugin } = proxyLazy(() => require("../../plugins")); +import { togglePluginEnabled } from "./togglePluginEnabled"; + const cl = classNameFactory("vc-plugins-"); -const logger = new Logger("PluginSettings", "#a6d189"); const InputStyles = findByPropsLazy("inputWrapper", "inputDefault", "error"); const ButtonClasses = findByPropsLazy("button", "disabled", "enabled"); -function showErrorToast(message: string) { - Toasts.show({ - message, - type: Toasts.Type.FAILURE, - id: Toasts.genId(), - options: { - position: Toasts.Position.BOTTOM - } - }); -} - function ReloadRequiredCard({ required }: { required: boolean; }) { return ( @@ -91,54 +76,9 @@ interface PluginCardProps extends React.HTMLProps { } export function PluginCard({ plugin, disabled, onRestartNeeded, onMouseEnter, onMouseLeave, isNew }: PluginCardProps) { - const settings = Settings.plugins[plugin.name]; + const isEnabled = () => Settings.plugins[plugin.name].enabled ?? false; - const isEnabled = () => settings.enabled ?? false; - - function toggleEnabled() { - const wasEnabled = isEnabled(); - - // If we're enabling a plugin, make sure all deps are enabled recursively. - if (!wasEnabled) { - const { restartNeeded, failures } = startDependenciesRecursive(plugin); - if (failures.length) { - logger.error(`Failed to start dependencies for ${plugin.name}: ${failures.join(", ")}`); - showNotice("Failed to start dependencies: " + failures.join(", "), "Close", () => null); - return; - } else if (restartNeeded) { - // If any dependencies have patches, don't start the plugin yet. - settings.enabled = true; - onRestartNeeded(plugin.name); - return; - } - } - - // if the plugin has patches, dont use stopPlugin/startPlugin. Wait for restart to apply changes. - if (plugin.patches?.length) { - settings.enabled = !wasEnabled; - onRestartNeeded(plugin.name); - return; - } - - // If the plugin is enabled, but hasn't been started, then we can just toggle it off. - if (wasEnabled && !plugin.started) { - settings.enabled = !wasEnabled; - return; - } - - const result = wasEnabled ? stopPlugin(plugin) : startPlugin(plugin); - - if (!result) { - settings.enabled = false; - - const msg = `Error while ${wasEnabled ? "stopping" : "starting"} plugin ${plugin.name}`; - logger.error(msg); - showErrorToast(msg); - return; - } - - settings.enabled = !wasEnabled; - } + const togglePlugin = () => togglePluginEnabled(isEnabled(), plugin, onRestartNeeded); return ( require("../../plugins")); + + +const logger = new Logger("PluginSettings", "#a6d189"); + +function showErrorToast(message: string) { + Toasts.show({ + message, + type: Toasts.Type.FAILURE, + id: Toasts.genId(), + options: { + position: Toasts.Position.BOTTOM + } + }); +} + +export function togglePluginEnabled(isEnabled: boolean, plugin: Plugin,onRestartNeeded: (pluginName: string) => void) { + const settings = Settings.plugins[plugin.name]; + const wasEnabled = isEnabled; + + // If we're enabling a plugin, make sure all deps are enabled recursively. + if (!wasEnabled) { + const { restartNeeded, failures } = startDependenciesRecursive(plugin); + if (failures.length) { + logger.error(`Failed to start dependencies for ${plugin.name}: ${failures.join(", ")}`); + showNotice("Failed to start dependencies: " + failures.join(", "), "Close", () => null); + return; + } else if (restartNeeded) { + // If any dependencies have patches, don't start the plugin yet. + settings.enabled = true; + onRestartNeeded(plugin.name); + return; + } + } + + // if the plugin has patches, dont use stopPlugin/startPlugin. Wait for restart to apply changes. + if (plugin.patches?.length) { + settings.enabled = !wasEnabled; + onRestartNeeded(plugin.name); + return; + } + + // If the plugin is enabled, but hasn't been started, then we can just toggle it off. + if (wasEnabled && !plugin.started) { + settings.enabled = !wasEnabled; + return; + } + + const result = wasEnabled ? stopPlugin(plugin) : startPlugin(plugin); + + if (!result) { + settings.enabled = false; + + const msg = `Error while ${wasEnabled ? "stopping" : "starting"} plugin ${plugin.name}`; + logger.error(msg); + showErrorToast(msg); + return; + } + + settings.enabled = !wasEnabled; +} diff --git a/src/plugins/_core/supportHelper.tsx b/src/plugins/_core/supportHelper.tsx index de8e37c79..f8e58cafa 100644 --- a/src/plugins/_core/supportHelper.tsx +++ b/src/plugins/_core/supportHelper.tsx @@ -22,12 +22,14 @@ import { getUserSettingLazy } from "@api/UserSettings"; import ErrorBoundary from "@components/ErrorBoundary"; import { Flex } from "@components/Flex"; import { Link } from "@components/Link"; +import { openPluginModal } from "@components/PluginSettings/PluginModal"; +import { togglePluginEnabled } from "@components/PluginSettings/togglePluginEnabled"; import { openUpdaterModal } from "@components/VencordSettings/UpdaterTab"; import { Devs, SUPPORT_CHANNEL_ID } from "@utils/constants"; import { sendMessage } from "@utils/discord"; import { Logger } from "@utils/Logger"; import { Margins } from "@utils/margins"; -import { isPluginDev, tryOrElse } from "@utils/misc"; +import { isObjectEmpty, isPluginDev, tryOrElse } from "@utils/misc"; import { relaunch } from "@utils/native"; import { onlyOnce } from "@utils/onlyOnce"; import { makeCodeblock } from "@utils/text"; @@ -321,6 +323,33 @@ export default definePlugin({ ); } + if (props.message.embeds[0]?.url?.includes("/plugins/")) { + const pluginName = props.message.embeds[0].rawTitle; + const isEnabled = Vencord.Plugins.isPluginEnabled(pluginName); + const toggle = isEnabled ? "Disable" : "Enable"; + const plugin = plugins[pluginName]; + const onRestartNeeded = () => showToast("Restart to apply changes!"); + const togglePlugin = () => togglePluginEnabled(isEnabled, plugin, onRestartNeeded); + buttons.push( + + ); + if (plugin.options && !isObjectEmpty(plugin.options)) { + buttons.push( + + ); + } + + } } } From db670d844cc1cfd1c1be4113e9d1f17cddda8173 Mon Sep 17 00:00:00 2001 From: Masterjoona <69722179+Masterjoona@users.noreply.github.com> Date: Sun, 28 Jul 2024 16:40:46 +0300 Subject: [PATCH 2/3] Excluded required plugins fixes --- src/components/PluginSettings/index.tsx | 10 +-- .../{togglePluginEnabled.ts => utils.ts} | 8 +++ src/plugins/_core/supportHelper.tsx | 63 +++++++++++++------ 3 files changed, 52 insertions(+), 29 deletions(-) rename src/components/PluginSettings/{togglePluginEnabled.ts => utils.ts} (87%) diff --git a/src/components/PluginSettings/index.tsx b/src/components/PluginSettings/index.tsx index e13761530..95ac6dc5a 100644 --- a/src/components/PluginSettings/index.tsx +++ b/src/components/PluginSettings/index.tsx @@ -35,7 +35,7 @@ import { Alerts, Button, Card, Forms, lodash, Parser, React, Select, Text, TextI import Plugins, { ExcludedPlugins } from "~plugins"; -import { togglePluginEnabled } from "./togglePluginEnabled"; +import { ExcludedReasons, togglePluginEnabled } from "./utils"; const cl = classNameFactory("vc-plugins-"); @@ -116,14 +116,6 @@ function ExcludedPluginsList({ search }: { search: string; }) { const matchingExcludedPlugins = Object.entries(ExcludedPlugins) .filter(([name]) => name.toLowerCase().includes(search)); - const ExcludedReasons: Record<"web" | "discordDesktop" | "vencordDesktop" | "desktop" | "dev", string> = { - desktop: "Discord Desktop app or Vesktop", - discordDesktop: "Discord Desktop app", - vencordDesktop: "Vesktop app", - web: "Vesktop app and the Web version of Discord", - dev: "Developer version of Vencord" - }; - return ( {matchingExcludedPlugins.length diff --git a/src/components/PluginSettings/togglePluginEnabled.ts b/src/components/PluginSettings/utils.ts similarity index 87% rename from src/components/PluginSettings/togglePluginEnabled.ts rename to src/components/PluginSettings/utils.ts index 2d7f81391..b53765385 100644 --- a/src/components/PluginSettings/togglePluginEnabled.ts +++ b/src/components/PluginSettings/utils.ts @@ -73,3 +73,11 @@ export function togglePluginEnabled(isEnabled: boolean, plugin: Plugin,onRestart settings.enabled = !wasEnabled; } + +export const ExcludedReasons: Record<"web" | "discordDesktop" | "vencordDesktop" | "desktop" | "dev", string> = { + desktop: "Discord Desktop app or Vesktop", + discordDesktop: "Discord Desktop app", + vencordDesktop: "Vesktop app", + web: "Vesktop app and the Web version of Discord", + dev: "Developer version of Vencord" +}; diff --git a/src/plugins/_core/supportHelper.tsx b/src/plugins/_core/supportHelper.tsx index f8e58cafa..0492fceee 100644 --- a/src/plugins/_core/supportHelper.tsx +++ b/src/plugins/_core/supportHelper.tsx @@ -23,7 +23,7 @@ import ErrorBoundary from "@components/ErrorBoundary"; import { Flex } from "@components/Flex"; import { Link } from "@components/Link"; import { openPluginModal } from "@components/PluginSettings/PluginModal"; -import { togglePluginEnabled } from "@components/PluginSettings/togglePluginEnabled"; +import { ExcludedReasons, togglePluginEnabled } from "@components/PluginSettings/utils"; import { openUpdaterModal } from "@components/VencordSettings/UpdaterTab"; import { Devs, SUPPORT_CHANNEL_ID } from "@utils/constants"; import { sendMessage } from "@utils/discord"; @@ -38,7 +38,7 @@ import { checkForUpdates, isOutdated, update } from "@utils/updater"; import { Alerts, Button, Card, ChannelStore, Forms, GuildMemberStore, Parser, RelationshipStore, showToast, Text, Toasts, UserStore } from "@webpack/common"; import gitHash from "~git-hash"; -import plugins, { PluginMeta } from "~plugins"; +import plugins, { ExcludedPlugins, PluginMeta } from "~plugins"; import SettingsPlugin from "./settings"; @@ -325,30 +325,53 @@ export default definePlugin({ } if (props.message.embeds[0]?.url?.includes("/plugins/")) { const pluginName = props.message.embeds[0].rawTitle; - const isEnabled = Vencord.Plugins.isPluginEnabled(pluginName); - const toggle = isEnabled ? "Disable" : "Enable"; - const plugin = plugins[pluginName]; - const onRestartNeeded = () => showToast("Restart to apply changes!"); - const togglePlugin = () => togglePluginEnabled(isEnabled, plugin, onRestartNeeded); - buttons.push( - - ); - if (plugin.options && !isObjectEmpty(plugin.options)) { + const excludedPlugin = ExcludedPlugins[pluginName]; + if (excludedPlugin) { buttons.push( ); + // button really wide, good ideas? + } else { + const isEnabled = Vencord.Plugins.isPluginEnabled(pluginName); + const toggle = isEnabled ? "Disable" : "Enable"; + const plugin = plugins[pluginName]; + const onRestartNeeded = () => showToast("Restart to apply changes!"); + const togglePlugin = () => togglePluginEnabled(isEnabled, plugin, onRestartNeeded); + if (plugin.required) { + buttons.push( + + ); + } else { + buttons.push( + + ); + } + if (plugin.options && !isObjectEmpty(plugin.options)) { + buttons.push( + + ); + // Should it be Open {pluginName} Settings or just Open Settings? "Open Settings Settings" is dumb + } } - } } } From 01d3660e92798b15d219f07420a1ce770bc13b06 Mon Sep 17 00:00:00 2001 From: Masterjoona <69722179+Masterjoona@users.noreply.github.com> Date: Sat, 10 Aug 2024 17:37:06 +0300 Subject: [PATCH 3/3] Fix wording --- src/plugins/_core/supportHelper.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/plugins/_core/supportHelper.tsx b/src/plugins/_core/supportHelper.tsx index 0492fceee..3e0b7b72f 100644 --- a/src/plugins/_core/supportHelper.tsx +++ b/src/plugins/_core/supportHelper.tsx @@ -366,10 +366,9 @@ export default definePlugin({ key="vc-plugin-settings" onClick={() => openPluginModal(plugin, onRestartNeeded)} > - Open {pluginName} Settings + Open plugin Settings ); - // Should it be Open {pluginName} Settings or just Open Settings? "Open Settings Settings" is dumb } } }