import { useState, useEffect } from "react"; import type { NodeAddresses, ChequebookAddressResponse, ChequebookBalanceResponse, BalanceResponse, LastChequesResponse, AllSettlements, LastCashoutActionResponse, Health, Peer, Topology } from '@ethersphere/bee-js' import { beeDebugApi, beeApi } from '../services/bee'; export const useApiHealth = () => { const [health, setHealth] = useState(false) const [isLoadingHealth, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeApi.status.health() .then(res => { setHealth(res) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { health, isLoadingHealth, error } ; } export const useDebugApiHealth = () => { const [nodeHealth, setNodeHealth] = useState(null) const [isLoadingNodeHealth, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.status.nodeHealth() .then(res => { setNodeHealth(res) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { nodeHealth, isLoadingNodeHealth, error } ; } export const useApiNodeAddresses = () => { const [nodeAddresses, setNodeAddresses] = useState(null) const [isLoadingNodeAddresses, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.connectivity.addresses() .then(res => { setNodeAddresses(res) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { nodeAddresses, isLoadingNodeAddresses, error } ; } export const useApiNodeTopology = () => { const [topology, setNodeTopology] = useState(null) const [isLoading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.connectivity.topology() .then(res => { setNodeTopology(res) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { topology, isLoading, error } ; } export const useApiChequebookAddress = () => { const [chequebookAddress, setChequebookAddress] = useState(null) const [isLoadingChequebookAddress, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.chequebook.address() .then(res => { setChequebookAddress(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { chequebookAddress, isLoadingChequebookAddress, error }; } export const useApiNodePeers = () => { const [peers, setPeers] = useState(null) const [isLoading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.connectivity.listPeers() .then(res => { setPeers(res) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { peers, isLoading, error }; } export const useApiChequebookBalance = () => { const [chequebookBalance, setChequebookBalance] = useState(null) const [isLoadingChequebookBalance, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.chequebook.balance() .then(res => { setChequebookBalance(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { chequebookBalance, isLoadingChequebookBalance, error }; } export const useApiPeerBalances = () => { const [peerBalances, setPeerBalances] = useState(null) const [isLoadingPeerBalances, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.balance.balances() .then(res => { setPeerBalances(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { peerBalances, isLoadingPeerBalances, error }; } export const useApiPeerCheques = () => { const [peerCheques, setPeerCheques] = useState(null) const [isLoadingPeerCheques, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.chequebook.getLastCheques() .then(res => { setPeerCheques(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { peerCheques, isLoadingPeerCheques, error }; } export const useApiPeerLastCheque = (peerId: string) => { const [peerCheque, setPeerCheque] = useState({ peer: '-', chequebook: "", cumulativePayout: 0, beneficiary: "", transactionHash: "", result: { recipient: "", lastPayout: 0, bounced: false }} ) const [isLoadingPeerCheque, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.chequebook.getPeerLastCheques(peerId) .then(res => { setPeerCheque(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, [peerId]) return { peerCheque, isLoadingPeerCheque, error }; } export const useApiSettlements = () => { const [settlements, setSettlements] = useState(null) const [isLoadingSettlements, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.settlements.getSettlements() .then(res => { setSettlements(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, []) return { settlements, isLoadingSettlements, error }; } export const useApiPingPeer = (peerId: string) => { const [peerRTP, setPeerRTP] = useState('') const [isPingingPeer, setPingingPeer] = useState(false) const [error, setError] = useState(null) useEffect(() => { setPingingPeer(true) beeDebugApi.connectivity.ping(peerId) .then(res => { setPeerRTP(res.data) }) .catch(error => { setError(error) }) .finally(() => { setPingingPeer(false) }) }, [peerId]) return { peerRTP, isPingingPeer, error }; } export const useApiPeerLastCashout = (peerId: string) => { const [peerCashout, setPeerCashout] = useState(null) const [isLoadingPeerCashout, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.chequebook.getPeerLastCashout(peerId) .then(res => { setPeerCashout(res.data) }) .catch(error => { setError(error) }) .finally(() => { setLoading(false) }) }, [peerId]) return { peerCashout, isLoadingPeerCashout, error }; }