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) {
const me = useMe()
const storageKey = getStorageKey(wallet?.name, me)
const [localConfig, setLocalConfig, clearLocalConfig] = useLocalConfig(storageKey)
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) => {
if (wallet.sendPayment) {
setLocalConfig(config)
} else {
await setServerConfig(config)
}
if (hasLocalConfig) setLocalConfig(config)
if (hasServerConfig) await setServerConfig(config)
}, [wallet])
const clearConfig = useCallback(async () => {
if (wallet.sendPayment) {
clearLocalConfig()
} else {
await clearServerConfig()
}
})
if (hasLocalConfig) clearLocalConfig()
if (hasServerConfig) await clearServerConfig()
}, [wallet])
return [config, saveConfig, clearConfig]
}