optimize api calls

This commit is contained in:
Riccardo Balbo 2024-10-15 00:34:20 +02:00 committed by k00b
parent 4aa9608212
commit 6bd07284a5
2 changed files with 31 additions and 26 deletions

View File

@ -22,7 +22,7 @@ import dynamic from 'next/dynamic'
import { HasNewNotesProvider } from '@/components/use-has-new-notes' import { HasNewNotesProvider } from '@/components/use-has-new-notes'
import { WebLnProvider } from '@/wallets/webln/client' import { WebLnProvider } from '@/wallets/webln/client'
import { AccountProvider } from '@/components/account' 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 }) const PWAPrompt = dynamic(() => import('react-ios-pwa-prompt'), { ssr: false })
@ -111,7 +111,7 @@ export default function MyApp ({ Component, pageProps: { ...props } }) {
<ServiceWorkerProvider> <ServiceWorkerProvider>
<AccountProvider> <AccountProvider>
<PriceProvider price={price}> <PriceProvider price={price}>
<WalletsMigrator> <WalletProvider>
<LightningProvider> <LightningProvider>
<ToastProvider> <ToastProvider>
<ShowModalProvider> <ShowModalProvider>
@ -126,7 +126,7 @@ export default function MyApp ({ Component, pageProps: { ...props } }) {
</ShowModalProvider> </ShowModalProvider>
</ToastProvider> </ToastProvider>
</LightningProvider> </LightningProvider>
</WalletsMigrator> </WalletProvider>
</PriceProvider> </PriceProvider>
</AccountProvider> </AccountProvider>
</ServiceWorkerProvider> </ServiceWorkerProvider>

View File

@ -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 { useMe } from '@/components/me'
import { openVault } from '@/components/use-vault' import { openVault } from '@/components/use-vault'
import { useWalletLogger } from '@/components/wallet-logger' import { useWalletLogger } from '@/components/wallet-logger'
@ -137,12 +137,6 @@ export function useWallet (name) {
return wallet return wallet
}, [walletDef, config, status, enabled, priority, logger, enablePayments, disablePayments, save, delete_, deleteLogs_, setPriority, hasConfig]) }, [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 return wallet
} }
@ -442,9 +436,12 @@ export function walletPrioritySort (w1, w2) {
return w1.card.title < w2.card.title ? -1 : 1 return w1.card.title < w2.card.title ? -1 : 1
} }
export function useWallets () { const WalletContext = createContext({
const wallets = walletDefs.map(def => useWallet(def.name)) wallets: []
})
export function useWallets () {
const { wallets } = useContext(WalletContext)
const resetClient = useCallback(async (wallet) => { const resetClient = useCallback(async (wallet) => {
for (const w of wallets) { for (const w of wallets) {
if (w.canSend) { if (w.canSend) {
@ -453,31 +450,35 @@ export function useWallets () {
await w.deleteLogs({ clientOnly: true }) await w.deleteLogs({ clientOnly: true })
} }
}, wallets) }, 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([]) const [walletsReady, setWalletsReady] = useState([])
useEffect(() => { useEffect(() => {
setWalletsReady(wallets.filter(w => w)) setWalletsReady(wallets.filter(w => w))
}, wallets) }, wallets)
return { wallets: walletsReady, resetClient } // migration
}
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)
useEffect(() => { useEffect(() => {
if (SSR || !me?.id || !wallets.length) return if (SSR || !me?.id || !wallets.length) return
if (ran.current) return if (migrationRan.current) return
ran.current = true migrationRan.current = true
if (!keys?.length) { if (!migratableKeys?.length) {
console.log('wallet migrator: nothing to migrate', keys) console.log('wallet migrator: nothing to migrate', migratableKeys)
return return
} }
const userId = me.id const userId = me.id
// List all local storage keys related to wallet settings // 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 () => { ;(async () => {
for (const key of userKeys) { for (const key of userKeys) {
const walletType = key.substring('wallet:'.length, key.length - userId.length - 1) const walletType = key.substring('wallet:'.length, key.length - userId.length - 1)
@ -493,5 +494,9 @@ export function WalletsMigrator ({ children }) {
} }
})() })()
}, [me, wallets]) }, [me, wallets])
return children return (
<WalletContext.Provider value={{ wallets: walletsReady }}>
{children}
</WalletContext.Provider>
)
} }