|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import {Badge, Button, Card, Col, Container, ListGroup, Row, Stack} from "react-bootstrap"; |
| 3 | +import CertificateModal from '../../CertificateModal'; |
| 4 | +import RawJsonButton from "../../RawJsonButton"; |
| 5 | +import {useSelector} from "react-redux"; |
| 6 | + |
| 7 | +export default function MithrilStakeDistributionsList(props) { |
| 8 | + const [mithrilStakeDistributions, setMithrilStakeDistributions] = useState([]); |
| 9 | + const [selectedCertificateHash, setSelectedCertificateHash] = useState(undefined); |
| 10 | + const aggregator = useSelector((state) => state.settings.selectedAggregator); |
| 11 | + const autoUpdate = useSelector((state) => state.settings.autoUpdate); |
| 12 | + const updateInterval = useSelector((state) => state.settings.updateInterval); |
| 13 | + |
| 14 | + useEffect(() => { |
| 15 | + if (!autoUpdate) { |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + let fetchMithrilStakeDistribution = () => { |
| 20 | + fetch(`${aggregator}/artifact/mithril-stake-distributions`) |
| 21 | + .then(response => response.json()) |
| 22 | + .then(data => setMithrilStakeDistributions(data)) |
| 23 | + .catch(error => { |
| 24 | + setMithrilStakeDistributions([]); |
| 25 | + console.error("Fetch mithrilStakeDistributions error:", error); |
| 26 | + }); |
| 27 | + }; |
| 28 | + |
| 29 | + // Fetch them once without waiting |
| 30 | + fetchMithrilStakeDistribution(); |
| 31 | + |
| 32 | + const interval = setInterval(fetchMithrilStakeDistribution, updateInterval); |
| 33 | + return () => clearInterval(interval); |
| 34 | + }, [aggregator, updateInterval, autoUpdate]); |
| 35 | + |
| 36 | + function handleCertificateHashChange(hash) { |
| 37 | + setSelectedCertificateHash(hash); |
| 38 | + } |
| 39 | + |
| 40 | + function showCertificate(hash) { |
| 41 | + setSelectedCertificateHash(hash); |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <> |
| 46 | + <CertificateModal |
| 47 | + aggregator={aggregator} |
| 48 | + hash={selectedCertificateHash} |
| 49 | + onHashChange={handleCertificateHashChange}/> |
| 50 | + |
| 51 | + <div className={props.className}> |
| 52 | + <h2>Mithril Stake Distribution <RawJsonButton href={`${aggregator}/artifact/mithril-stake-distributions`} variant="outline-light" size="sm"/></h2> |
| 53 | + {Object.entries(mithrilStakeDistributions).length === 0 |
| 54 | + ? <p>No snapshot available</p> |
| 55 | + : |
| 56 | + <Container fluid> |
| 57 | + <Row xs={1} md={2} lg={3} xl={4}> |
| 58 | + {mithrilStakeDistributions.map((mithrilStakeDistribution, index) => |
| 59 | + <Col key={mithrilStakeDistribution.hash} className="mb-2"> |
| 60 | + <Card border={index === 0 ? "primary" : ""}> |
| 61 | + <Card.Body> |
| 62 | + <Card.Title>{mithrilStakeDistribution.hash}</Card.Title> |
| 63 | + <ListGroup variant="flush" className="data-list-group"> |
| 64 | + <ListGroup.Item>Epoch: {mithrilStakeDistribution.epoch}</ListGroup.Item> |
| 65 | + <ListGroup.Item>Certificate hash: <br/> |
| 66 | + {mithrilStakeDistribution.certificate_hash}{' '} |
| 67 | + <Button size="sm" onClick={() => showCertificate(mithrilStakeDistribution.certificate_hash)}>Show</Button> |
| 68 | + </ListGroup.Item> |
| 69 | + </ListGroup> |
| 70 | + </Card.Body> |
| 71 | + <Card.Footer> |
| 72 | + <Stack direction="horizontal" gap={1}> |
| 73 | + {index === 0 && |
| 74 | + <><Badge bg="primary">Latest</Badge>{' '}</> |
| 75 | + } |
| 76 | + |
| 77 | + <RawJsonButton href={`${aggregator}/artifact/mithril-stake-distribution/${mithrilStakeDistribution.hash}`} size="sm" |
| 78 | + className="ms-auto"/> |
| 79 | + </Stack> |
| 80 | + </Card.Footer> |
| 81 | + </Card> |
| 82 | + </Col> |
| 83 | + )} |
| 84 | + </Row> |
| 85 | + </Container> |
| 86 | + } |
| 87 | + </div> |
| 88 | + </> |
| 89 | + ); |
| 90 | +} |
0 commit comments