import React, { useState, useEffect, useContext } from "react";
import { useParams } from "react-router-dom";
import { Card, Row, Col, Container, Collapse, Spinner } from "react-bootstrap";
import { QueryPreservingLink } from "src/services/network/networkProvider";
import { NetworkContext } from "src/services/network/networkProvider";
import {
qaToZil,
timestampToTimeago,
timestampToDisplay,
pubKeyToZilAddr,
} from "src/utils/Utils";
import { DsBlockObj, MinerInfo } from "@zilliqa-js/core/src/types";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faCaretSquareLeft,
faCaretSquareRight,
} from "@fortawesome/free-regular-svg-icons";
import {
faCubes,
faAngleUp,
faAngleLeft,
faAngleRight,
faAngleDown,
} from "@fortawesome/free-solid-svg-icons";
import NotFoundPage from "../../ErrorPages/NotFoundPage";
import MinerTable from "./MinerTable/MinerTable";
import LabelStar from "../Misc/LabelComponent/LabelStar";
import "./DSBlockDetailsPage.css";
const DSBlockDetailsPage: React.FC = () => {
const { blockNum } = useParams<{ blockNum: string }>();
const networkContext = useContext(NetworkContext);
if (!networkContext) {
return (
);
}
const { dataService } = networkContext;
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState(null);
const [data, setData] = useState(null);
const [latestDSBlockNum, setLatestDSBlockNum] = useState(null);
const [minerInfo, setMinerInfo] = useState(null);
const [currShardIdx, setCurrShardIdx] = useState(0);
const [showMore, setShowMore] = useState(false);
// Fetch data
useEffect(() => {
setIsLoading(true);
if (!dataService) return;
let latestDSBlockNum: number;
let receivedData: DsBlockObj;
let minerInfo: MinerInfo;
const getData = async () => {
try {
if (isNaN(parseInt(blockNum)))
throw new Error("Not a valid block number");
receivedData = await dataService.getDSBlockDetails(blockNum);
latestDSBlockNum = await dataService.getNumDSBlocks();
try {
// wrapped in another try catch because it is optional
minerInfo = await dataService.getMinerInfo(blockNum);
} catch (e) {
console.log(e);
}
if (receivedData) setData(receivedData);
if (latestDSBlockNum) setLatestDSBlockNum(latestDSBlockNum);
if (minerInfo) setMinerInfo(minerInfo);
} catch (e) {
console.error(e);
// TODO: Get the structure of the error and extract the message
setError("An error occurred - please see the console.");
} finally {
setIsLoading(false);
}
};
getData();
return () => {
setData(null);
setLatestDSBlockNum(null);
setMinerInfo(null);
setError(null);
setCurrShardIdx(0);
setShowMore(false);
};
}, [blockNum, dataService]);
return (
<>
{isLoading ? (
) : null}
{error ? (
) : (
data && (
<>
DS Block{" "}
#{data.header.BlockNum}
Date:
{timestampToDisplay(data.header.Timestamp)} (
{timestampToTimeago(data.header.Timestamp)})
Difficulty:
{data.header.Difficulty}
DS Difficulty:
{data.header.DifficultyDS}
Gas Price:
{qaToZil(data.header.GasPrice)}
DS Leader:
{pubKeyToZilAddr(data.header.LeaderPubKey)}
Signature:
{data.signature}
{data.header.PoWWinners.length > 0 && (
PoW Winners
{data.header.PoWWinners.map((x, index) => (
[{index}]{" "}
{pubKeyToZilAddr(x)}
))}
)}
{minerInfo && (
<>
DS Committee
Total:{" "}
{minerInfo.dscommittee.length}
{minerInfo.dscommittee.length > 0 ? (
) : (
No addresses to show
)}
Shard {currShardIdx + 1} of{" "}
{minerInfo.shards.length}
setCurrShardIdx(currShardIdx - 1)
}
className={
currShardIdx === 0 ? "disabled" : ""
}
icon={faAngleLeft}
/>
setCurrShardIdx(currShardIdx + 1)
}
className={
currShardIdx ===
minerInfo.shards.length - 1
? "disabled ml-3"
: "ml-3"
}
icon={faAngleRight}
/>
Total:{" "}
{
minerInfo.shards[currShardIdx].nodes
.length
}
{minerInfo.shards[currShardIdx].nodes.length >
0 ? (
) : (
No addresses to show
)}
>
)}
setShowMore(!showMore)}
>
>
)
)}
>
);
};
export default DSBlockDetailsPage;