Guard block transaction fetch state
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
|
|
||||||
import { transactionsApi, type Transaction } from '@/services/api/transactions'
|
import { transactionsApi, type Transaction } from '@/services/api/transactions'
|
||||||
|
|
||||||
@@ -16,9 +16,14 @@ export function useBlockTransactions({ blockNumber, chainId, enabled }: UseBlock
|
|||||||
const [error, setError] = useState(false)
|
const [error, setError] = useState(false)
|
||||||
const [hasNextPage, setHasNextPage] = useState(false)
|
const [hasNextPage, setHasNextPage] = useState(false)
|
||||||
const [page, setPage] = useState(1)
|
const [page, setPage] = useState(1)
|
||||||
|
const previousBlockNumberRef = useRef(blockNumber)
|
||||||
|
|
||||||
const loadTransactions = useCallback(async () => {
|
useEffect(() => {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
|
previousBlockNumberRef.current = blockNumber
|
||||||
|
if (page !== 1) {
|
||||||
|
setPage(1)
|
||||||
|
}
|
||||||
setTransactions([])
|
setTransactions([])
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
setError(false)
|
setError(false)
|
||||||
@@ -26,37 +31,40 @@ export function useBlockTransactions({ blockNumber, chainId, enabled }: UseBlock
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (previousBlockNumberRef.current !== blockNumber) {
|
||||||
|
previousBlockNumberRef.current = blockNumber
|
||||||
|
if (page !== 1) {
|
||||||
|
setPage(1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancelled = false
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
setError(false)
|
setError(false)
|
||||||
|
|
||||||
const result = await transactionsApi.listByBlockSafe(
|
void (async () => {
|
||||||
chainId,
|
const result = await transactionsApi.listByBlockSafe(
|
||||||
blockNumber,
|
chainId,
|
||||||
page,
|
blockNumber,
|
||||||
DEFAULT_BLOCK_TRANSACTION_PAGE_SIZE,
|
page,
|
||||||
)
|
DEFAULT_BLOCK_TRANSACTION_PAGE_SIZE,
|
||||||
setTransactions(result.items)
|
)
|
||||||
setHasNextPage(result.hasNextPage)
|
|
||||||
setError(!result.ok)
|
|
||||||
setLoading(false)
|
|
||||||
}, [blockNumber, chainId, enabled, page])
|
|
||||||
|
|
||||||
useEffect(() => {
|
if (cancelled) {
|
||||||
if (!enabled) {
|
return
|
||||||
setPage(1)
|
}
|
||||||
setTransactions([])
|
|
||||||
|
setTransactions(result.items)
|
||||||
|
setHasNextPage(result.hasNextPage)
|
||||||
|
setError(!result.ok)
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
setError(false)
|
})()
|
||||||
setHasNextPage(false)
|
|
||||||
return
|
return () => {
|
||||||
|
cancelled = true
|
||||||
}
|
}
|
||||||
|
}, [blockNumber, chainId, enabled, page])
|
||||||
setPage(1)
|
|
||||||
}, [blockNumber, enabled])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void loadTransactions()
|
|
||||||
}, [loadTransactions])
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
transactions,
|
transactions,
|
||||||
|
|||||||
Reference in New Issue
Block a user