diff options
author | Justice Almanzar <superdash993@gmail.com> | 2023-01-13 17:15:45 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-13 23:15:45 +0100 |
commit | ea748dfb605386b80a4919183ad6fa9249a82e21 (patch) | |
tree | 8660c5d192ac553e7574d6b510e18f99c7ac5ddd /src/api/settings.ts | |
parent | 6c5fcc4119d05389bbc71bd3e52090f6fd29b10c (diff) | |
download | Vencord-ea748dfb605386b80a4919183ad6fa9249a82e21.tar.gz Vencord-ea748dfb605386b80a4919183ad6fa9249a82e21.tar.bz2 Vencord-ea748dfb605386b80a4919183ad6fa9249a82e21.zip |
feat: Typesafe Settings Definitions (#403)
Co-authored-by: Ven <vendicated@riseup.net>
Diffstat (limited to 'src/api/settings.ts')
-rw-r--r-- | src/api/settings.ts | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/api/settings.ts b/src/api/settings.ts index 384647c..9bae8b7 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -19,7 +19,7 @@ import IpcEvents from "@utils/IpcEvents"; import Logger from "@utils/Logger"; import { mergeDefaults } from "@utils/misc"; -import { OptionType } from "@utils/types"; +import { DefinedSettings, OptionType, SettingsChecks, SettingsDefinition } from "@utils/types"; import { React } from "@webpack/common"; import plugins from "~plugins"; @@ -146,6 +146,7 @@ export const Settings = makeProxy(settings); * @param paths An optional list of paths to whitelist for rerenders * @returns Settings */ +// TODO: Representing paths as essentially "string[].join('.')" wont allow dots in paths, change to "paths?: string[][]" later export function useSettings(paths?: string[]) { const [, forceUpdate] = React.useReducer(() => ({}), {}); @@ -200,3 +201,19 @@ export function migratePluginSettings(name: string, ...oldNames: string[]) { } } } + +export function definePluginSettings<D extends SettingsDefinition, C extends SettingsChecks<D>>(def: D, checks?: C) { + const definedSettings: DefinedSettings<D> = { + get store() { + if (!definedSettings.pluginName) throw new Error("Cannot access settings before plugin is initialized"); + return Settings.plugins[definedSettings.pluginName] as any; + }, + use: settings => useSettings( + settings?.map(name => `plugins.${definedSettings.pluginName}.${name}`) + ).plugins[definedSettings.pluginName] as any, + def, + checks: checks ?? {}, + pluginName: "", + }; + return definedSettings; +} |