From 23aa6876d17559bce1319942cc7db7fd14206b86 Mon Sep 17 00:00:00 2001 From: Zerebos Date: Fri, 6 Dec 2024 19:40:00 -0500 Subject: [PATCH] Fix components api and allow custom inputs --- renderer/src/modules/api/index.js | 38 ++++++++++--------- renderer/src/modules/api/ui.js | 6 ++- renderer/src/ui/customcss/editor.jsx | 2 +- renderer/src/ui/settings/addoncard.jsx | 2 +- .../src/ui/settings/components/switch.jsx | 2 +- renderer/src/ui/settings/group.jsx | 23 +++++------ 6 files changed, 40 insertions(+), 33 deletions(-) diff --git a/renderer/src/modules/api/index.js b/renderer/src/modules/api/index.js index 84dd7a30..dc14e7d7 100644 --- a/renderer/src/modules/api/index.js +++ b/renderer/src/modules/api/index.js @@ -69,22 +69,6 @@ export default class BdApi { get UI() {return UI;} get ReactUtils() {return ReactUtils;} get ContextMenu() {return ContextMenuAPI;} - Components = { - get Tooltip() {return DiscordModules.Tooltip;}, - get ColorInput() {return ColorInput;}, - get DropdownInput() {return DropdownInput;}, - get SettingItem() {return SettingItem;}, - get KeybindInput() {return KeybindInput;}, - get NumberInput() {return NumberInput;}, - get RadioInput() {return RadioInput;}, - get SearchInput() {return SearchInput;}, - get SliderInput() {return SliderInput;}, - get SwitchInput() {return SwitchInput;}, - get TextInput() {return TextInput;}, - get SettingGroup() {return SettingGroup;}, - get ErrorBoundary() {return ErrorBoundary;}, - }; - Net = {fetch}; } // Add legacy functions @@ -150,10 +134,30 @@ BdApi.DOM = DOMAPI; */ BdApi.ContextMenu = ContextMenuAPI; +/** + * An set of react components plugins can make use of. + * @type Components + */ BdApi.Components = { - get Tooltip() {return DiscordModules.Tooltip;} + get Tooltip() {return DiscordModules.Tooltip;}, + get ColorInput() {return ColorInput;}, + get DropdownInput() {return DropdownInput;}, + get SettingItem() {return SettingItem;}, + get KeybindInput() {return KeybindInput;}, + get NumberInput() {return NumberInput;}, + get RadioInput() {return RadioInput;}, + get SearchInput() {return SearchInput;}, + get SliderInput() {return SliderInput;}, + get SwitchInput() {return SwitchInput;}, + get TextInput() {return TextInput;}, + get SettingGroup() {return SettingGroup;}, + get ErrorBoundary() {return ErrorBoundary;}, }; +/** + * An instance of {@link Net} for using network related tools. + * @type Net + */ BdApi.Net = {fetch}; Object.freeze(BdApi); diff --git a/renderer/src/modules/api/ui.js b/renderer/src/modules/api/ui.js index 60175075..bc6cccb2 100644 --- a/renderer/src/modules/api/ui.js +++ b/renderer/src/modules/api/ui.js @@ -146,13 +146,15 @@ const UI = { * The shape of the object should match the props of the component you want to render, check the * `BdApi.Components` section for details. Shown below are ones common to all setting types. * @param {object} setting + * @param {string} setting.type One of: dropdown, number, switch, text, slider, radio, keybind, color, custom * @param {string} setting.id Identifier to used for callbacks * @param {string} setting.name Visual name to display * @param {string} setting.note Visual description to display * @param {any} setting.value Current value of the setting + * @param {ReactElement} [setting.children] Only used for "custom" type * @param {CallableFunction} [setting.onChange] Callback when the value changes (only argument is new value) - * @param {boolean} [setting.disabled] Whether this setting is disabled - + * @param {boolean} [setting.disabled=false] Whether this setting is disabled + * @param {boolean} [setting.inline=true] Whether the input should render inline with the name (this is false by default only for radio type) * @returns A SettingItem with a an input as the child */ buildSetting(setting) { diff --git a/renderer/src/ui/customcss/editor.jsx b/renderer/src/ui/customcss/editor.jsx index 6062c965..647ad99c 100644 --- a/renderer/src/ui/customcss/editor.jsx +++ b/renderer/src/ui/customcss/editor.jsx @@ -24,7 +24,7 @@ function makeButton(button, value) { function makeSwitch(control) { return {control.label} - + ; } diff --git a/renderer/src/ui/settings/addoncard.jsx b/renderer/src/ui/settings/addoncard.jsx index 47181206..269b16b8 100644 --- a/renderer/src/ui/settings/addoncard.jsx +++ b/renderer/src/ui/settings/addoncard.jsx @@ -170,7 +170,7 @@ export default function AddonCard({addon, prefix, type, disabled, enabled: initi
{type === "plugin" ? : }
{title}
- +
{disabled &&
{`An error was encountered while trying to load this ${type}.`}
} diff --git a/renderer/src/ui/settings/components/switch.jsx b/renderer/src/ui/settings/components/switch.jsx index 398a6ae9..7cb06d84 100644 --- a/renderer/src/ui/settings/components/switch.jsx +++ b/renderer/src/ui/settings/components/switch.jsx @@ -3,7 +3,7 @@ import React from "@modules/react"; const {useState, useCallback} = React; -export default function Switch({id, checked: initialValue, disabled, onChange, internalState = true}) { +export default function Switch({id, value: initialValue, disabled, onChange, internalState = true}) { const [checked, setChecked] = useState(initialValue); const change = useCallback(() => { onChange?.(!checked); diff --git a/renderer/src/ui/settings/group.jsx b/renderer/src/ui/settings/group.jsx index 9e270122..a0fdddd9 100644 --- a/renderer/src/ui/settings/group.jsx +++ b/renderer/src/ui/settings/group.jsx @@ -31,15 +31,16 @@ export default function Group({onChange, id, name, button, shown, onDrawerToggle export function buildSetting(setting) { - let component = null; - if (setting.type == "dropdown") component = ; - if (setting.type == "number") component = ; - if (setting.type == "switch") component = ; - if (setting.type == "text") component = ; - if (setting.type == "slider") component = ; - if (setting.type == "radio") component = ; - if (setting.type == "keybind") component = ; - if (setting.type == "color") component = ; - if (!component) return null; - return {component}; + let children = null; + if (setting.type == "dropdown") children = ; + if (setting.type == "number") children = ; + if (setting.type == "switch") children = ; + if (setting.type == "text") children = ; + if (setting.type == "slider") children = ; + if (setting.type == "radio") children = ; + if (setting.type == "keybind") children = ; + if (setting.type == "color") children = ; + if (setting.type == "custom") children = setting.children; + if (!children) return null; + return {children}; } \ No newline at end of file