From b0207a290660b3eb37d006543caf0008e2efd08d Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 24 Nov 2024 03:19:22 +0100 Subject: [PATCH] Create useWalletStatus and useWalletSupport (#1646) --- components/wallet-card.js | 19 ++++++--------- components/wallet-status.js | 47 ++++++++++++++++++++++++++++++++++++ components/wallet-support.js | 8 ++++++ wallets/common.js | 28 --------------------- wallets/index.js | 24 +++--------------- 5 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 components/wallet-status.js create mode 100644 components/wallet-support.js diff --git a/components/wallet-card.js b/components/wallet-card.js index 14ae570c..df5ad5e2 100644 --- a/components/wallet-card.js +++ b/components/wallet-card.js @@ -8,18 +8,13 @@ import DraggableIcon from '@/svgs/draggable.svg' import RecvIcon from '@/svgs/arrow-left-down-line.svg' import SendIcon from '@/svgs/arrow-right-up-line.svg' import { useWalletImage } from '@/components/wallet-image' - -const statusToClass = status => { - switch (status) { - case Status.Enabled: return styles.success - case Status.Disabled: return styles.disabled - case Status.Error: return styles.error - case Status.Warning: return styles.warning - } -} +import { useWalletStatus, statusToClass } from '@/components/wallet-status' +import { useWalletSupport } from '@/components/wallet-support' export default function WalletCard ({ wallet, draggable, onDragStart, onDragEnter, onDragEnd, onTouchStart, sourceIndex, targetIndex, index }) { const image = useWalletImage(wallet) + const status = useWalletStatus(wallet) + const support = useWalletSupport(wallet) return (
- {wallet.status.any !== Status.Disabled && } - {wallet.support.recv && } - {wallet.support.send && } + {status.any !== Status.Disabled && } + {support.recv && } + {support.send && }
{ + if (status.any === Status.Disabled) return status + + // override status depending on if there have been warnings or errors in the logs recently + // find first log from which we can derive status (logs are sorted by recent first) + const walletLogs = logs.filter(l => l.wallet === wallet.def.name) + const sendLevel = walletLogs.find(l => l.context?.status && l.context?.send)?.level + const recvLevel = walletLogs.find(l => l.context?.status && l.context?.recv)?.level + + const levelToStatus = (level) => { + switch (level?.toLowerCase()) { + case 'ok': + case 'success': return Status.Enabled + case 'error': return Status.Error + case 'warn': return Status.Warning + } + } + + return { + any: status.any, + send: levelToStatus(sendLevel) || status.send, + recv: levelToStatus(recvLevel) || status.recv + } +} + +export const statusToClass = status => { + switch (status) { + case Status.Enabled: return styles.success + case Status.Disabled: return styles.disabled + case Status.Error: return styles.error + case Status.Warning: return styles.warning + } +} diff --git a/components/wallet-support.js b/components/wallet-support.js new file mode 100644 index 00000000..270d35c2 --- /dev/null +++ b/components/wallet-support.js @@ -0,0 +1,8 @@ +import { supportsReceive, supportsSend } from '@/wallets/common' + +export function useWalletSupport (wallet) { + return { + send: supportsSend(wallet), + recv: supportsReceive(wallet) + } +} diff --git a/wallets/common.js b/wallets/common.js index c36d70aa..c0c98047 100644 --- a/wallets/common.js +++ b/wallets/common.js @@ -175,31 +175,3 @@ export async function saveWalletLocally (name, config, userId) { const storageKey = getStorageKey(name, userId) window.localStorage.setItem(storageKey, JSON.stringify(config)) } - -export const statusFromLog = (wallet, logs) => { - if (wallet.status.any === Status.Disabled) return wallet - - // override status depending on if there have been warnings or errors in the logs recently - // find first log from which we can derive status (logs are sorted by recent first) - const walletLogs = logs.filter(l => l.wallet === wallet.def.name) - const sendLevel = walletLogs.find(l => l.context?.status && l.context?.send)?.level - const recvLevel = walletLogs.find(l => l.context?.status && l.context?.recv)?.level - - const levelToStatus = (level) => { - switch (level?.toLowerCase()) { - case 'ok': - case 'success': return Status.Enabled - case 'error': return Status.Error - case 'warn': return Status.Warning - } - } - - return { - ...wallet, - status: { - ...wallet.status, - send: levelToStatus(sendLevel) || wallet.status.send, - recv: levelToStatus(recvLevel) || wallet.status.recv - } - } -} diff --git a/wallets/index.js b/wallets/index.js index c2e83eed..0347c63b 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -3,9 +3,9 @@ import { SET_WALLET_PRIORITY, WALLETS } from '@/fragments/wallet' import { SSR } from '@/lib/constants' import { useApolloClient, useMutation, useQuery } from '@apollo/client' import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react' -import { getStorageKey, getWalletByType, Status, walletPrioritySort, canSend, isConfigured, upsertWalletVariables, siftConfig, saveWalletLocally, canReceive, supportsReceive, supportsSend, statusFromLog } from './common' +import { getStorageKey, getWalletByType, walletPrioritySort, canSend, isConfigured, upsertWalletVariables, siftConfig, saveWalletLocally } from './common' import useVault from '@/components/vault/use-vault' -import { useWalletLogger, useWalletLogs } from '@/components/wallet-logger' +import { useWalletLogger } from '@/components/wallet-logger' import { decode as bolt11Decode } from 'bolt11' import walletDefs from '@/wallets/client' import { generateMutation } from './graphql' @@ -67,7 +67,6 @@ export function WalletsProvider ({ children }) { const [setWalletPriority] = useMutation(SET_WALLET_PRIORITY) const [serverWallets, setServerWallets] = useState([]) const client = useApolloClient() - const { logs } = useWalletLogs() const { data, refetch } = useQuery(WALLETS, SSR ? {} : { nextFetchPolicy: 'cache-and-network' }) @@ -130,23 +129,8 @@ export function WalletsProvider ({ children }) { } // sort by priority, then add status field - return Object.values(merged) - .sort(walletPrioritySort) - .map(w => { - return { - ...w, - support: { - recv: supportsReceive(w), - send: supportsSend(w) - }, - status: { - any: w.config?.enabled && isConfigured(w) ? Status.Enabled : Status.Disabled, - send: w.config?.enabled && canSend(w) ? Status.Enabled : Status.Disabled, - recv: w.config?.enabled && canReceive(w) ? Status.Enabled : Status.Disabled - } - } - }).map(w => statusFromLog(w, logs)) - }, [serverWallets, localWallets, logs]) + return Object.values(merged).sort(walletPrioritySort) + }, [serverWallets, localWallets]) const settings = useMemo(() => { return {