From ea748dfb605386b80a4919183ad6fa9249a82e21 Mon Sep 17 00:00:00 2001 From: Justice Almanzar Date: Fri, 13 Jan 2023 17:15:45 -0500 Subject: feat: Typesafe Settings Definitions (#403) Co-authored-by: Ven --- src/components/PluginSettings/PluginModal.tsx | 1 + .../PluginSettings/components/SettingBooleanComponent.tsx | 6 +++--- .../PluginSettings/components/SettingNumericComponent.tsx | 6 +++--- src/components/PluginSettings/components/SettingSelectComponent.tsx | 6 +++--- src/components/PluginSettings/components/SettingSliderComponent.tsx | 6 +++--- src/components/PluginSettings/components/SettingTextComponent.tsx | 6 +++--- src/components/PluginSettings/components/index.ts | 3 ++- 7 files changed, 18 insertions(+), 16 deletions(-) (limited to 'src/components/PluginSettings') diff --git a/src/components/PluginSettings/PluginModal.tsx b/src/components/PluginSettings/PluginModal.tsx index 4656850..43e1d31 100644 --- a/src/components/PluginSettings/PluginModal.tsx +++ b/src/components/PluginSettings/PluginModal.tsx @@ -144,6 +144,7 @@ export default function PluginModal({ plugin, onRestartNeeded, onClose, transiti onChange={onChange} onError={onError} pluginSettings={pluginSettings} + definedSettings={plugin.settings} /> ); }); diff --git a/src/components/PluginSettings/components/SettingBooleanComponent.tsx b/src/components/PluginSettings/components/SettingBooleanComponent.tsx index 0aaafa0..c90af16 100644 --- a/src/components/PluginSettings/components/SettingBooleanComponent.tsx +++ b/src/components/PluginSettings/components/SettingBooleanComponent.tsx @@ -21,7 +21,7 @@ import { Forms, React, Select } from "@webpack/common"; import { ISettingElementProps } from "."; -export function SettingBooleanComponent({ option, pluginSettings, id, onChange, onError }: ISettingElementProps) { +export function SettingBooleanComponent({ option, pluginSettings, definedSettings, id, onChange, onError }: ISettingElementProps) { const def = pluginSettings[id] ?? option.default; const [state, setState] = React.useState(def ?? false); @@ -37,7 +37,7 @@ export function SettingBooleanComponent({ option, pluginSettings, id, onChange, ]; function handleChange(newValue: boolean): void { - const isValid = (option.isValid && option.isValid(newValue)) ?? true; + const isValid = option.isValid?.call(definedSettings, newValue) ?? true; if (typeof isValid === "string") setError(isValid); else if (!isValid) setError("Invalid input provided."); else { @@ -51,7 +51,7 @@ export function SettingBooleanComponent({ option, pluginSettings, id, onChange, {option.description} ) { +export function SettingSliderComponent({ option, pluginSettings, definedSettings, id, onChange, onError }: ISettingElementProps) { const def = pluginSettings[id] ?? option.default; const [error, setError] = React.useState(null); @@ -39,7 +39,7 @@ export function SettingSliderComponent({ option, pluginSettings, id, onChange, o }, [error]); function handleChange(newValue: number): void { - const isValid = (option.isValid && option.isValid(newValue)) ?? true; + const isValid = option.isValid?.call(definedSettings, newValue) ?? true; if (typeof isValid === "string") setError(isValid); else if (!isValid) setError("Invalid input provided."); else { @@ -52,7 +52,7 @@ export function SettingSliderComponent({ option, pluginSettings, id, onChange, o {option.description} ) { +export function SettingTextComponent({ option, pluginSettings, definedSettings, id, onChange, onError }: ISettingElementProps) { const [state, setState] = React.useState(pluginSettings[id] ?? option.default ?? null); const [error, setError] = React.useState(null); @@ -30,7 +30,7 @@ export function SettingTextComponent({ option, pluginSettings, id, onChange, onE }, [error]); function handleChange(newValue) { - const isValid = (option.isValid && option.isValid(newValue)) ?? true; + const isValid = option.isValid?.call(definedSettings, newValue) ?? true; if (typeof isValid === "string") setError(isValid); else if (!isValid) setError("Invalid input provided."); else { @@ -47,7 +47,7 @@ export function SettingTextComponent({ option, pluginSettings, id, onChange, onE value={state} onChange={handleChange} placeholder={option.placeholder ?? "Enter a value"} - disabled={option.disabled?.() ?? false} + disabled={option.disabled?.call(definedSettings) ?? false} {...option.componentProps} /> {error && {error}} diff --git a/src/components/PluginSettings/components/index.ts b/src/components/PluginSettings/components/index.ts index d44fb38..52745ea 100644 --- a/src/components/PluginSettings/components/index.ts +++ b/src/components/PluginSettings/components/index.ts @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { PluginOptionBase } from "@utils/types"; +import { DefinedSettings, PluginOptionBase } from "@utils/types"; export interface ISettingElementProps { option: T; @@ -27,6 +27,7 @@ export interface ISettingElementProps { }; id: string; onError(hasError: boolean): void; + definedSettings?: DefinedSettings; } export * from "./BadgeComponent"; -- cgit