feat: changing API urls does not need the app refresh (#173)
* feat: changing API urls does not need the app refresh * fix: propagate beeDebugApi and beeApi change to the refresh interval * fix: any failed request on the Bee provider does not stop the execution of other requests * fix: error handling for incorrect bee and bee debug urls * fix: change debug API in the settings tab
This commit is contained in:
+12
-22
@@ -1,16 +1,11 @@
|
||||
import { LastCashoutActionResponse } from '@ethersphere/bee-js'
|
||||
import { LastCashoutActionResponse, BeeDebug } from '@ethersphere/bee-js'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Token } from '../models/Token'
|
||||
import { beeDebugApi } from '../services/bee'
|
||||
import { makeRetriablePromise, unwrapPromiseSettlements } from '../utils'
|
||||
import { Balance, Settlement, useApiPeerBalances, useApiSettlements } from './apiHooks'
|
||||
import { Balance, Settlements, Settlement } from '../types'
|
||||
|
||||
interface UseAccountingHook {
|
||||
isLoading: boolean
|
||||
isLoadingUncashed: boolean
|
||||
error: Error | null
|
||||
totalsent: Token
|
||||
totalreceived: Token
|
||||
accounting: Accounting[] | null
|
||||
}
|
||||
|
||||
@@ -77,39 +72,34 @@ function mergeAccounting(
|
||||
)
|
||||
}
|
||||
|
||||
export const useAccounting = (): UseAccountingHook => {
|
||||
const settlements = useApiSettlements()
|
||||
const balances = useApiPeerBalances()
|
||||
|
||||
export const useAccounting = (
|
||||
beeDebugApi: BeeDebug | null,
|
||||
settlements: Settlements | null,
|
||||
balances: Balance[] | null,
|
||||
): UseAccountingHook => {
|
||||
const [isLoadingUncashed, setIsloadingUncashed] = useState<boolean>(false)
|
||||
const [uncashedAmounts, setUncashedAmounts] = useState<LastCashoutActionResponse[] | undefined>(undefined)
|
||||
|
||||
const error = balances.error || settlements.error
|
||||
|
||||
useEffect(() => {
|
||||
// We don't have any settlements loaded yet or we are already loading/have loaded the uncashed amounts
|
||||
if (isLoadingUncashed || !settlements.settlements || uncashedAmounts || error) return
|
||||
if (isLoadingUncashed || !beeDebugApi || !settlements || uncashedAmounts) return
|
||||
|
||||
setIsloadingUncashed(true)
|
||||
const promises = settlements.settlements.settlements
|
||||
const promises = settlements.settlements
|
||||
.filter(({ received }) => received.toBigNumber.gt('0'))
|
||||
.map(({ peer }) => makeRetriablePromise(() => beeDebugApi.chequebook.getPeerLastCashout(peer)))
|
||||
.map(({ peer }) => makeRetriablePromise(() => beeDebugApi.getLastCashoutAction(peer)))
|
||||
|
||||
Promise.allSettled(promises).then(settlements => {
|
||||
const results = unwrapPromiseSettlements(settlements)
|
||||
setUncashedAmounts(results.fulfilled)
|
||||
setIsloadingUncashed(false)
|
||||
})
|
||||
}, [settlements, isLoadingUncashed, uncashedAmounts, error])
|
||||
}, [settlements, isLoadingUncashed, uncashedAmounts])
|
||||
|
||||
const accounting = mergeAccounting(balances.peerBalances, settlements.settlements?.settlements, uncashedAmounts)
|
||||
const accounting = mergeAccounting(balances, settlements?.settlements, uncashedAmounts)
|
||||
|
||||
return {
|
||||
isLoading: settlements.isLoadingSettlements || balances.isLoadingPeerBalances,
|
||||
isLoadingUncashed,
|
||||
error,
|
||||
accounting,
|
||||
totalsent: settlements.settlements?.totalSent || new Token('0'),
|
||||
totalreceived: settlements.settlements?.totalReceived || new Token('0'),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,94 +1,5 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
|
||||
import { beeDebugApi } from '../services/bee'
|
||||
import axios from 'axios'
|
||||
import { Token } from '../models/Token'
|
||||
|
||||
export interface Balance {
|
||||
peer: string
|
||||
balance: Token
|
||||
}
|
||||
|
||||
export interface PeerBalanceHook {
|
||||
peerBalances: Balance[] | null
|
||||
isLoadingPeerBalances: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiPeerBalances = (): PeerBalanceHook => {
|
||||
const [peerBalances, setPeerBalances] = useState<Balance[] | null>(null)
|
||||
const [isLoadingPeerBalances, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true)
|
||||
beeDebugApi.balance
|
||||
.balances()
|
||||
.then(res => {
|
||||
// for some reason sometimes these are numbers and not BigInts
|
||||
const balances = res.balances.map(({ peer, balance }) => ({ peer, balance: new Token(balance) }))
|
||||
setPeerBalances(balances)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
}, [])
|
||||
|
||||
return { peerBalances, isLoadingPeerBalances, error }
|
||||
}
|
||||
|
||||
export interface Settlement {
|
||||
peer: string
|
||||
received: Token
|
||||
sent: Token
|
||||
}
|
||||
|
||||
export interface Settlements {
|
||||
totalReceived: Token
|
||||
totalSent: Token
|
||||
settlements: Settlement[]
|
||||
}
|
||||
|
||||
export interface SettlementsHook {
|
||||
settlements: Settlements | null
|
||||
isLoadingSettlements: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
export const useApiSettlements = (): SettlementsHook => {
|
||||
const [settlements, setSettlements] = useState<Settlements | null>(null)
|
||||
const [isLoadingSettlements, setLoading] = useState<boolean>(true)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true)
|
||||
beeDebugApi.settlements
|
||||
.getSettlements()
|
||||
.then(({ totalReceived, settlements, totalSent }) => {
|
||||
const set = {
|
||||
totalReceived: new Token(totalReceived),
|
||||
totalSent: new Token(totalSent),
|
||||
settlements: settlements.map(({ peer, received, sent }) => ({
|
||||
peer,
|
||||
received: new Token(received),
|
||||
sent: new Token(sent),
|
||||
})),
|
||||
}
|
||||
setSettlements(set)
|
||||
})
|
||||
.catch(error => {
|
||||
setError(error)
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false)
|
||||
})
|
||||
}, [])
|
||||
|
||||
return { settlements, isLoadingSettlements, error }
|
||||
}
|
||||
|
||||
export interface LatestBeeReleaseHook {
|
||||
latestBeeRelease: LatestBeeRelease | null
|
||||
|
||||
Reference in New Issue
Block a user