From 23f9e9dfae955dbcc15445a77db2e906c82dbdd7 Mon Sep 17 00:00:00 2001 From: herrrta <73949927+herrrta@users.noreply.github.com> Date: Sun, 12 Jan 2025 19:23:32 -0500 Subject: [PATCH] fix: Override default settings with plugin unlocked default settings - This sets the defaults on login and allows users to still change them --- utils/atoms/settings.ts | 54 +++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/utils/atoms/settings.ts b/utils/atoms/settings.ts index 993c722d..d92f7a74 100644 --- a/utils/atoms/settings.ts +++ b/utils/atoms/settings.ts @@ -222,27 +222,6 @@ export const useSettings = () => { [api] ) - // We do not want to save over users pre-existing settings in case admin ever removes/unlocks a setting. - // If admin sets locked to false but provides a value, - // use user settings first and fallback on admin setting if required. - const settings: Settings = useMemo(() => { - const overrideSettings = Object.entries(pluginSettings || {}) - .reduce((acc, [key, setting]) => { - if (setting) { - const {value, locked} = setting - acc = Object.assign(acc, { - [key]: locked ? value : _settings?.[key as keyof Settings] ?? value - }) - } - return acc - }, {} as Settings) - - return { - ..._settings, - ...overrideSettings - } - }, [_settings, setSettings, pluginSettings, _setPluginSettings, setPluginSettings]) - const updateSettings = (update: Partial) => { if (settings) { const newSettings = { ...settings, ...update }; @@ -252,5 +231,38 @@ export const useSettings = () => { } }; + // We do not want to save over users pre-existing settings in case admin ever removes/unlocks a setting. + // If admin sets locked to false but provides a value, + // use user settings first and fallback on admin setting if required. + const settings: Settings = useMemo(() => { + let unlockedPluginDefaults = {} as Settings; + const overrideSettings = Object.entries(pluginSettings || {}) + .reduce((acc, [key, setting]) => { + if (setting) { + const {value, locked} = setting + + // Make sure we override default settings with plugin settings when they are not locked. + // Admin decided what users defaults should be and grants them the ability to change them too. + if (!locked && value && _settings?.[key as keyof Settings] !== value) { + unlockedPluginDefaults = Object.assign(unlockedPluginDefaults, {[key as keyof Settings]: value}) + } + + acc = Object.assign(acc, { + [key]: locked ? value : _settings?.[key as keyof Settings] ?? value + }) + } + return acc + }, {} as Settings) + + // Update settings with plugin defined defaults + if (Object.keys(unlockedPluginDefaults).length > 0) { + updateSettings(unlockedPluginDefaults) + } + return { + ..._settings, + ...overrideSettings + } + }, [_settings, pluginSettings]) + return [settings, updateSettings, pluginSettings, setPluginSettings, refreshStreamyfinPluginSettings] as const; };