From 5ad35c36e458fb49532da199fcee2e01ac640e44 Mon Sep 17 00:00:00 2001 From: Nuckyz <61953774+Nuckyz@users.noreply.github.com> Date: Wed, 29 Jan 2025 14:34:44 -0300 Subject: [PATCH] Make Option.Component not require description Also correctly infers the type from "default" --- src/components/PluginSettings/PluginModal.tsx | 3 ++- .../components/SettingCustomComponent.tsx | 4 +-- .../PluginSettings/components/index.ts | 5 +++- src/plugins/clientTheme/index.tsx | 5 +--- src/plugins/decor/settings.tsx | 1 - src/plugins/ignoreActivities/index.tsx | 4 +-- src/plugins/messageLinkEmbeds/index.tsx | 4 +-- src/plugins/reviewDB/settings.tsx | 2 -- src/plugins/textReplace/index.tsx | 1 - src/utils/types.ts | 25 +++++++++++-------- 10 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/components/PluginSettings/PluginModal.tsx b/src/components/PluginSettings/PluginModal.tsx index 3f7965d58..7baeba081 100644 --- a/src/components/PluginSettings/PluginModal.tsx +++ b/src/components/PluginSettings/PluginModal.tsx @@ -37,6 +37,7 @@ import { Constructor } from "type-fest"; import { PluginMeta } from "~plugins"; import { + ISettingCustomElementProps, ISettingElementProps, SettingBooleanComponent, SettingCustomComponent, @@ -74,7 +75,7 @@ function makeDummyUser(user: { username: string; id?: string; avatar?: string; } return newUser; } -const Components: Record>> = { +const Components: Record | ISettingCustomElementProps>> = { [OptionType.STRING]: SettingTextComponent, [OptionType.NUMBER]: SettingNumericComponent, [OptionType.BIGINT]: SettingNumericComponent, diff --git a/src/components/PluginSettings/components/SettingCustomComponent.tsx b/src/components/PluginSettings/components/SettingCustomComponent.tsx index af7192f3f..25e8c9c6a 100644 --- a/src/components/PluginSettings/components/SettingCustomComponent.tsx +++ b/src/components/PluginSettings/components/SettingCustomComponent.tsx @@ -18,8 +18,8 @@ import { PluginOptionComponent } from "@utils/types"; -import { ISettingElementProps } from "."; +import { ISettingCustomElementProps } from "."; -export function SettingCustomComponent({ option, onChange, onError }: ISettingElementProps) { +export function SettingCustomComponent({ option, onChange, onError }: ISettingCustomElementProps) { return option.component({ setValue: onChange, setError: onError, option }); } diff --git a/src/components/PluginSettings/components/index.ts b/src/components/PluginSettings/components/index.ts index d307b4e68..c38f209b7 100644 --- a/src/components/PluginSettings/components/index.ts +++ b/src/components/PluginSettings/components/index.ts @@ -18,7 +18,7 @@ import { DefinedSettings, PluginOptionBase } from "@utils/types"; -export interface ISettingElementProps { +interface ISettingElementPropsBase { option: T; onChange(newValue: any): void; pluginSettings: { @@ -30,6 +30,9 @@ export interface ISettingElementProps { definedSettings?: DefinedSettings; } +export type ISettingElementProps = ISettingElementPropsBase; +export type ISettingCustomElementProps> = ISettingElementPropsBase; + export * from "../../Badge"; export * from "./SettingBooleanComponent"; export * from "./SettingCustomComponent"; diff --git a/src/plugins/clientTheme/index.tsx b/src/plugins/clientTheme/index.tsx index 4c1668aae..2dc7ccf6c 100644 --- a/src/plugins/clientTheme/index.tsx +++ b/src/plugins/clientTheme/index.tsx @@ -91,15 +91,12 @@ function ThemeSettings() { const settings = definePluginSettings({ color: { - description: "Color your Discord client theme will be based around. Light mode isn't supported", type: OptionType.COMPONENT, default: "313338", - component: () => + component: ThemeSettings }, resetColor: { - description: "Reset Theme Color", type: OptionType.COMPONENT, - default: "313338", component: () => ( + ) } }); diff --git a/src/plugins/reviewDB/settings.tsx b/src/plugins/reviewDB/settings.tsx index eeebd0aa1..2b58d080c 100644 --- a/src/plugins/reviewDB/settings.tsx +++ b/src/plugins/reviewDB/settings.tsx @@ -27,7 +27,6 @@ import { cl } from "./utils"; export const settings = definePluginSettings({ authorize: { type: OptionType.COMPONENT, - description: "Authorize with ReviewDB", component: () => ( diff --git a/src/plugins/textReplace/index.tsx b/src/plugins/textReplace/index.tsx index 3d1e891d1..4bec9b6f9 100644 --- a/src/plugins/textReplace/index.tsx +++ b/src/plugins/textReplace/index.tsx @@ -45,7 +45,6 @@ const makeEmptyRuleArray = () => [makeEmptyRule()]; const settings = definePluginSettings({ replace: { type: OptionType.COMPONENT, - description: "", component: () => { const { stringRules, regexRules } = settings.use(["stringRules", "regexRules"]); diff --git a/src/utils/types.ts b/src/utils/types.ts index 54de59e34..8f0ec3860 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -198,15 +198,16 @@ export type SettingsChecks = { (IsDisabled> & IsValid, DefinedSettings>); }; -export type PluginSettingDef = (PluginSettingCustomDef & Pick) | (( - | PluginSettingStringDef - | PluginSettingNumberDef - | PluginSettingBooleanDef - | PluginSettingSelectDef - | PluginSettingSliderDef - | PluginSettingComponentDef - | PluginSettingBigIntDef -) & PluginSettingCommon); +export type PluginSettingDef = + (PluginSettingCustomDef & Pick) | + (PluginSettingComponentDef & Omit) | (( + | PluginSettingStringDef + | PluginSettingNumberDef + | PluginSettingBooleanDef + | PluginSettingSelectDef + | PluginSettingSliderDef + | PluginSettingBigIntDef + ) & PluginSettingCommon); export interface PluginSettingCommon { description: string; @@ -226,12 +227,14 @@ export interface PluginSettingCommon { */ target?: "WEB" | "DESKTOP" | "BOTH"; } + interface IsDisabled { /** * Checks if this setting should be disabled */ disabled?(this: D): boolean; } + interface IsValid { /** * Prevents the user from saving settings if this is false or a string @@ -320,7 +323,7 @@ type PluginSettingType = O extends PluginSettingStri O extends PluginSettingBooleanDef ? boolean : O extends PluginSettingSelectDef ? O["options"][number]["value"] : O extends PluginSettingSliderDef ? number : - O extends PluginSettingComponentDef ? any : + O extends PluginSettingComponentDef ? O extends { default: infer Default; } ? Default : any : O extends PluginSettingCustomDef ? O extends { default: infer Default; } ? Default : any : never; @@ -382,7 +385,7 @@ export type PluginOptionNumber = (PluginSettingNumberDef | PluginSettingBigIntDe export type PluginOptionBoolean = PluginSettingBooleanDef & PluginSettingCommon & IsDisabled & IsValid; export type PluginOptionSelect = PluginSettingSelectDef & PluginSettingCommon & IsDisabled & IsValid; export type PluginOptionSlider = PluginSettingSliderDef & PluginSettingCommon & IsDisabled & IsValid; -export type PluginOptionComponent = PluginSettingComponentDef & PluginSettingCommon; +export type PluginOptionComponent = PluginSettingComponentDef & Omit; export type PluginOptionCustom = PluginSettingCustomDef & Pick; export type PluginNative any>> = {