From 36e9f3f16fd3ef0ea7edf32fe84ab252f7f53020 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 12 Sep 2024 18:09:44 +0200 Subject: [PATCH] useWallet hook cleanup (#1396) * Assign everything to wallet object * Add canReceive for sake of completeness --- wallets/index.js | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/wallets/index.js b/wallets/index.js index 4d1cbf8fc..3587a64e3 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -45,12 +45,6 @@ export function useWallet (name) { logger.info('payments disabled') }, [name, me, logger]) - if (wallet) { - wallet.isConfigured = _isConfigured - wallet.enablePayments = enablePayments - wallet.disablePayments = disablePayments - } - const status = config?.enabled ? Status.Enabled : Status.Initialized const enabled = status === Status.Enabled const priority = config?.priority @@ -95,21 +89,30 @@ export function useWallet (name) { if (!wallet) return null - return { - ...wallet, - canSend: !!wallet.sendPayment, - sendPayment, - config, - save, - delete: delete_, - deleteLogs, - setPriority, - hasConfig, - status, - enabled, - priority, - logger - } + // Assign everything to wallet object so every function that is passed this wallet object in this + // `useWallet` hook has access to all others via the reference to it. + // Essentially, you can now use functions like `enablePayments` _inside_ of functions that are + // called by `useWallet` even before enablePayments is defined and not only in functions + // that use the return value of `useWallet`. + wallet.isConfigured = _isConfigured + wallet.enablePayments = enablePayments + wallet.disablePayments = disablePayments + wallet.canSend = !!wallet.sendPayment + wallet.canReceive = !!wallet.createInvoice + wallet.config = config + wallet.save = save + wallet.delete = delete_ + wallet.deleteLogs = deleteLogs + wallet.setPriority = setPriority + wallet.hasConfig = hasConfig + wallet.status = status + wallet.enabled = enabled + wallet.priority = priority + wallet.logger = logger + + // can't assign sendPayment to wallet object because it already exists + // as an imported function and thus can't be overwritten + return { ...wallet, sendPayment } } function extractConfig (fields, config, client) {