-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create networkStats hook, update components to prevent rerender.
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
- Loading branch information
1 parent
91b3f4d
commit b0c42ea
Showing
4 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useIsMounted } from "~helpers/hooks/useIsMounted"; | ||
import { ServiceFactory } from "~factories/serviceFactory"; | ||
import { NOVA } from "~models/config/protocolVersion"; | ||
import { NovaApiClient } from "~services/nova/novaApiClient"; | ||
|
||
/** | ||
* Periodicaly refresh network stats. | ||
* @param network The network in context. | ||
* @returns The network stats. | ||
*/ | ||
export function useNetworkStats(network: string): [string] { | ||
const isMounted = useIsMounted(); | ||
const [apiClient] = useState(ServiceFactory.get<NovaApiClient>(`api-client-${NOVA}`)); | ||
const [updateTimerId, setUpdateTimerId] = useState<NodeJS.Timer | null>(null); | ||
const [blocksPerSecond, setBlocksPerSecond] = useState<string>("--"); | ||
|
||
useEffect(() => { | ||
if (network) { | ||
updateNetworkStats(); | ||
} | ||
|
||
return () => { | ||
if (updateTimerId) { | ||
clearTimeout(updateTimerId); | ||
setUpdateTimerId(null); | ||
} | ||
}; | ||
}, [network]); | ||
|
||
const updateNetworkStats = () => { | ||
if (isMounted && apiClient && network) { | ||
apiClient | ||
.stats({ | ||
network, | ||
includeHistory: true, | ||
}) | ||
.then((ips) => { | ||
const itemsPerSecond = ips.itemsPerSecond ?? 0; | ||
setBlocksPerSecond(itemsPerSecond >= 0 ? itemsPerSecond.toFixed(2) : "--"); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}) | ||
.finally(() => { | ||
setUpdateTimerId(setTimeout(async () => updateNetworkStats(), 4000)); | ||
}); | ||
} | ||
}; | ||
|
||
return [blocksPerSecond]; | ||
} |