aboutsummaryrefslogtreecommitdiff
path: root/src/api/settings.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/settings.ts')
-rw-r--r--src/api/settings.ts19
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;
+}