ValidatorsStats.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Button, Card, CardActions, CardContent, makeStyles, Typography } from '@material-ui/core';
  2. import { ActiveEra } from './Types';
  3. const useStyles = makeStyles({
  4. root: {
  5. minWidth: '98%',
  6. textAlign: 'left'
  7. },
  8. title: {
  9. fontSize: 18,
  10. },
  11. pos: {
  12. marginBottom: 12,
  13. marginTop: 12,
  14. },
  15. });
  16. export const ValidatorsStats = (props: { stash: String, activeEras: ActiveEra[]; }) => {
  17. const classes = useStyles();
  18. let sortedByBlock = props.activeEras
  19. let noDataLabel = 'No Data Available'
  20. let firstBlock: ActiveEra | undefined = undefined
  21. let lastBlock: ActiveEra | undefined = undefined
  22. let scoringPeriodText = ''
  23. const copyValidatorStatistics = () => navigator.clipboard.writeText(scoringPeriodText)
  24. if(props.activeEras.length > 0) {
  25. sortedByBlock = props.activeEras.sort((e1,e2) => e1.block - e2.block)
  26. firstBlock = sortedByBlock[0];
  27. lastBlock = sortedByBlock[sortedByBlock.length - 1];
  28. scoringPeriodText = `Validator Date: ${new Date(firstBlock!.date).toLocaleDateString()}-${new Date(lastBlock!.date).toLocaleDateString()}\nDescription: I was an active validator from era/block ${firstBlock!.era}/${firstBlock!.block} to era/block ${lastBlock!.era}/${lastBlock!.block}\nwith stash account ${props.stash}. (I was active in all the eras in this range and found a total of ? blocks)`
  29. return (
  30. <Card className={classes.root}>
  31. <CardContent>
  32. <Typography className={classes.title} color="textPrimary" gutterBottom>
  33. Scoring period text:
  34. </Typography>
  35. { scoringPeriodText.split('\n').map((i, key) => <Typography key={key} className={classes.pos} color="textSecondary">{i}</Typography>) }
  36. </CardContent>
  37. <CardActions>
  38. <Button onClick={copyValidatorStatistics} size="small">Copy to clipboard</Button>
  39. </CardActions>
  40. </Card>
  41. );
  42. } else {
  43. return (
  44. <Card className={classes.root}>
  45. <CardContent>
  46. <Typography className={classes.pos} color="textSecondary">
  47. { noDataLabel }
  48. </Typography>
  49. </CardContent>
  50. </Card>
  51. )
  52. }
  53. };