Skip to content

Commit

Permalink
Only fetch logs if we need them on the current page
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Nov 22, 2024
1 parent 64957c9 commit d4a1d2c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions components/wallet-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useIndexedDB, { getDbName } from './use-indexeddb'
import { SSR } from '@/lib/constants'
import { decode as bolt11Decode } from 'bolt11'
import { formatMsats } from '@/lib/format'
import { useRouter } from 'next/router'

export function WalletLogs ({ wallet, embedded }) {
const { logs, setLogs, hasMore, loadMore, loading } = useWalletLogs(wallet)
Expand Down Expand Up @@ -204,6 +205,7 @@ export function useWalletLogs (wallet, initialPage = 1, logsPerPage = 10) {
const [cursor, setCursor] = useState(null)
const [loading, setLoading] = useState(true)
const { me } = useMe()
const router = useRouter()

const { getPage, error, notSupported } = useWalletLogDB()
const [getWalletLogs] = useLazyQuery(WALLET_LOGS, SSR ? {} : { fetchPolicy: 'cache-and-network' })
Expand Down Expand Up @@ -314,13 +316,15 @@ export function useWalletLogs (wallet, initialPage = 1, logsPerPage = 10) {
}, [logs, wallet?.def, loadLogsPage])

useEffect(() => {
if (me) {
// only fetch new logs if we are on a page that uses logs
const needLogs = router.asPath.startsWith('/settings/wallets') || router.asPath.startsWith('/wallet/logs')
if (me && needLogs) {
const interval = setInterval(() => {
loadNew().catch(console.error)
}, 1_000)
return () => clearInterval(interval)
}
}, [me?.id, loadNew])
}, [me?.id, router.pathname, loadNew])

return { logs, hasMore: !loading && hasMore, loadMore, setLogs, loading }
}
Expand Down

0 comments on commit d4a1d2c

Please sign in to comment.