Only include local/server config if required

This commit is contained in:
ekzyis 2024-07-03 18:24:05 +02:00
parent e091377d94
commit d8e82ddea5
1 changed files with 17 additions and 12 deletions

View File

@ -97,27 +97,32 @@ export function useWallet (name) {
function useConfig (wallet) { function useConfig (wallet) {
const me = useMe() const me = useMe()
const storageKey = getStorageKey(wallet?.name, me) const storageKey = getStorageKey(wallet?.name, me)
const [localConfig, setLocalConfig, clearLocalConfig] = useLocalConfig(storageKey) const [localConfig, setLocalConfig, clearLocalConfig] = useLocalConfig(storageKey)
const [serverConfig, setServerConfig, clearServerConfig] = useServerConfig(wallet) const [serverConfig, setServerConfig, clearServerConfig] = useServerConfig(wallet)
const config = { ...localConfig, ...serverConfig } const hasLocalConfig = !!wallet?.sendPayment
const hasServerConfig = !!wallet?.server
const config = {
// only include config if it makes sense for this wallet
// since server config always returns default values for autowithdraw settings
// which might be confusing to have for wallets that don't support autowithdraw
...(hasLocalConfig ? localConfig : {}),
...(hasServerConfig ? serverConfig : {})
}
const saveConfig = useCallback(async (config) => { const saveConfig = useCallback(async (config) => {
if (wallet.sendPayment) { if (hasLocalConfig) setLocalConfig(config)
setLocalConfig(config) if (hasServerConfig) await setServerConfig(config)
} else {
await setServerConfig(config)
}
}, [wallet]) }, [wallet])
const clearConfig = useCallback(async () => { const clearConfig = useCallback(async () => {
if (wallet.sendPayment) { if (hasLocalConfig) clearLocalConfig()
clearLocalConfig() if (hasServerConfig) await clearServerConfig()
} else { }, [wallet])
await clearServerConfig()
}
})
return [config, saveConfig, clearConfig] return [config, saveConfig, clearConfig]
} }