From 6bd07284a5cd66b3a42b86fd5a22179ac960e0cd Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Tue, 15 Oct 2024 00:34:20 +0200 Subject: [PATCH] optimize api calls --- pages/_app.js | 6 +++--- wallets/index.js | 51 ++++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/pages/_app.js b/pages/_app.js index 7980945a..6c27b837 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -22,7 +22,7 @@ import dynamic from 'next/dynamic' import { HasNewNotesProvider } from '@/components/use-has-new-notes' import { WebLnProvider } from '@/wallets/webln/client' import { AccountProvider } from '@/components/account' -import { WalletsMigrator } from '@/wallets/index' +import { WalletProvider } from '@/wallets/index' const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false }) @@ -111,7 +111,7 @@ export default function MyApp ({ Component, pageProps: { ...props } }) { - + @@ -126,7 +126,7 @@ export default function MyApp ({ Component, pageProps: { ...props } }) { - + diff --git a/wallets/index.js b/wallets/index.js index 5406bd4e..f81ffe9f 100644 --- a/wallets/index.js +++ b/wallets/index.js @@ -1,4 +1,4 @@ -import { useCallback, useState, useEffect, useRef, useMemo } from 'react' +import { useContext, createContext, useCallback, useState, useEffect, useRef, useMemo } from 'react' import { useMe } from '@/components/me' import { openVault } from '@/components/use-vault' import { useWalletLogger } from '@/components/wallet-logger' @@ -137,12 +137,6 @@ export function useWallet (name) { return wallet }, [walletDef, config, status, enabled, priority, logger, enablePayments, disablePayments, save, delete_, deleteLogs_, setPriority, hasConfig]) - useEffect(() => { - if (wallet.enabled && wallet.canSend) { - disableFreebies().catch(console.error) - } - }, [wallet]) - return wallet } @@ -442,9 +436,12 @@ export function walletPrioritySort (w1, w2) { return w1.card.title < w2.card.title ? -1 : 1 } -export function useWallets () { - const wallets = walletDefs.map(def => useWallet(def.name)) +const WalletContext = createContext({ + wallets: [] +}) +export function useWallets () { + const { wallets } = useContext(WalletContext) const resetClient = useCallback(async (wallet) => { for (const w of wallets) { if (w.canSend) { @@ -453,31 +450,35 @@ export function useWallets () { await w.deleteLogs({ clientOnly: true }) } }, wallets) + return { wallets, resetClient } +} + +export function WalletProvider ({ children }) { + if (SSR) return children + + const { me } = useMe() + const migrationRan = useRef(false) + const migratableKeys = !migrationRan.current && !SSR ? Object.keys(window.localStorage).filter(k => k.startsWith('wallet:')) : undefined + + const wallets = walletDefs.map(def => useWallet(def.name)) const [walletsReady, setWalletsReady] = useState([]) useEffect(() => { setWalletsReady(wallets.filter(w => w)) }, wallets) - return { wallets: walletsReady, resetClient } -} - -export function WalletsMigrator ({ children }) { - const { me } = useMe() - const { wallets } = useWallets() - const keys = !SSR ? Object.keys(window.localStorage).filter(k => k.startsWith('wallet:')) : [] - const ran = useRef(false) + // migration useEffect(() => { if (SSR || !me?.id || !wallets.length) return - if (ran.current) return - ran.current = true - if (!keys?.length) { - console.log('wallet migrator: nothing to migrate', keys) + if (migrationRan.current) return + migrationRan.current = true + if (!migratableKeys?.length) { + console.log('wallet migrator: nothing to migrate', migratableKeys) return } const userId = me.id // List all local storage keys related to wallet settings - const userKeys = keys.filter(k => k.endsWith(`:${userId}`)) + const userKeys = migratableKeys.filter(k => k.endsWith(`:${userId}`)) ;(async () => { for (const key of userKeys) { const walletType = key.substring('wallet:'.length, key.length - userId.length - 1) @@ -493,5 +494,9 @@ export function WalletsMigrator ({ children }) { } })() }, [me, wallets]) - return children + return ( + + {children} + + ) }