diff --git a/src/components/PluginSettings/PluginModal.tsx b/src/components/PluginSettings/PluginModal.tsx index 43558ef1..b96a5ea9 100644 --- a/src/components/PluginSettings/PluginModal.tsx +++ b/src/components/PluginSettings/PluginModal.tsx @@ -30,7 +30,7 @@ import { classes, isObjectEmpty } from "@utils/misc"; import { ModalCloseButton, ModalContent, ModalFooter, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; import { OptionType, Plugin } from "@utils/types"; import { findByPropsLazy, findComponentByCodeLazy } from "@webpack"; -import { Button, Clickable, FluxDispatcher, Forms, React, Text, Tooltip, UserStore, UserUtils } from "@webpack/common"; +import { Button, Clickable, FluxDispatcher, Forms, React, Text, Toasts, Tooltip, UserStore, UserUtils } from "@webpack/common"; import { User } from "discord-types/general"; import { Constructor } from "type-fest"; @@ -139,6 +139,10 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti onClose(); } + function handleResetClick() { + openWarningModal(plugin, { onClose, transitionState }, onRestartNeeded); + } + function renderSettings() { if (!hasSettings || !plugin.options) { return There are no settings for this plugin.; @@ -279,29 +283,45 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti {hasSettings && - - - + + {({ onMouseEnter, onMouseLeave }) => ( )} + + + + {({ onMouseEnter, onMouseLeave }) => ( + + )} + + {saveError && Error while saving: {saveError}} @@ -319,3 +339,125 @@ export function openPluginModal(plugin: Plugin, onRestartNeeded?: (pluginName: s /> )); } + +function resetSettings(plugin: Plugin, warningModalProps?: ModalProps, pluginModalProps?: ModalProps, onRestartNeeded?: (pluginName: string) => void) { + const defaultSettings = plugin.settings?.def; + const pluginName = plugin.name; + + if (!defaultSettings) { + console.error(`No default settings found for ${pluginName}`); + return; + } + + const newSettings: Record = {}; + let restartNeeded = false; + + for (const key in defaultSettings) { + if (key === "enabled") continue; + + const setting = defaultSettings[key]; + setting.type = setting.type ?? OptionType.STRING; + + if (setting.type === OptionType.STRING) { + newSettings[key] = setting.default !== undefined && setting.default !== "" ? setting.default : ""; + } else if ("default" in setting && setting.default !== undefined) { + newSettings[key] = setting.default; + } + + if (setting?.restartNeeded) { + restartNeeded = true; + } + } + + + const currentSettings = plugin.settings?.store; + if (currentSettings) { + Object.assign(currentSettings, newSettings); + } + + if (plugin.afterSave) { + plugin.afterSave(); + } + + if (restartNeeded) { + onRestartNeeded?.(plugin.name); + } + + Toasts.show({ + message: `Settings for ${pluginName} have been reset.`, + id: Toasts.genId(), + type: Toasts.Type.SUCCESS, + options: { + position: Toasts.Position.TOP + } + }); + + warningModalProps?.onClose(); + pluginModalProps?.onClose(); +} + +export function openWarningModal(plugin: Plugin, pluginModalProps: ModalProps, onRestartNeeded?: (pluginName: string) => void) { + openModal(warningModalProps => ( + + + Warning: Dangerous Action + + + + + + Warning + + You are about to reset all settings for {plugin.name} to their default values. + + + This action is irreversible. + + + If you are certain you want to proceed, click Confirm Reset. Otherwise, click Cancel. + + + + + + + + + + {({ onMouseEnter, onMouseLeave }) => ( + + )} + + + + + + )); +} diff --git a/src/components/PluginSettings/index.tsx b/src/components/PluginSettings/index.tsx index eac7bcbc..4114e42b 100644 --- a/src/components/PluginSettings/index.tsx +++ b/src/components/PluginSettings/index.tsx @@ -386,6 +386,76 @@ export default function PluginSettings() { Plugins + {enabledPlugins.length > 0 && ( + + )} + {plugins.length || requiredPlugins.length ? (