From 7528e5c2b6a9e423cf309c76a6a1ffa5c4ee264a Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sun, 7 Jul 2024 09:43:37 +0200 Subject: [PATCH] Add optional wallet short name for logging --- components/wallet-logger.js | 16 ++++++++++------ components/wallet/lightning-address.js | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/components/wallet-logger.js b/components/wallet-logger.js index c3cbec62..7d0e467b 100644 --- a/components/wallet-logger.js +++ b/components/wallet-logger.js @@ -128,7 +128,7 @@ export const WalletLoggerProvider = ({ children }) => { .map(({ createdAt, wallet: walletType, ...log }) => { return { ts: +new Date(createdAt), - wallet: getServerWallet(walletType).name, + wallet: tag(getServerWallet(walletType)), ...log } }) @@ -199,7 +199,7 @@ export const WalletLoggerProvider = ({ children }) => { }, []) const appendLog = useCallback((wallet, level, message) => { - const log = { wallet: wallet.name, level, message, ts: +new Date() } + const log = { wallet: tag(wallet), level, message, ts: +new Date() } saveLog(log) setLogs((prevLogs) => [log, ...prevLogs]) }, [saveLog]) @@ -212,7 +212,7 @@ export const WalletLoggerProvider = ({ children }) => { const tx = idb.current.transaction(idbStoreName, 'readwrite') const objectStore = tx.objectStore(idbStoreName) const idx = objectStore.index('wallet_ts') - const request = wallet ? idx.openCursor(window.IDBKeyRange.bound([wallet.name, -Infinity], [wallet.name, Infinity])) : idx.openCursor() + const request = wallet ? idx.openCursor(window.IDBKeyRange.bound([tag(wallet), -Infinity], [tag(wallet), Infinity])) : idx.openCursor() request.onsuccess = function (event) { const cursor = event.target.result if (cursor) { @@ -220,7 +220,7 @@ export const WalletLoggerProvider = ({ children }) => { cursor.continue() } else { // finished - setLogs((logs) => logs.filter(l => wallet ? l.wallet !== wallet.name : false)) + setLogs((logs) => logs.filter(l => wallet ? l.wallet !== tag(wallet) : false)) } } } @@ -251,7 +251,7 @@ export function useWalletLogger (wallet) { // also send this to us if diagnostics was enabled, // very similar to how the service worker logger works. appendLog(wallet, level, message) - console[level !== 'error' ? 'info' : 'error'](`[${wallet.name}]`, message) + console[level !== 'error' ? 'info' : 'error'](`[${tag(wallet)}]`, message) }, [appendLog, wallet]) const logger = useMemo(() => ({ @@ -265,7 +265,11 @@ export function useWalletLogger (wallet) { return { logger, deleteLogs } } +function tag (wallet) { + return wallet?.shortName || wallet?.name +} + export function useWalletLogs (wallet) { const logs = useContext(WalletLogsContext) - return logs.filter(l => !wallet || l.wallet === wallet.name) + return logs.filter(l => !wallet || l.wallet === tag(wallet)) } diff --git a/components/wallet/lightning-address.js b/components/wallet/lightning-address.js index 3ca77cda..e472c17c 100644 --- a/components/wallet/lightning-address.js +++ b/components/wallet/lightning-address.js @@ -3,6 +3,7 @@ import { lnAddrAutowithdrawSchema } from '@/lib/validate' import { fetchLnAddrInvoice } from '@/lib/wallet' export const name = 'lightning-address' +export const shortName = 'lnAddr' export const fields = [ {