Peers.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017-2019 @polkadot/app-nodeinfo authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { PeerInfo } from '@polkadot/types/interfaces';
  5. import { I18nProps } from '@polkadot/react-components/types';
  6. import React from 'react';
  7. import { formatNumber } from '@polkadot/util';
  8. import translate from './translate';
  9. interface Props extends I18nProps {
  10. peers?: PeerInfo[] | null;
  11. }
  12. class Peers extends React.PureComponent<Props> {
  13. public render (): React.ReactNode {
  14. const { t } = this.props;
  15. return (
  16. <section className='status--Peers'>
  17. <h1>{t('connected peers')}</h1>
  18. {this.renderPeers()}
  19. </section>
  20. );
  21. }
  22. private renderPeers (): React.ReactNode {
  23. const { peers, t } = this.props;
  24. if (!peers || !peers.length) {
  25. return (
  26. <div className='ui disabled'>
  27. {t('no peers connected')}
  28. </div>
  29. );
  30. }
  31. return (
  32. <article>
  33. <table>
  34. <thead>
  35. <tr>
  36. <th className='roles'>{t('role')}</th>
  37. <th className='peerid ui--media-medium'>{t('peer id')}</th>
  38. <th className='number'>{t('best #')}</th>
  39. <th className='hash'>{t('best hash')}</th>
  40. </tr>
  41. </thead>
  42. <tbody>
  43. {peers
  44. .sort((a, b): number => b.bestNumber.cmp(a.bestNumber))
  45. .map(this.renderPeer)
  46. }
  47. </tbody>
  48. </table>
  49. </article>
  50. );
  51. }
  52. private renderPeer = (peer: PeerInfo): React.ReactNode => {
  53. const peerId = peer.peerId.toString();
  54. return (
  55. <tr key={peerId}>
  56. <td className='roles'>{peer.roles.toString().toLowerCase()}</td>
  57. <td className='peerid ui--media-medium'>{peerId}</td>
  58. <td className='number'>{formatNumber(peer.bestNumber)}</td>
  59. <td className='hash'>{peer.bestHash.toHex()}</td>
  60. </tr>
  61. );
  62. }
  63. }
  64. export default translate(Peers);