From d711b4cd8506afa4e28f7aa38fe251d0d73d1cb3 Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Fri, 2 Apr 2021 10:12:11 +0200 Subject: [PATCH] refactor: addresses and peers --- src/hooks/apiHooks.tsx | 12 +++++------ src/pages/peers/PeerTable.tsx | 31 +++++++++++++++++++++------- src/pages/peers/index.tsx | 39 +++++++++++++++++------------------ src/services/bee.tsx | 4 ++-- 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/hooks/apiHooks.tsx b/src/hooks/apiHooks.tsx index 309004d..245d0aa 100644 --- a/src/hooks/apiHooks.tsx +++ b/src/hooks/apiHooks.tsx @@ -1,7 +1,7 @@ import { useState, useEffect } from "react"; import type { NodeAddresses, ChequebookAddressResponse, ChequebookBalanceResponse, BalanceResponse, - LastChequesResponse, AllSettlements, LastCashoutActionResponse, Health } from '@ethersphere/bee-js' + LastChequesResponse, AllSettlements, LastCashoutActionResponse, Health, Peer } from '@ethersphere/bee-js' import { beeDebugApi, beeApi } from '../services/bee'; @@ -58,7 +58,7 @@ export const useApiNodeAddresses = () => { setLoading(true) beeDebugApi.connectivity.addresses() .then(res => { - setNodeAddresses(res.data) + setNodeAddresses(res) }) .catch(error => { setError(error) @@ -116,15 +116,15 @@ export const useApiChequebookAddress = () => { } export const useApiNodePeers = () => { - const [nodePeers, setNodePeers] = useState({ peers: [{ address: '-'}]}) - const [isLoadingNodePeers, setLoading] = useState(false) + const [peers, setPeers] = useState(null) + const [isLoading, setLoading] = useState(false) const [error, setError] = useState(null) useEffect(() => { setLoading(true) beeDebugApi.connectivity.listPeers() .then(res => { - setNodePeers(res.data) + setPeers(res) }) .catch(error => { setError(error) @@ -134,7 +134,7 @@ export const useApiNodePeers = () => { }) }, []) - return { nodePeers, isLoadingNodePeers, error }; + return { peers, isLoading, error }; } export const useApiChequebookBalance = () => { diff --git a/src/pages/peers/PeerTable.tsx b/src/pages/peers/PeerTable.tsx index e2e7402..a5032f5 100644 --- a/src/pages/peers/PeerTable.tsx +++ b/src/pages/peers/PeerTable.tsx @@ -4,6 +4,7 @@ import { Table, TableBody, TableCell, TableContainer, TableRow, TableHead, Butto import { Autorenew } from '@material-ui/icons'; import { beeDebugApi } from '../../services/bee'; +import type { Peer } from '@ethersphere/bee-js'; const useStyles = makeStyles({ table: { @@ -11,8 +12,14 @@ const useStyles = makeStyles({ }, }); +interface Props { + peers: Peer[] | null + isLoading: boolean + error: Error | null +} + -function PeerTable(props: any) { +function PeerTable(props: Props) { const classes = useStyles(); const [peerLatency, setPeerLatency] = useState([{ peerId: '', rtt: '', loading: false }]); @@ -29,13 +36,24 @@ function PeerTable(props: any) { }) } - return ( -
- {props.loading ? + if (props.isLoading) { + return ( - : + ) + } + + if (props.error || props.peers === null) { + return ( + +

Failed to load peers

+
+ ) + } + + return ( +
@@ -46,7 +64,7 @@ function PeerTable(props: any) { - {props.nodePeers.peers.map((peer: any, idx: number) => ( + {props.peers.map((peer: any, idx: number) => ( {idx + 1} @@ -67,7 +85,6 @@ function PeerTable(props: any) {
- }
) } diff --git a/src/pages/peers/index.tsx b/src/pages/peers/index.tsx index 05a021d..afb1a59 100644 --- a/src/pages/peers/index.tsx +++ b/src/pages/peers/index.tsx @@ -6,16 +6,27 @@ import StatCard from '../../components/StatCard'; import PeerTable from './PeerTable'; import TroubleshootConnectionCard from '../../components/TroubleshootConnectionCard'; -import { useApiNodeTopology, useApiNodePeers } from '../../hooks/apiHooks'; +import { useApiNodeTopology, useApiNodePeers, useDebugApiHealth } from '../../hooks/apiHooks'; -export default function Peers(props: any) { +export default function Peers() { const { nodeTopology, isLoadingNodeTopology } = useApiNodeTopology() - const { nodePeers, isLoadingNodePeers } = useApiNodePeers() + const debugHealth = useDebugApiHealth() + const peers = useApiNodePeers() + + if (debugHealth.isLoadingNodeHealth) { + return ( + + + + ) + } + + if (debugHealth.error) { + return + } return ( -
- {props.nodeHealth?.status === 'ok' && props.health ? -
+ <> @@ -41,19 +52,7 @@ export default function Peers(props: any) { - -
- : - props.isLoadingHealth || props.isLoadingNodeHealth ? - - - - : - - } -
+ + ) } diff --git a/src/services/bee.tsx b/src/services/bee.tsx index 83bbdc6..5c6b8f4 100644 --- a/src/services/bee.tsx +++ b/src/services/bee.tsx @@ -57,10 +57,10 @@ export const beeDebugApi = { }, connectivity: { addresses() { - return beeDebugApiClient().get(`/addresses`) + return beeJSDebugClient().getNodeAddresses() }, listPeers() { - return beeDebugApiClient().get(`/peers`) + return beeJSDebugClient().getPeers() }, blockListedPeers() { return beeDebugApiClient().get(`/blocklist`)