Header.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // @flow
  2. // Copyright 2017 The go-ethereum Authors
  3. // This file is part of the go-ethereum library.
  4. //
  5. // The go-ethereum library is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // The go-ethereum library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  17. import React, {Component} from 'react';
  18. import withStyles from '@material-ui/core/styles/withStyles';
  19. import AppBar from '@material-ui/core/AppBar';
  20. import Toolbar from '@material-ui/core/Toolbar';
  21. import IconButton from '@material-ui/core/IconButton';
  22. import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
  23. import {faBars, faSortAmountUp, faClock, faUsers, faSync} from '@fortawesome/free-solid-svg-icons';
  24. import Typography from '@material-ui/core/Typography';
  25. import type {Content} from '../types/content';
  26. const magnitude = [31536000, 604800, 86400, 3600, 60, 1];
  27. const label = ['y', 'w', 'd', 'h', 'm', 's'];
  28. // styles contains the constant styles of the component.
  29. const styles = {
  30. header: {
  31. height: '8%',
  32. },
  33. headerText: {
  34. marginRight: 15,
  35. },
  36. toolbar: {
  37. height: '100%',
  38. minHeight: 'unset',
  39. },
  40. };
  41. // themeStyles returns the styles generated from the theme for the component.
  42. const themeStyles = (theme: Object) => ({
  43. header: {
  44. backgroundColor: theme.palette.grey[900],
  45. color: theme.palette.getContrastText(theme.palette.grey[900]),
  46. zIndex: theme.zIndex.appBar,
  47. },
  48. toolbar: {
  49. paddingLeft: theme.spacing.unit,
  50. paddingRight: theme.spacing.unit,
  51. },
  52. title: {
  53. paddingLeft: theme.spacing.unit,
  54. fontSize: 3 * theme.spacing.unit,
  55. flex: 1,
  56. },
  57. });
  58. export type Props = {
  59. classes: Object, // injected by withStyles()
  60. switchSideBar: () => void,
  61. content: Content,
  62. networkID: number,
  63. };
  64. type State = {
  65. since: string,
  66. }
  67. // Header renders the header of the dashboard.
  68. class Header extends Component<Props, State> {
  69. constructor(props) {
  70. super(props);
  71. this.state = {since: ''};
  72. }
  73. componentDidMount() {
  74. this.interval = setInterval(() => this.setState(() => {
  75. // time (seconds) since last block.
  76. let timeDiff = Math.floor((Date.now() - this.props.content.chain.currentBlock.timestamp * 1000) / 1000);
  77. let since = '';
  78. let i = 0;
  79. for (; i < magnitude.length && timeDiff < magnitude[i]; i++);
  80. for (let j = 2; i < magnitude.length && j > 0; j--, i++) {
  81. const t = Math.floor(timeDiff / magnitude[i]);
  82. if (t > 0) {
  83. since += `${t}${label[i]} `;
  84. timeDiff %= magnitude[i];
  85. }
  86. }
  87. if (since === '') {
  88. since = 'now';
  89. }
  90. this.setState({since: since});
  91. }), 1000);
  92. }
  93. componentWillUnmount() {
  94. clearInterval(this.interval);
  95. }
  96. render() {
  97. const {classes} = this.props;
  98. return (
  99. <AppBar position='static' className={classes.header} style={styles.header}>
  100. <Toolbar className={classes.toolbar} style={styles.toolbar}>
  101. <IconButton onClick={this.props.switchSideBar}>
  102. <FontAwesomeIcon icon={faBars} />
  103. </IconButton>
  104. <Typography type='title' color='inherit' noWrap className={classes.title}>
  105. Go Ethereum Dashboard
  106. </Typography>
  107. <Typography style={styles.headerText}>
  108. <FontAwesomeIcon icon={faSortAmountUp} /> {this.props.content.chain.currentBlock.number}
  109. </Typography>
  110. <Typography style={styles.headerText}>
  111. <FontAwesomeIcon icon={faClock} /> {this.state.since}
  112. </Typography>
  113. <Typography style={styles.headerText}>
  114. <FontAwesomeIcon icon={faUsers} /> {this.props.content.network.activePeerCount}
  115. </Typography>
  116. </Toolbar>
  117. </AppBar>
  118. );
  119. }
  120. }
  121. export default withStyles(themeStyles)(Header);